Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ public static string ExpandPathEnvironmentVariables(string path)

path = Regex.Replace(path, @"(?<!\\)\$([a-zA-Z0-9_]+)", "%$1%");
path = Environment.ExpandEnvironmentVariables(path);

// Special case: If the path contains "%2F", which represents an encoded forward slash,
// Special case: If the path contains URL-encoded forward slash (%2F) or space (%20),
// we assume it's part of a URL-encoded filename and not an unexpanded environment variable.
// In this case, we return the path as is to avoid incorrectly modifying the filename.
// Example: "~/Octopus/Files/Data%2FFileTransferService%[email protected]"
// Here, %2F is part of the filename and should not be treated as an environment variable.
if (path.Contains("%2F") || path.Contains("%2f"))
// Example: "~/Octopus/Files/https_dev.azure.com_Org_ADO%[email protected]"
if (path.Contains("%2F") || path.Contains("%2f") || path.Contains("%20"))
{
return path;
}

// Clean up any remaining unexpanded environment variables.
// This removes any %VAR% patterns that weren't expanded,
// ensuring only valid, expanded variables remain in the path.
return Regex.Replace(path, @"(?<!\\)%([a-zA-Z0-9_]+)%", "");
}
Expand Down
2 changes: 2 additions & 0 deletions source/Calamari.Tests/Fixtures/Util/CrossPlatformFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void TildePrefixReplacedWithHome()
[TestCase("\\$MARIO_BROTHER/blah", "\\$MARIO_BROTHER/blah", Description = "Escaped dollar preserved")]
[TestCase("$MARIO_BROTHER/blah%2fblah.zip", "LUIGI/blah%2fblah.zip", Description = "Linux-style variable ($VAR) is expanded, but URL-encoded slash (%2f) in filename is preserved")]
[TestCase("%MARIO_BROTHER%/blah%2fblah.zip", "LUIGI/blah%2fblah.zip", Description = "Windows-style variable (%VAR%) is expanded, and URL-encoded slash (%2f) in filename is preserved")]
[TestCase("$MARIO_BROTHER/blah%20blah.zip", "LUIGI/blah%20blah.zip", Description = "Linux-style variable ($VAR) is expanded, but URL-encoded space (%20) in filename is preserved")]
[TestCase("%MARIO_BROTHER%/blah%20blah.zip", "LUIGI/blah%20blah.zip", Description = "Windows-style variable (%VAR%) is expanded, and URL-encoded space (%20) in filename is preserved")]
[Category(TestCategory.CompatibleOS.OnlyNixOrMac)]
public void NixEnvironmentVariableReplaced(string inputValue, string expectedResult)
{
Expand Down