Skip to content

fix(app): prevent runtime variables from leaking across environments - #8830

Closed
Max-Reisinger wants to merge 3 commits into
usebruno:mainfrom
Max-Reisinger:bugfix/runtime-variable-environment-isolation
Closed

Max-Reisinger wants to merge 3 commits into
usebruno:mainfrom
Max-Reisinger:bugfix/runtime-variable-environment-isolation

Conversation

@Max-Reisinger

@Max-Reisinger Max-Reisinger commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Clear collection runtime variables when the active collection environment actually changes.

This prevents values created with bru.setVar() in one environment from overriding variables in another environment.

Problem

Fixes #8825.

Runtime variables have the highest variable precedence. A runtime variable created while environment A was active remained in collection.runtimeVariables after switching to environment B, causing it to override B's environment variable with the same name.

Fix

  • Track the previously active collection environment in the selectEnvironment reducer.
  • Clear collection.runtimeVariables only when the selected environment UID actually changes.
  • Preserve runtime variables when the active environment is selected again.
  • Clear runtime variables when switching to "No Environment".
  • Add reducer and Playwright regression coverage for environment switching and variable interpolation.

This implementation treats changing the active collection environment as ending the previous runtime-variable context. The current documentation describes runtime variables as collection-scoped, so this behavior is called out for maintainer confirmation.

Testing

  • npm test --workspace=packages/bruno-app -- --runInBand src/providers/ReduxStore/slices/collections/index.spec.js
  • npx playwright test tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts --project=default --grep "Runtime variables do not leak"
  • npm run lint -- packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts
  • npm run build:web

The targeted lint command completed with zero errors. It reports pre-existing warnings in the collections reducer outside the changed lines.

Screenshots

Not applicable. The behavior is covered by the Playwright regression test.

Before After
Runtime variables from environment A override environment B variables. Switching to environment B clears the previous runtime variables.

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.
  • I've run the claude code review skill locally.

Summary by CodeRabbit

Bug Fixes

  • Runtime variables are now cleared when switching collection environments or clearing the active environment.
  • Prevented stale environment updates from modifying runtime variables.
  • Ensured variables from one environment do not persist or affect requests in another environment.

Tests

  • Added coverage for environment switching, deselection, and runtime variable isolation.
  • Added interpolation scenarios for reading variables from multiple environments.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9ca2b751-6cd5-4b73-beac-858012500779

📥 Commits

Reviewing files that changed from the base of the PR and between b019fff and abd8d74.

📒 Files selected for processing (2)
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js

Walkthrough

setActiveEnvironment now centralizes active-environment updates and clears runtime variables when the normalized environment UID changes. Runtime-variable events apply only to the active environment. Reducer and interpolation tests verify environment isolation.

Changes

Environment Runtime Variable Isolation

Layer / File(s) Summary
Runtime update environment contract
packages/bruno-electron/src/ipc/network/index.js, packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js, packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js
IPC events now include environmentUid. The reducer applies updates only when the UID matches the active environment.
Reducer environment reset behavior
packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js, packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js
Environment selection, default activation, and environment loading use setActiveEnvironment. The helper clears runtime variables when the active UID changes.
Interpolation isolation validation
tests/interpolation/dynamic-variable/collection/environments/*, tests/interpolation/dynamic-variable/collection/read-environment-variable.bru, tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts
Fixtures and serial browser coverage verify that values set in environment A do not persist in environment B.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • usebruno/bruno#8612: Updates shared collection environment selection and runtime-variable handling.
  • usebruno/bruno#8784: Modifies environment-scoped variable state during environment switching.

Suggested reviewers: sid-bruno

Poem

Environment A sets a value,
Environment B starts anew.
Matching UIDs keep state aligned,
Stale updates stay behind.
Switch, send, and verify.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the primary fix: preventing runtime variables from leaking across environments.
Linked Issues check ✅ Passed The changes isolate runtime variables across environment transitions and add regression coverage for issue #8825.
Out of Scope Changes check ✅ Passed The reducer changes, IPC update, fixtures, and tests directly support environment runtime-variable isolation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts (1)

47-49: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use a page-module response locator.

page.locator('.response-pane') adds a raw selector to the spec. Expose the response pane through tests/utils/page/locators and consume that helper here.

As per coding guidelines and path instructions, specs must centralize locators/actions in tests/utils/page/* and avoid inline raw selectors.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts` around
lines 47 - 49, Replace the inline page.locator('.response-pane') usage in the
Runtime variables do not leak across collection environments test with the
response-pane locator exposed by tests/utils/page/locators, and use that
page-module helper through the existing page fixture.

Sources: Coding guidelines, Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js`:
- Around line 421-425: Update runtimeVariablesUpdateEvent and its dispatchers to
include the originating environment or scope with each payload, then ignore
updates whose origin no longer matches the collection’s active environment after
selectEnvironment clears runtimeVariables. Preserve valid updates for the
current environment and add a regression test covering an update arriving after
an environment switch.

---

Nitpick comments:
In `@tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts`:
- Around line 47-49: Replace the inline page.locator('.response-pane') usage in
the Runtime variables do not leak across collection environments test with the
response-pane locator exposed by tests/utils/page/locators, and use that
page-module helper through the existing page fixture.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b4a2bd30-f99b-412b-bfab-8d56bc06b654

📥 Commits

Reviewing files that changed from the base of the PR and between 97c9413 and 4900fb5.

📒 Files selected for processing (6)
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js
  • tests/interpolation/dynamic-variable/collection/environments/A.bru
  • tests/interpolation/dynamic-variable/collection/environments/B.bru
  • tests/interpolation/dynamic-variable/collection/read-environment-variable.bru
  • tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js (1)

421-425: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Reset runtime variables on every environment transition.

selectEnvironment clears runtimeVariables only for transitions handled by this reducer. The same file assigns activeEnvironmentUid directly in applyDefaultEnvironment and collectionAddEnvFileEvent. Those paths can activate an environment without clearing variables from the previous selection.

Move the transition comparison into a shared helper, or apply the same reset at every direct activeEnvironmentUid assignment. The direct assignments at Lines 442 and 3146 support this finding.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js`
around lines 421 - 425, Ensure runtimeVariables is reset whenever
activeEnvironmentUid changes, not only in selectEnvironment. Reuse a shared
transition helper or add equivalent previous-versus-new environment checks to
applyDefaultEnvironment and collectionAddEnvFileEvent, while preserving the
existing null normalization and avoiding resets when the environment remains
unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js`:
- Around line 421-425: Ensure runtimeVariables is reset whenever
activeEnvironmentUid changes, not only in selectEnvironment. Reuse a shared
transition helper or add equivalent previous-versus-new environment checks to
applyDefaultEnvironment and collectionAddEnvFileEvent, while preserving the
existing null normalization and avoiding resets when the environment remains
unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cffe13f6-47a8-44dd-b49b-da7ab8d18f0e

📥 Commits

Reviewing files that changed from the base of the PR and between 4900fb5 and b019fff.

📒 Files selected for processing (4)
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js
  • packages/bruno-electron/src/ipc/network/index.js
  • tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts
  • packages/bruno-app/src/providers/ReduxStore/slices/collections/index.spec.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bru.setVar sets ephemeral variables across environments

2 participants