Repository navigation
fix(#9157): treat "export -n NAME" as un-exporting NAME #9159
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
base: main
Are you sure you want to change the base?
Changes from all commits
69344e2
9a1636a
9b1d492
82e8f25
116eb03
47cccc2
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 |
|---|---|---|
|
|
@@ -21,16 +21,33 @@ var envNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) | |
| // | ||
| // A plain assignment with no command ("NAME=value" alone, or "readonly | ||
| // NAME=value") is shell-local and is not reported, nor is a prefix | ||
| // assignment on a shell builtin ("IFS= read"). Assignment-shaped text | ||
| // assignment on a shell builtin ("IFS= read"). "export -n NAME" removes | ||
| // the export attribute, so a name the scripts un-export after exporting | ||
| // it is not reported; scripts are read in order as one shell, and an | ||
| // "sh -c" script is its own shell whose un-exports do not reach its | ||
| // parent. Prefix assignments affect only their command's process, and a | ||
| // name exported when a process launches stays in that process's | ||
| // environment, so a later "export -n" does not retract either. "export | ||
| // -f" works on functions, so "export -fn NAME" leaves variable NAME | ||
| // exported. An "export" with an option other than -f, -n or -p | ||
| // changes nothing, and "command -v" only looks a command up and | ||
| // "builtin" runs only a shell builtin, so neither launches a process. | ||
| // Parentheses are only separators, so an "export -n" inside a | ||
| // ( ... ) subshell is treated as if it ran in the parent shell. | ||
| // Assignment-shaped text | ||
| // inside a quoted value is data, not a binding, and a command | ||
| // substitution's exports stay in its subshell. The parser is a tokenizer, | ||
| // not a shell: it handles single and double quotes, backslash escapes, | ||
| // $(...) nesting, comments, and the ; && || | & ( ) { } newline | ||
| // separators, which is the subset the runner's emitters generate. | ||
| func ExportedNames(scripts ...string) []string { | ||
| seen := map[string]bool{} | ||
| exported := map[string]bool{} | ||
| for _, script := range scripts { | ||
| collectExportedNames(script, seen, 0) | ||
| collectExportedNames(script, seen, exported, 0) | ||
| } | ||
| for name := range exported { | ||
| seen[name] = true | ||
| } | ||
| names := make([]string, 0, len(seen)) | ||
| for name := range seen { | ||
|
|
@@ -48,6 +65,10 @@ var shellBuiltins = map[string]bool{ | |
| "echo": true, "eval": true, "exit": true, "false": true, "printf": true, | ||
| "read": true, "return": true, "set": true, "shift": true, "test": true, | ||
| "true": true, "unset": true, | ||
| "readonly": true, "local": true, "declare": true, "typeset": true, | ||
| "let": true, "pwd": true, "wait": true, "trap": true, "umask": true, | ||
| "getopts": true, "source": true, ".": true, "alias": true, | ||
| "hash": true, "type": true, "ulimit": true, "builtin": true, | ||
| } | ||
|
|
||
| // maxShellDepth bounds sh -c recursion so a malformed input cannot loop. | ||
|
|
@@ -74,17 +95,21 @@ func (w shellWord) assignmentName() string { | |
| return name | ||
| } | ||
|
|
||
| func collectExportedNames(script string, seen map[string]bool, depth int) { | ||
| // collectExportedNames records the names script exports. seen collects | ||
| // names that reach a child process for certain (prefix and env | ||
| // assignments, and a finished child shell's exports); exported holds the | ||
| // current shell's export attributes, which a later "export -n" can clear. | ||
| func collectExportedNames(script string, seen, exported map[string]bool, depth int) { | ||
| if depth > maxShellDepth { | ||
| return | ||
| } | ||
| for _, words := range shellCommands(script) { | ||
| collectFromCommand(words, seen, depth) | ||
| collectFromCommand(words, seen, exported, depth) | ||
| } | ||
| } | ||
|
|
||
| // collectFromCommand records the names one simple command exports. | ||
| func collectFromCommand(words []shellWord, seen map[string]bool, depth int) { | ||
| func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth int) { | ||
| // Leading NAME=value words are prefix assignments when a command | ||
| // follows them, and shell-local assignments when nothing does. | ||
| i := 0 | ||
|
|
@@ -99,17 +124,55 @@ func collectFromCommand(words []shellWord, seen map[string]bool, depth int) { | |
| for _, w := range words[:i] { | ||
| seen[w.assignmentName()] = true | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error Prefix assignments are added to seen before it is known whether export, command or exec launches a process, so "Y=1 command -v true" and "Y=1 export -n Y" report Y. The new builtin forwarding re-dispatches the prefix words and inherits this. Over-reporting only; the command case exists on the base branch. Suggested fix: Carry temporary prefix bindings through dispatch and record them only when the resolved operation launches a process; add tests for prefix assignments on lookup-only command and on export -n. |
||
| } | ||
| // Launching a process hands it the shell's current export | ||
| // attributes, so a later "export -n" cannot retract them. "export" | ||
| // and "command" run inside the shell (command may run a builtin), | ||
| // and "exec" with no command launches nothing. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error Previously reported finding remains open: low logic-error in internal/envmanifest/shellexports.go:130. |
||
| if cmd != "export" && cmd != "command" && (cmd != "exec" || len(args) > 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error The process-launch snapshot still treats unlisted shell builtins as external commands. For example, Suggested fix: Add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error The exec launch check treats any argument as a command, including options alone. ExportedNames("export X=1; exec --; export -n X") returns X, although Bash exec -- launches nothing and the later export -n removes X. Suggested fix: Skip exec options (at least --, and -a/-c/-l) before deciding whether a command remains, and add an options-only exec regression test. |
||
| for name := range exported { | ||
| seen[name] = true | ||
| } | ||
| } | ||
| } | ||
| switch cmd { | ||
| case "export": | ||
| // Options precede the operands and end at "--" or the first | ||
| // operand (a lone "-" is an operand); -n (alone or combined, as in | ||
| // -pn) switches the command to removing the export attribute. -f | ||
| // selects the function namespace, so function-mode operands never | ||
| // change a variable's export attribute. | ||
| unexport, funcMode, optsDone := false, false, false | ||
| for _, a := range args { | ||
| if strings.HasPrefix(a.text, "-") { | ||
| if !optsDone && a.text != "-" && strings.HasPrefix(a.text, "-") { | ||
| if a.text == "--" { | ||
| optsDone = true | ||
| } else { | ||
| // An option the builtin rejects makes the whole | ||
| // command a no-op, so no export state changes. | ||
| if strings.Trim(a.text[1:], "fnp") != "" { | ||
| return | ||
| } | ||
| if strings.Contains(a.text[1:], "n") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error Any export option containing 'n' sets un-export mode, even when Bash rejects the option as invalid. For example Suggested fix: Validate the option characters (only the letters export accepts, e.g. f, n, p) before changing export state, and add a regression test for an invalid option containing 'n'. |
||
| unexport = true | ||
| } | ||
| if strings.Contains(a.text[1:], "f") { | ||
| funcMode = true | ||
| } | ||
| } | ||
| continue | ||
| } | ||
| if name := a.assignmentName(); name != "" { | ||
| seen[name] = true | ||
| } else if envNameRe.MatchString(a.text) { | ||
| seen[a.text] = true | ||
| optsDone = true | ||
| name := a.assignmentName() | ||
| if name == "" && envNameRe.MatchString(a.text) { | ||
| name = a.text | ||
| } | ||
| switch { | ||
| case name == "": | ||
| case funcMode: | ||
| case unexport: | ||
| delete(exported, name) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error
Suggested fix: Before processing a non-builtin command launch, copy the currently exported names into |
||
| default: | ||
| exported[name] = true | ||
| } | ||
| } | ||
| case "env": | ||
|
|
@@ -119,17 +182,59 @@ func collectFromCommand(words []shellWord, seen map[string]bool, depth int) { | |
| } | ||
| name := a.assignmentName() | ||
| if name == "" { | ||
| collectFromCommand(args[j:], seen, depth) | ||
| // env launches an external program, which cannot run the | ||
| // export builtin, so the command gets an isolated set. | ||
| collectFromCommand(args[j:], seen, map[string]bool{}, depth) | ||
| return | ||
| } | ||
| seen[name] = true | ||
| } | ||
| case "exec", "command", "nohup": | ||
| collectFromCommand(args, seen, depth) | ||
| case "builtin": | ||
| // builtin runs only a shell builtin, never an external program, so | ||
| // it launches no process itself. A target outside the shell's | ||
| // own export/command/exec/builtin dispatchers has no effect on | ||
| // export attributes, and a non-builtin target is an error. | ||
| if len(args) > 0 && args[0].text == "--" { | ||
| args = args[1:] | ||
| } | ||
| // The dispatched command keeps this command's prefix assignments, | ||
| // so "Y=1 builtin exec prog" still hands Y to prog. | ||
| if len(args) > 0 { | ||
| switch args[0].text { | ||
| case "export", "command", "exec", "builtin": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] logic-error The builtin dispatcher drops prefix assignments even when its target launches an external process (e.g. Suggested fix: Propagate prefix assignments through builtin dispatch and record them when the resolved target launches a process; add tests for prefixed |
||
| target := append(slices.Clone(words[:i]), args...) | ||
| collectFromCommand(target, seen, exported, depth) | ||
| } | ||
| } | ||
| case "exec", "nohup": | ||
| collectFromCommand(args, seen, map[string]bool{}, depth) | ||
| case "command": | ||
| // Options end at "--" or the first non-option word. -v and -V | ||
| // only look the command up, and an option the builtin rejects | ||
| // runs nothing, so neither launches a process. | ||
| for j, a := range args { | ||
| if a.text == "--" { | ||
| collectFromCommand(args[j+1:], seen, exported, depth) | ||
| return | ||
| } | ||
| if !strings.HasPrefix(a.text, "-") || a.text == "-" { | ||
| collectFromCommand(args[j:], seen, exported, depth) | ||
| return | ||
| } | ||
| if strings.Trim(a.text[1:], "pvV") != "" || strings.ContainsAny(a.text[1:], "vV") { | ||
| return | ||
| } | ||
| } | ||
| case "sh", "bash", "/bin/sh", "/bin/bash": | ||
| for j, a := range args { | ||
| if strings.HasPrefix(a.text, "-") && strings.Contains(a.text, "c") && j+1 < len(args) { | ||
| collectExportedNames(args[j+1].text, seen, depth+1) | ||
| // The child shell has its own export attributes: its | ||
| // "export -n" cannot un-export a name in this shell. | ||
| child := map[string]bool{} | ||
| collectExportedNames(args[j+1].text, seen, child, depth+1) | ||
| for name := range child { | ||
| seen[name] = true | ||
| } | ||
| return | ||
| } | ||
| } | ||
|
|
||
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.
[low] edge-case
All tokenized commands share one mutable export set while the tokenizer treats parentheses as plain command separators, so
export X=1; (export -n X)returns no X although Bash runs the un-export in a subshell and X stays exported in the parent. This is consistent with the documented 'tokenizer, not a shell' limits; the pre-PR code had the same scope loss in the other direction ((export X=1)was over-reported).Suggested fix: Add a regression test and either isolate export mutations inside parenthesized subshells or document this as a known tokenizer limitation.