-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(lib): Allow env_add_path and shortcuts field to use variables #6616
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
Open
abgox
wants to merge
5
commits into
ScoopInstaller:develop
Choose a base branch
from
abgox:allow-var-in-field
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+29
−6
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45eb5eb
feat(lib): allow env_add_path to use variables
abgox 7991f7a
feat(lib): allow shortcuts to use variables
abgox 3c05571
docs(changelog): update CHANGLOG
abgox 7611222
feat: allow env_add_path to include variables defined in env_set
abgox 0c22b23
refactor: use -like to check variables
abgox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security: Expanded paths bypass containment validation.
The
is_in_dircheck that prevents adding arbitrary paths outside the app directory is completely bypassed for expanded variables. A malicious or misconfigured manifest could add system directories to PATH via entries like$env:WINDIR\System32.Additionally, the past review concern remains unaddressed: undefined variables like
$foo\binwill expand to\binsinceExpandString()replaces undefined variables with empty strings, corrupting the intended path.🛡️ Proposed fix: validate expanded paths and check for supported variables only
$path = $env_add_path.Where({ $_ }) | ForEach-Object { - if ($_ -like '$*'){ - $ExecutionContext.InvokeCommand.ExpandString($_) - }else{ + # Only expand known safe variables ($env:*, $home) + $usesSupportedVariable = $_ -match '(^|[^`])\$(env:|home\b|\{env:|\{home\})' + if ($usesSupportedVariable) { + $path_expand = $ExecutionContext.InvokeCommand.ExpandString($_) + # Only use expanded value if expansion actually changed it + # (undefined variables expand to empty, corrupting the path) + if ($path_expand -ne $_ -and $path_expand -ne '') { + $path_expand + } + } else { Join-Path $dir $_ | Get-AbsolutePath | Where-Object { is_in_dir $dir $_ } } }Consider whether expanded paths should also be validated against a whitelist of allowed directories, or at minimum logged for audit purposes.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More save solution would be to use Scoop core
substitutefunction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Let me look at the
substitutefunction implementation to understand its behavior.🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 1074
@chendler, thesubstituteapproach is appealing because it preserves theis_in_dircontainment check. However, there are a few issues with the proposed implementation worth considering:Substitution happens after
Join-Path— for an entry like$env:ProgramFiles\Git\bin, the pipeline first produces<appdir>\$env:ProgramFiles\Git\bin, and then substitution transforms it into<appdir>\C:\Program Files\Git\bin, which is a garbled path.Key format mismatch — the mapping uses
'$' + $_.Name(e.g.,$PROGRAMFILES), but the issue examples (#6605) use$env:ProgramFilesstyle. These keys won't match.PowerShell automatic variables (e.g.,
$home) aren't inEnv:*—Get-ChildItem Env:*only yields Windows environment variables, so$home\.local\binwould never be substituted.is_in_dirwill always reject absolute expanded paths — even if substitution worked, an expanded absolute path likeC:\Program Files\Git\binis intentionally outside$dir, so it gets silently dropped by the.Where({ is_in_dir $dir $_ })filter, meaning no variable-based absolute path would ever actually be added.A corrected version that addresses these points would need to substitute before
Join-Path, include both$env:NAMEand$NAMEkeys, add PowerShell automatic variables, and then branch on whether the result is an absolute or relative path:That said, skipping
is_in_dirfor expanded absolute paths is an intentional trade-off — it's the whole point of the feature. The security question then shifts to whether manifests in trusted buckets are sufficient gatekeeping, or whether an explicit allowlist of variable prefixes (e.g., only$env:*and$home) is warranted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can agree that $env: can be used as a prefix
$home is just an alias to $env:USERPROFILE
Omitting is_in_dir check will contradict manifest spec defined here https://github.com/ScoopInstaller/Scoop/wiki/App-Manifests#optional-properties
I assume that features built-in features prevent side effects, for other special cases there is post_install script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chendler, all three points are well-taken. Agreed on all counts:$env:prefix needed.$homeis redundant —$env:USERPROFILEcovers it.is_in_dircheck must be preserved per the manifest spec. That constraint also provides a natural security boundary: any variable that expands to an absolute path outside$dirwill fail the containment check and be silently dropped, which is the correct behavior.With those constraints in mind, here's the corrected implementation that substitutes
$env:variables beforeJoin-Path(fixing the ordering issue from my earlier comment) while keeping the existing containment pipeline intact:And symmetrically for
env_rm_path:Key properties of this approach:
substituteuses-replace(case-insensitive in PowerShell), so$env:ProgramFiles,$env:PROGRAMFILES, and$env:programfilesall match correctly against the hashtable built fromGet-ChildItem Env:*.$_ -like '$*'branch is gone entirely; every entry goes through the same pipeline.is_in_diris always enforced — if an env var happens to expand to an absolute path,Join-Path $dir <absolute>produces a garbled path that fails containment, so it gets dropped. Intentional absolute paths outside the app dir belong inpost_install, as you noted.✏️ Learnings added