-
Notifications
You must be signed in to change notification settings - Fork 21
fix(privileged-context): Reject statements with helpful error message (fixes bug 2027926) #61
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
dmose
wants to merge
1
commit into
mozilla:main
Choose a base branch
from
dmose:fix-statement-fails
base: main
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.
+147
−1
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * Tests for statement detection and rejection in privileged context tools | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { | ||
| evaluatePrivilegedScriptTool, | ||
| handleEvaluatePrivilegedScript, | ||
| isLikelyStatement, | ||
| } from '../../src/tools/privileged-context.js'; | ||
|
|
||
| // Mock the index module (used by handler tests) | ||
| const mockGetFirefox = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock('../../src/index.js', () => ({ | ||
| getFirefox: () => mockGetFirefox(), | ||
| })); | ||
|
Collaborator
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. Can we just move that to |
||
|
|
||
| describe('Privileged Context Tool Definitions', () => { | ||
| describe('evaluatePrivilegedScriptTool', () => { | ||
| it('should have correct name', () => { | ||
| expect(evaluatePrivilegedScriptTool.name).toBe('evaluate_privileged_script'); | ||
| }); | ||
|
|
||
| it('should mention expression in description', () => { | ||
| expect(evaluatePrivilegedScriptTool.description).toContain('expression'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isLikelyStatement', () => { | ||
| it('should detect const declarations', () => { | ||
| expect(isLikelyStatement('const x = 1')).toBe(true); | ||
| }); | ||
|
|
||
| it('should detect let declarations', () => { | ||
| expect(isLikelyStatement('let x = 1')).toBe(true); | ||
| }); | ||
|
|
||
| it('should detect var declarations', () => { | ||
| expect(isLikelyStatement('var x = 1')).toBe(true); | ||
| }); | ||
|
|
||
| it('should allow function calls', () => { | ||
| expect(isLikelyStatement('Services.prefs.getBoolPref("foo")')).toBe(false); | ||
| }); | ||
|
|
||
| it('should allow simple expressions', () => { | ||
| expect(isLikelyStatement('1 + 2')).toBe(false); | ||
| }); | ||
|
|
||
| it('should allow property access', () => { | ||
| expect(isLikelyStatement('document.title')).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle leading whitespace', () => { | ||
| expect(isLikelyStatement(' const x = 1')).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Privileged Context Tool Handlers', () => { | ||
| const mockExecuteScript = vi.fn(); | ||
| const mockSetContext = vi.fn(); | ||
| const mockSwitchToWindow = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('handleEvaluatePrivilegedScript', () => { | ||
| it('should reject const statements with error', async () => { | ||
| const result = await handleEvaluatePrivilegedScript({ expression: 'const x = 1' }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| }); | ||
|
|
||
| it('should mention "statement" in error message', async () => { | ||
| const result = await handleEvaluatePrivilegedScript({ expression: 'const x = 1' }); | ||
|
|
||
| expect(result.content[0]).toHaveProperty('text', expect.stringMatching(/statement/i)); | ||
| }); | ||
|
|
||
| it('should suggest IIFE workaround in error message', async () => { | ||
| const result = await handleEvaluatePrivilegedScript({ expression: 'const x = 1' }); | ||
|
|
||
| expect(result.content[0].text).toContain('function()'); | ||
| }); | ||
|
|
||
| it('should return error when expression parameter is missing', async () => { | ||
| const result = await handleEvaluatePrivilegedScript({}); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toContain('expression parameter is required'); | ||
| }); | ||
|
|
||
| it('should execute valid expressions successfully', async () => { | ||
| const mockFirefox = { | ||
| getDriver: vi.fn().mockReturnValue({ | ||
| switchTo: () => ({ window: mockSwitchToWindow }), | ||
| setContext: mockSetContext, | ||
| executeScript: mockExecuteScript.mockResolvedValue('test-result'), | ||
| }), | ||
| }; | ||
|
|
||
| mockGetFirefox.mockResolvedValue(mockFirefox); | ||
|
|
||
| const result = await handleEvaluatePrivilegedScript({ expression: 'document.title' }); | ||
|
|
||
| expect(result.isError).toBeUndefined(); | ||
| expect(result.content[0].text).toContain('test-result'); | ||
| }); | ||
|
|
||
| it('should reject let statements', async () => { | ||
| const result = await handleEvaluatePrivilegedScript({ expression: 'let y = 2' }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0]).toHaveProperty('text', expect.stringMatching(/statement/i)); | ||
| }); | ||
|
|
||
| it('should reject var statements', async () => { | ||
| const result = await handleEvaluatePrivilegedScript({ expression: 'var z = 3' }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0]).toHaveProperty('text', expect.stringMatching(/statement/i)); | ||
| }); | ||
| }); | ||
| }); | ||
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.
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.
The suggestion was to move the existing tests to privileged-context-state.test.ts (since they are not really testing a tool, but rather the side effect of using a tool on the privileged context "state")