-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add UntrustedLocation check #11286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UntrustedLocation check #11286
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Build.Construction; | ||
using Microsoft.Build.Shared; | ||
|
||
namespace Microsoft.Build.Experimental.BuildCheck.Checks; | ||
internal sealed class UntrustedLocationCheck : Check | ||
YuliiaKovalova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public static CheckRule SupportedRule = new CheckRule( | ||
"BC0301", | ||
"UntrustedLocation", | ||
ResourceUtilities.GetResourceString("BuildCheck_BC0301_Title")!, | ||
ResourceUtilities.GetResourceString("BuildCheck_BC0301_MessageFmt")!, | ||
new CheckConfiguration() { Severity = CheckResultSeverity.Error }); | ||
|
||
public override string FriendlyName => "DotUtils.UntrustedLocationCheck"; | ||
|
||
public override IReadOnlyList<CheckRule> SupportedRules { get; } = new List<CheckRule>() { SupportedRule }; | ||
|
||
public override void Initialize(ConfigurationContext configurationContext) | ||
{ | ||
checkedProjects.Clear(); | ||
} | ||
|
||
internal override bool IsBuiltIn => true; | ||
|
||
public override void RegisterActions(IBuildCheckRegistrationContext registrationContext) | ||
{ | ||
registrationContext.RegisterEvaluatedPropertiesAction(EvaluatedPropertiesAction); | ||
} | ||
|
||
private HashSet<string> checkedProjects = new HashSet<string>(); | ||
|
||
private void EvaluatedPropertiesAction(BuildCheckDataContext<EvaluatedPropertiesCheckData> context) | ||
{ | ||
if (checkedProjects.Add(context.Data.ProjectFilePath) && | ||
context.Data.ProjectFileDirectory.StartsWith(PathsHelper.Downloads, Shared.FileUtilities.PathComparison)) | ||
{ | ||
context.ReportResult(BuildCheckResult.Create( | ||
SupportedRule, | ||
ElementLocation.EmptyLocation, | ||
context.Data.ProjectFileDirectory, | ||
context.Data.ProjectFilePath.Substring(context.Data.ProjectFileDirectory.Length + 1))); | ||
} | ||
} | ||
|
||
private static class PathsHelper | ||
{ | ||
public static readonly string Downloads = GetDownloadsPath(); | ||
|
||
private static string GetDownloadsPath() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
try | ||
{ | ||
// based on doc - a final slash is not added | ||
return SHGetKnownFolderPath(new Guid("374DE290-123F-4565-9164-39C4925E467B"), 0, IntPtr.Zero); | ||
} | ||
catch | ||
{ | ||
// ignored | ||
} | ||
} | ||
|
||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads"); | ||
} | ||
|
||
[DllImport("shell32", | ||
CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false)] | ||
private static extern string SHGetKnownFolderPath( | ||
[MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, | ||
IntPtr hToken); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.