Repository navigation
fix(interpreter): isolate RANDOM state in child contexts - #1949
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a state-leak in the interpreter’s $RANDOM implementation by ensuring the per-instance PRNG state (random_state) is properly isolated across child execution contexts (subshells/substitutions and path-based script execution), and by enforcing readonly RANDOM before allowing reseeds.
Changes:
- Add
random_stateto subshell snapshots and restore it after command/arithmetic substitutions and subshell execution. - Save/restore
random_statearound path-based script execution so child scripts can’t mutate the parent’s PRNG stream. - Move
RANDOM=Nreseed handling to after readonly checks and add regression tests for isolation + readonly protection.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
bashkit | c3c1526 | Commit Preview URL | Jun 05 2026, 08:10 PM |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
$RANDOMLCG state (random_state) was not saved/restored, allowing child code to advance or reseed the parent PRNG.RANDOM=Nreseeding executed before readonly enforcement, soreadonly RANDOMdid not protect the PRNG state.Description
random_state: u32toSubshellSnapshotand include it insnapshot_subshell_state()andrestore_subshell_state()so command/arith substitutions and subshells no longer leak PRNG mutations.random_statearound path-based script (bash -c/ file-exec) execution so path-subprocess isolation preserves parent PRNG state.RANDOM=Nreseed handling to after readonly attribute checks inset_variable()soreadonly RANDOMblocks reseeding, and add focused regression tests covering subshells, command substitution, arithmetic substitution,bash -c, path script isolation, and readonly protection forRANDOM.Testing
cargo fmt --checkwhich succeeded.interpreter::tests::test_random_state_isolated_from_subshell_contexts,interpreter::tests::test_random_state_isolated_from_path_script, andinterpreter::tests::test_readonly_random_blocks_reseedand all passed.cargo test -p bashkit random_state --libandcargo test -p bashkit test_readonly_random_blocks_reseed --lib(and the combined test/check sequence used in verification) and they all passed;git diff --checkalso reported no issues.Codex Task