Skip to content

feat: Implement isSafeMode() API - #5760

Merged
sid-bruno merged 2 commits into
usebruno:feature/bru-safemode-5760from
datag:feature/bru-safemode
Jan 5, 2026
Merged

sid-bruno merged 2 commits into
usebruno:feature/bru-safemode-5760from
datag:feature/bru-safemode

Conversation

@datag

@datag datag commented Oct 10, 2025 •

Copy link
Copy Markdown
Contributor

Add isSafeMode() to Bru API that returns true in case the runtime is a sandbox.

This allows for scripts to test for and handle whether running in sandbox or not:

if (bru.isSafeMode()) {
  throw new Error('This script requires Developer mode')
}

Resolves #4238, JIRA

Summary by CodeRabbit

  • New Features
    • Added bru.isSafeMode() API method that returns a boolean indicating whether the script is executing in safe mode, with autocomplete support.

✏️ Tip: You can customize this high-level summary in your review settings.

Add `isSafeMode()` to Bru API that returns `true` in
case the runtime is a sandbox.

This allows for scripts to test for and handle whether
running in sandbox or not:

```javascript
if (bru.isSafeMode()) {
  throw new Error('This script requires Developer mode')
}
```
@helloanoop

Copy link
Copy Markdown
Contributor

Thanks for the PR @datag !
We'll review this in the next couple of weeks.

@coderabbitai

coderabbitai Bot commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request introduces a bru.isSafeMode() API method that allows scripts to query whether Bruno is running in safe mode. The implementation adds a runtime parameter to the Bru class constructor and updates all instantiation points across runtime handlers and tests to pass the runtime identifier.

Changes

Cohort / File(s) Summary
Bru class core
packages/bruno-js/src/bru.js
Added runtime as first constructor parameter and new public method isSafeMode() returning true when runtime equals 'quickjs'
Runtime instantiation updates
packages/bruno-js/src/runtime/assert-runtime.js, packages/bruno-js/src/runtime/script-runtime.js, packages/bruno-js/src/runtime/test-runtime.js, packages/bruno-js/src/runtime/vars-runtime.js
Updated all Bru constructor calls to pass this.runtime as the first argument, shifting existing parameters
QuickJS sandbox shim
packages/bruno-js/src/sandbox/quickjs/shims/bru.js
Exposed new isSafeMode() method as a VM-facing shim function
Autocomplete hints
packages/bruno-app/src/utils/codemirror/autocomplete.js
Added bru.isSafeMode() to STATIC_API_HINTS for editor autocomplete
Tests
packages/bruno-js/tests/setEnvVar.spec.js
Updated Bru constructor invocation in test setup to include runtime parameter

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify all Bru instantiation sites have been consistently updated with the runtime parameter across runtime handlers
  • Confirm the isSafeMode() implementation correctly maps the runtime value and is properly exposed through both direct class usage and the QuickJS shim
  • Check that the parameter order consistency is maintained across all call sites

Suggested labels

size/L

Suggested reviewers

  • lohit-bruno
  • naman-bruno
  • bijin-bruno

Poem

🔒 Safe mode queries bloom today,
🧭 A runtime check to light the way,
✨ Scripts now ask before they play,
💫 "Is it safe?" — hooray, hooray! 🎉

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately describes the main feature addition of implementing the isSafeMode() API across the codebase.
Linked Issues check ✅ Passed PR implements the isSafeMode() API method [#4238] enabling scripts to detect Safe Mode, matching the exact usage example and objective from the linked issue.
Out of Scope Changes check ✅ Passed All changes are focused on implementing isSafeMode() API: adding the method to Bru class, updating all constructor call sites with runtime parameter, and exposing it in the QuickJS shim and autocomplete hints. No unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

@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: 0

Caution

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

⚠️ Outside diff range comments (1)
packages/bruno-js/src/bru.js (1)

9-21: Critical: runtime parameter isn't stored in the constructor, so isSafeMode() will always return false.

Add this.runtime = runtime; as the first line in the constructor. The method currently compares undefined === 'quickjs', which is never true. Given that the codebase supports both 'quickjs' (safe/sandboxed) and 'nodevm' (non-safe) runtimes, this bug completely breaks runtime-aware safe-mode detection.

 class Bru {
   constructor(runtime, envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName, promptVariables) {
+    this.runtime = runtime;
     this.envVariables = envVariables || {};
     this.runtimeVariables = runtimeVariables || {};
     this.promptVariables = promptVariables || {};
     this.processEnvVars = cloneDeep(processEnvVars || {});
     this.collectionVariables = collectionVariables || {};
     this.folderVariables = folderVariables || {};
     this.requestVariables = requestVariables || {};
     this.globalEnvironmentVariables = globalEnvironmentVariables || {};
     this.oauth2CredentialVariables = oauth2CredentialVariables || {};
     this.collectionPath = collectionPath;
     this.collectionName = collectionName;
🧹 Nitpick comments (3)
packages/bruno-js/tests/setEnvVar.spec.js (1)

4-21: Constructor update is correct; consider a runtime constant to avoid typos.

 describe('Bru.setEnvVar', () => {
+  const RUNTIME = 'quickjs';
   const makeBru = () =>
     new Bru(
-      /* runtime */ 'quickjs',
+      /* runtime */ RUNTIME,
       /* envVariables */ {},
       /* runtimeVariables */ {},
       /* processEnvVars */ {},
       /* collectionPath */ '/',
       /* historyLogger */ undefined,
       /* setVisualizations */ undefined,
       /* secretVariables */ {},
       /* collectionVariables */ {},
       /* folderVariables */ {},
       /* requestVariables */ {},
       /* globalEnvironmentVariables */ {},
       /* oauth2CredentialVariables */ {},
       /* iterationDetails */ {},
       /* collectionName */ 'Test'
     );
packages/bruno-js/src/runtime/vars-runtime.js (1)

38-41: Runtime arg pass-through is correct; consider formatting this constructor call like other runtimes.

-    const bru = new Bru(this.runtime, envVariables, runtimeVariables, processEnvVars, undefined, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, undefined, promptVariables);
+    const bru = new Bru(
+      this.runtime,
+      envVariables,
+      runtimeVariables,
+      processEnvVars,
+      undefined,
+      collectionVariables,
+      folderVariables,
+      requestVariables,
+      globalEnvironmentVariables,
+      oauth2CredentialVariables,
+      undefined,
+      promptVariables
+    );
packages/bruno-js/src/runtime/script-runtime.js (1)

35-37: Both Bru instantiation updates look consistent; +1 to the existing “single options object” TODO. Adding runtime as another positional arg makes the call even more fragile—refactoring to an options object would reduce future churn.

Also applies to: 130-132

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6652cca and 134df9b.

📒 Files selected for processing (8)
  • packages/bruno-app/src/utils/codemirror/autocomplete.js (1 hunks)
  • packages/bruno-js/src/bru.js (2 hunks)
  • packages/bruno-js/src/runtime/assert-runtime.js (1 hunks)
  • packages/bruno-js/src/runtime/script-runtime.js (2 hunks)
  • packages/bruno-js/src/runtime/test-runtime.js (1 hunks)
  • packages/bruno-js/src/runtime/vars-runtime.js (1 hunks)
  • packages/bruno-js/src/sandbox/quickjs/shims/bru.js (1 hunks)
  • packages/bruno-js/tests/setEnvVar.spec.js (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CODING_STANDARDS.md)

**/*.{js,jsx,ts,tsx}: Use 2 spaces for indentation. No tabs, just spaces
Stick to single quotes for strings. For JSX/TSX attributes, use double quotes (e.g., )
Always add semicolons at the end of statements
No trailing commas
Always use parentheses around parameters in arrow functions, even for single params
For multiline constructs, put opening braces on the same line, and ensure consistency. Minimum 2 elements for multiline
No newlines inside function parentheses
Space before and after the arrow in arrow functions. () => {} is good
No space between function name and parentheses. func() not func ()
Semicolons go at the end of the line, not on a new line
Names for functions need to be concise and descriptive
Add in JSDoc comments to add more details to the abstractions if needed
Add in meaningful comments instead of obvious ones where complex code flow is explained properly

Files:

  • packages/bruno-app/src/utils/codemirror/autocomplete.js
  • packages/bruno-js/src/runtime/script-runtime.js
  • packages/bruno-js/src/runtime/test-runtime.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bru.js
  • packages/bruno-js/src/runtime/vars-runtime.js
  • packages/bruno-js/src/bru.js
  • packages/bruno-js/src/runtime/assert-runtime.js
  • packages/bruno-js/tests/setEnvVar.spec.js
🧠 Learnings (1)
📚 Learning: 2025-12-03T06:32:11.738Z
Learnt from: sanish-bruno
Repo: usebruno/bruno PR: 6090
File: packages/bruno-js/src/runtime/hooks-runtime.js:8-135
Timestamp: 2025-12-03T06:32:11.738Z
Learning: vm2 runtime is being removed from the Bruno application and QuickJS will become the default runtime for script execution.

Applied to files:

  • packages/bruno-js/src/runtime/script-runtime.js
  • packages/bruno-js/src/runtime/test-runtime.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bru.js
  • packages/bruno-js/src/runtime/vars-runtime.js
  • packages/bruno-js/src/bru.js
  • packages/bruno-js/tests/setEnvVar.spec.js
🧬 Code graph analysis (3)
packages/bruno-js/src/runtime/script-runtime.js (5)
packages/bruno-js/src/runtime/assert-runtime.js (1)
  • Bru (4-4)
packages/bruno-js/src/runtime/test-runtime.js (1)
  • Bru (2-2)
packages/bruno-js/src/runtime/vars-runtime.js (1)
  • Bru (2-2)
packages/bruno-js/tests/runtime.spec.js (1)
  • Bru (4-4)
packages/bruno-js/tests/setEnvVar.spec.js (1)
  • Bru (1-1)
packages/bruno-js/src/runtime/test-runtime.js (6)
packages/bruno-js/src/runtime/assert-runtime.js (1)
  • Bru (4-4)
packages/bruno-js/src/runtime/script-runtime.js (1)
  • Bru (2-2)
packages/bruno-js/src/runtime/vars-runtime.js (1)
  • Bru (2-2)
packages/bruno-js/tests/runtime.spec.js (1)
  • Bru (4-4)
packages/bruno-cli/src/commands/run.js (2)
  • processEnvVars (466-468)
  • collectionPath (295-295)
packages/bruno-electron/src/ipc/network/prepare-grpc-request.js (1)
  • processEnvVars (88-88)
packages/bruno-js/src/sandbox/quickjs/shims/bru.js (1)
packages/bruno-js/src/sandbox/quickjs/utils/index.js (1)
  • marshallToVm (1-31)
🔇 Additional comments (4)
packages/bruno-app/src/utils/codemirror/autocomplete.js (1)

55-100: API hint addition looks good. The new 'bru.isSafeMode()' entry is consistent with existing hint strings and coding style.

packages/bruno-js/src/sandbox/quickjs/shims/bru.js (1)

26-31: QuickJS shim wiring for bru.isSafeMode() looks correct. Wrapper/marshalling and disposal are consistent with the rest of this file.

packages/bruno-js/src/runtime/assert-runtime.js (1)

242-272: Call-site update is correct; will be effective once Bru stores runtime. This aligns with the new constructor signature, but bru.isSafeMode() will be broken until this.runtime is assigned in packages/bruno-js/src/bru.js.

packages/bruno-js/src/runtime/test-runtime.js (1)

33-37: Call-site update to pass this.runtime is consistent with the new Bru API. (Will work as intended once Bru assigns this.runtime.)

@helloanoop

Copy link
Copy Markdown
Contributor

I fixed the PR conflicts. Changes look fine to me.
We have allotted time for the team to look at this and get it merged in Q1 26

class Bru {
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName, promptVariables) {
constructor(runtime, envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName, promptVariables) {
this.envVariables = envVariables || {};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

runtime parameter is received but never assigned to this.runtime, so isSafeMode() always returns false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@pooja-bruno In my original commit 96655e6 it has been assigned. I guess it was lost on rebase conflict resolving.

Unfortunately I won't find the time in the next weeks to do any changes on this PR.

@pooja-bruno

Copy link
Copy Markdown
Collaborator

@datag, Thanks for raising this PR! One small issue runtime is passed to the constructor but never assigned to this.runtime, so isSafeMode() always returns false. Could you fix that? Also, if possible, could you add a test for isSafeMode() to verify it returns the correct value?

@sid-bruno sid-bruno added the adopted-by-maintainers PR needs additional work and is now being worked on by the maintainers label Jan 5, 2026
@sid-bruno
sid-bruno changed the base branch from main to feature/bru-safemode-5760 January 5, 2026 11:03
@sid-bruno

Copy link
Copy Markdown
Collaborator

Merging this in for it to be refined and polished by the internal team, Thanks @datag for the contribution.

@sid-bruno
sid-bruno merged commit 4e526e4 into usebruno:feature/bru-safemode-5760 Jan 5, 2026
sid-bruno pushed a commit that referenced this pull request Jan 19, 2026
Add `isSafeMode()` to Bru API that returns `true` in
case the runtime is a sandbox.

This allows for scripts to test for and handle whether
running in sandbox or not:

```javascript
if (bru.isSafeMode()) {
  throw new Error('This script requires Developer mode')
}
```

Co-authored-by: Anoop M D <[email protected]>
sid-bruno added a commit that referenced this pull request Jan 19, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Jan 22, 2026
6 tasks
FraCata00 pushed a commit to FraCata00/bruno that referenced this pull request Feb 9, 2026
Add `isSafeMode()` to Bru API that returns `true` in
case the runtime is a sandbox.

This allows for scripts to test for and handle whether
running in sandbox or not:

```javascript
if (bru.isSafeMode()) {
  throw new Error('This script requires Developer mode')
}
```

Co-authored-by: Anoop M D <[email protected]>
maxenceleblanc pushed a commit to maxenceleblanc/curly_cats2 that referenced this pull request Jul 20, 2026
Add `isSafeMode()` to Bru API that returns `true` in
case the runtime is a sandbox.

This allows for scripts to test for and handle whether
running in sandbox or not:

```javascript
if (bru.isSafeMode()) {
  throw new Error('This script requires Developer mode')
}
```

Co-authored-by: Anoop M D <[email protected]>
maxenceleblanc pushed a commit to maxenceleblanc/curly_cats2 that referenced this pull request Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adopted-by-maintainers PR needs additional work and is now being worked on by the maintainers size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query Safe/Developer Mode from scripts

4 participants