Skip to content

Commit 31e377a

Browse files
authored
Block path separators in aliases (#7244)
1 parent b260088 commit 31e377a

22 files changed

Lines changed: 421 additions & 13 deletions

File tree

.github/copilot-instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- When creating pull requests, always follow the [PR template](PULL_REQUEST_TEMPLATE.md).
66
- Always format before submitting a pull request.
7+
- Before implementing any code changes, read all files in the `docs/` folder. It contains the NuGet development guidelines, including rules for SDKAnalysisLevel gating, public API policies, error handling patterns, and feature design requirements.
78

89
## Coding Standards
910

src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ private bool BeforeGraphResolutionValidations(int httpSourcesCount)
363363
success &= EvaluateHttpSourceUsage();
364364
success &= HasValidPlatformVersions();
365365
success &= PackageReferencesHaveVersions();
366+
success &= EnsureNoAliasesWithDisallowedCharacters();
366367

367368
return success;
368369
}
@@ -844,6 +845,70 @@ private bool HasValidPlatformVersions()
844845
}
845846
}
846847

848+
private bool EnsureNoAliasesWithDisallowedCharacters()
849+
{
850+
return EnsureNoAliasesWithDisallowedCharacters(_request.Project, _logger);
851+
}
852+
853+
internal static bool EnsureNoAliasesWithDisallowedCharacters(PackageSpec project, ILogger logger)
854+
{
855+
if (!SdkAnalysisLevelMinimums.IsEnabled(project.RestoreMetadata.SdkAnalysisLevel, project.RestoreMetadata.UsingMicrosoftNETSdk, SdkAnalysisLevelMinimums.V10_0_300))
856+
{
857+
return true;
858+
}
859+
860+
bool nonAsciiIsError = SdkAnalysisLevelMinimums.IsEnabled(project.RestoreMetadata.SdkAnalysisLevel, project.RestoreMetadata.UsingMicrosoftNETSdk, SdkAnalysisLevelMinimums.V11_0_100);
861+
var success = true;
862+
863+
foreach (TargetFrameworkInformation framework in project.TargetFrameworks)
864+
{
865+
string alias = framework.TargetAlias;
866+
if (string.IsNullOrEmpty(alias))
867+
{
868+
continue;
869+
}
870+
871+
if (alias.Contains('/') || alias.Contains('\\'))
872+
{
873+
logger.Log(RestoreLogMessage.CreateError(
874+
NuGetLogCode.NU1019,
875+
string.Format(CultureInfo.CurrentCulture, Strings.Log_AliasContainsDisallowedCharacters, project.Name, alias)));
876+
success = false;
877+
}
878+
else if (!IsAscii(alias))
879+
{
880+
if (nonAsciiIsError)
881+
{
882+
logger.Log(RestoreLogMessage.CreateError(
883+
NuGetLogCode.NU1019,
884+
string.Format(CultureInfo.CurrentCulture, Strings.Log_AliasContainsDisallowedCharacters, project.Name, alias)));
885+
success = false;
886+
}
887+
else
888+
{
889+
logger.Log(RestoreLogMessage.CreateWarning(
890+
NuGetLogCode.NU1019,
891+
string.Format(CultureInfo.CurrentCulture, Strings.Log_AliasContainsDisallowedCharacters, project.Name, alias)));
892+
}
893+
}
894+
}
895+
896+
return success;
897+
}
898+
899+
internal static bool IsAscii(string value)
900+
{
901+
foreach (char c in value.AsSpan())
902+
{
903+
if (c > 127)
904+
{
905+
return false;
906+
}
907+
}
908+
909+
return true;
910+
}
911+
847912
internal static void AnalyzePruningResults(PackageSpec project, TelemetryEvent telemetryEvent, ILogger logger)
848913
{
849914
bool enablePruningWarnings = HasFrameworkNewerThanNET10(project);

src/NuGet.Core/NuGet.Commands/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NuGet.Core/NuGet.Commands/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,4 +1174,7 @@ NuGet requires HTTPS sources. Refer to https://aka.ms/nuget-https-everywhere for
11741174
<value>The project {0} is attempting to restore duplicate frameworks, which are supported only in the default dependency resolver when the .NET SDK is version {1} or newer.
11751175
Upgrade your .NET SDK or remove RestoreUseLegacyDependencyResolver to use this feature.</value>
11761176
</data>
1177+
<data name="Log_AliasContainsDisallowedCharacters" xml:space="preserve">
1178+
<value>The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</value>
1179+
</data>
11771180
</root>

src/NuGet.Core/NuGet.Commands/Utility/SdkAnalysisLevelMinimums.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,19 @@ internal static class SdkAnalysisLevelMinimums
2929
/// Minimum SDK Analysis Level required for:
3030
/// <list type="bullet">
3131
/// <item>.NET SDK version that supports aliased assets files.</item>
32+
/// <item>error when TargetFramework alias contains path separators characters</item>
3233
/// </list>
3334
/// </summary>
3435
internal static readonly NuGetVersion V10_0_300 = new("10.0.300");
3536

37+
/// <summary>
38+
/// Minimum SDK Analysis Level required for:
39+
/// <list type="bullet">
40+
/// <item>error when TargetFramework alias contains non-ASCII characters</item>
41+
/// </list>
42+
/// </summary>
43+
internal static readonly NuGetVersion V11_0_100 = new("11.0.100");
44+
3645
/// <summary>
3746
/// Determines whether the feature is enabled based on the SDK analysis level.
3847
/// </summary>

src/NuGet.Core/NuGet.Commands/xlf/Strings.cs.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
33
<file datatype="xml" source-language="en" target-language="cs" original="../Strings.resx">
44
<body>
@@ -678,6 +678,11 @@ Další informace najdete tady: https://docs.nuget.org/docs/reference/command-li
678678
<target state="translated">Místní prostředky se částečně vymazaly.</target>
679679
<note />
680680
</trans-unit>
681+
<trans-unit id="Log_AliasContainsDisallowedCharacters">
682+
<source>The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</source>
683+
<target state="new">The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</target>
684+
<note />
685+
</trans-unit>
681686
<trans-unit id="Log_AliasingSupportedInNewDependencyResolver">
682687
<source>The project {0} is attempting to restore duplicate frameworks, which are supported only in the default dependency resolver when the .NET SDK is version {1} or newer.
683688
Upgrade your .NET SDK or remove RestoreUseLegacyDependencyResolver to use this feature.</source>

src/NuGet.Core/NuGet.Commands/xlf/Strings.de.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
33
<file datatype="xml" source-language="en" target-language="de" original="../Strings.resx">
44
<body>
@@ -678,6 +678,11 @@ Weitere Informationen finden Sie unter https://docs.nuget.org/docs/reference/com
678678
<target state="translated">Die lokalen Ressourcen wurden teilweise gelöscht.</target>
679679
<note />
680680
</trans-unit>
681+
<trans-unit id="Log_AliasContainsDisallowedCharacters">
682+
<source>The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</source>
683+
<target state="new">The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</target>
684+
<note />
685+
</trans-unit>
681686
<trans-unit id="Log_AliasingSupportedInNewDependencyResolver">
682687
<source>The project {0} is attempting to restore duplicate frameworks, which are supported only in the default dependency resolver when the .NET SDK is version {1} or newer.
683688
Upgrade your .NET SDK or remove RestoreUseLegacyDependencyResolver to use this feature.</source>

src/NuGet.Core/NuGet.Commands/xlf/Strings.es.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
33
<file datatype="xml" source-language="en" target-language="es" original="../Strings.resx">
44
<body>
@@ -678,6 +678,11 @@ Para obtener más información, visite https://docs.nuget.org/docs/reference/com
678678
<target state="translated">Recursos locales parcialmente borrados.</target>
679679
<note />
680680
</trans-unit>
681+
<trans-unit id="Log_AliasContainsDisallowedCharacters">
682+
<source>The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</source>
683+
<target state="new">The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</target>
684+
<note />
685+
</trans-unit>
681686
<trans-unit id="Log_AliasingSupportedInNewDependencyResolver">
682687
<source>The project {0} is attempting to restore duplicate frameworks, which are supported only in the default dependency resolver when the .NET SDK is version {1} or newer.
683688
Upgrade your .NET SDK or remove RestoreUseLegacyDependencyResolver to use this feature.</source>

src/NuGet.Core/NuGet.Commands/xlf/Strings.fr.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
33
<file datatype="xml" source-language="en" target-language="fr" original="../Strings.resx">
44
<body>
@@ -678,6 +678,11 @@ Pour plus d'informations, visitez https://docs.nuget.org/docs/reference/command-
678678
<target state="translated">Ressources locales partiellement effacées.</target>
679679
<note />
680680
</trans-unit>
681+
<trans-unit id="Log_AliasContainsDisallowedCharacters">
682+
<source>The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</source>
683+
<target state="new">The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</target>
684+
<note />
685+
</trans-unit>
681686
<trans-unit id="Log_AliasingSupportedInNewDependencyResolver">
682687
<source>The project {0} is attempting to restore duplicate frameworks, which are supported only in the default dependency resolver when the .NET SDK is version {1} or newer.
683688
Upgrade your .NET SDK or remove RestoreUseLegacyDependencyResolver to use this feature.</source>

src/NuGet.Core/NuGet.Commands/xlf/Strings.it.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
33
<file datatype="xml" source-language="en" target-language="it" original="../Strings.resx">
44
<body>
@@ -678,6 +678,11 @@ Per altre informazioni, vedere https://docs.nuget.org/docs/reference/command-lin
678678
<target state="translated">Le risorse locali sono state parzialmente cancellate.</target>
679679
<note />
680680
</trans-unit>
681+
<trans-unit id="Log_AliasContainsDisallowedCharacters">
682+
<source>The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</source>
683+
<target state="new">The project {0} contains a TargetFramework '{1}' with disallowed characters. TargetFramework names must contain only ASCII characters and must not contain path separators.</target>
684+
<note />
685+
</trans-unit>
681686
<trans-unit id="Log_AliasingSupportedInNewDependencyResolver">
682687
<source>The project {0} is attempting to restore duplicate frameworks, which are supported only in the default dependency resolver when the .NET SDK is version {1} or newer.
683688
Upgrade your .NET SDK or remove RestoreUseLegacyDependencyResolver to use this feature.</source>

0 commit comments

Comments
 (0)