-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix(oauth2): prevent code injection in OAuth2 callback handling #8405
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
e853e68
430c6b4
492976e
b852a2d
8135cd8
8fc0800
aa527e0
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,122 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancelOAuth2AuthorizationRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isOauth2AuthorizationRequestInProgress | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } = require('../../src/utils/oauth2-protocol-handler'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('handleOauth2ProtocolUrl - state validation', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let resolve; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let reject; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve = jest.fn(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| reject = jest.fn(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Clear any pending request between tests | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isOauth2AuthorizationRequestInProgress()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancelOAuth2AuthorizationRequest(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| jest.clearAllMocks(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('authorization code flow (state in query params)', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should resolve with the code when the returned state matches', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl('bruno://oauth2/callback?code=auth-code-123&state=expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).toHaveBeenCalledWith('auth-code-123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should reject when the returned state does not match', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl('bruno://oauth2/callback?code=auth-code-123&state=attacker-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect.objectContaining({ message: expect.stringContaining('state mismatch') }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should reject when no state is returned but one was expected', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl('bruno://oauth2/callback?code=auth-code-123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect.objectContaining({ message: expect.stringContaining('state mismatch') }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('implicit flow (state in hash fragment)', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should resolve with tokens when the returned state matches', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'bruno://oauth2/callback#access_token=token-abc&token_type=bearer&state=expected-state' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect.objectContaining({ access_token: 'token-abc' }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should reject when the hash state does not match', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'bruno://oauth2/callback#access_token=token-abc&token_type=bearer&state=attacker-state' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect.objectContaining({ message: expect.stringContaining('state mismatch') }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should reject when no hash state is returned but one was expected', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'bruno://oauth2/callback#access_token=token-abc&token_type=bearer' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect.objectContaining({ message: expect.stringContaining('state mismatch') }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('when no expected state was registered (backward compatibility)', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should resolve without validating state', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl('bruno://oauth2/callback?code=auth-code-123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).toHaveBeenCalledWith('auth-code-123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('error responses are handled before state validation', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should reject with the provider error even if state is absent', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOauth2ProtocolUrl('bruno://oauth2/callback?error=access_denied'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(resolve).not.toHaveBeenCalled(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(reject).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect.objectContaining({ message: expect.stringContaining('Authorization Failed') }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+110
to
+121
Contributor
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Add the hash-fragment provider-error case. This only proves precedence for Suggested test describe('error responses are handled before state validation', () => {
it('should reject with the provider error even if state is absent', () => {
registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state');
handleOauth2ProtocolUrl('bruno://oauth2/callback?error=access_denied');
expect(resolve).not.toHaveBeenCalled();
expect(reject).toHaveBeenCalledWith(
expect.objectContaining({ message: expect.stringContaining('Authorization Failed') })
);
});
+
+ it('should reject with the provider error from the hash fragment before state validation', () => {
+ registerOauth2AuthorizationRequest(resolve, reject, null, 'expected-state');
+
+ handleOauth2ProtocolUrl('bruno://oauth2/callback#error=access_denied');
+
+ expect(resolve).not.toHaveBeenCalled();
+ expect(reject).toHaveBeenCalledWith(
+ expect.objectContaining({ message: expect.stringContaining('Authorization Failed') })
+ );
+ });
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Normalize
formatIpcError()before sending it to the UI.formatIpcError()returns the original value for non-Errorinputs, so a truthy object payload here skips the fallback and gets pushed into bothtoast.error(...)andresponse.error. That will surface a useless[object Object]-style message instead of a readable OAuth error.Suggested fix
Also applies to: 133-135
🤖 Prompt for AI Agents