Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2025-05-15 - [Secure postMessage origins in OAuth2 flow]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟑 Minor

Date inconsistency in the documentation header.

The date "2025-05-15" appears to predate the PR creation timestamp (2026-03-08). This could cause confusion when reviewing the security timeline. Consider using the actual date when the vulnerability was fixed or documented.

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.jules/sentinel.md at line 1, Update the documentation header line "##
2025-05-15 - [Secure postMessage origins in OAuth2 flow]" to the correct,
current documentation/fix date (e.g., 2026-03-08 or the actual fix date) so the
timeline matches the PR; locate that header in .jules/sentinel.md and replace
the old date string with the accurate date.

**Vulnerability:** Use of `*` wildcard as target origin in `window.postMessage` during OAuth2 redirection, which could leak authorization codes to malicious windows.
**Learning:** The codebase has multiple places (frontend and backend) where `postMessage` is used to communicate the OAuth code back to the opener.
**Prevention:** Use `window.location.origin` on the client and `domainHelper.getPublicUrl` on the server to resolve the trusted frontend origin. Always extract only the origin (scheme + host + port) using `new URL(url).origin` to ensure strict matching by the browser.
2 changes: 1 addition & 1 deletion packages/react-ui/src/app/routes/redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const RedirectPage: React.FC = React.memo(() => {
{
code: code,
},
'*',
window.location.origin,
);
}
}, [location.search]);
Expand Down
3 changes: 2 additions & 1 deletion packages/react-ui/src/lib/oauth2-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ function constructUrl(params: OAuth2PopupParams, pckeChallenge: string) {
function getCode(redirectUrl: string): Promise<string> {
return new Promise<string>((resolve) => {
window.addEventListener('message', function handler(event) {
const expectedOrigin = new URL(redirectUrl).origin;
if (
redirectUrl &&
redirectUrl.startsWith(event.origin) &&
event.origin === expectedOrigin &&
event.data['code']
) {
resolve(decodeURIComponent(event.data.code));
Expand Down
4 changes: 3 additions & 1 deletion packages/server/api/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,14 @@ export const setupApp = async (app: FastifyInstance): Promise<FastifyInstance> =
return reply.send('The code is missing in url')
}
else {
const frontendUrl = await domainHelper.getPublicUrl({ platformId: null })
const frontendOrigin = new URL(frontendUrl).origin
Comment on lines +253 to +254
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Resolve postMessage target from current redirect host

Using domainHelper.getPublicUrl({ platformId: null }) hard-codes the postMessage target to the global FRONTEND_URL, which breaks tenant custom-domain flows. In enterprise/cloud, the redirect URL is generated per platform (see enterprise-flags.hooks.ts), so /redirect can run on tenant.example.com while this script targets the default origin; when targetOrigin does not match the opener’s origin, browsers drop the message and OAuth login/connection popups never deliver the code for those tenants.

Useful? React with πŸ‘Β / πŸ‘Ž.

return reply
.type('text/html')
.send(
`<script>if(window.opener){window.opener.postMessage({ 'code': '${encodeURIComponent(
params.code,
)}' },'*')}</script> <html>Redirect succuesfully, this window should close now</html>`,
)}' }, '${frontendOrigin}')}</script> <html>Redirect succuesfully, this window should close now</html>`,
Comment on lines +253 to +260
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check how domainHelper.getPublicUrl handles platformId and multi-tenant scenarios

# Find the implementation of getPublicUrl
ast-grep --pattern 'getPublicUrl($$$) {
  $$$
}'

# Check how other redirect endpoints handle platformId
rg -n "getPublicUrl|getInternalUrl" --type ts -C 3 | head -80

# See if there are any existing multi-platform redirect handling patterns
rg -n "platformId.*redirect|redirect.*platformId" --type ts -C 2

Repository: AGI-Corporation/Route.X

Length of output: 8543


🏁 Script executed:

# Get the full context of the endpoint where platformId: null is used
sed -n '240,270p' packages/server/api/src/app/app.ts

# Search for the endpoint definition and see if there's any state parameter handling
rg -n "platformId.*null|params\.code|oauth.*redirect" packages/server/api/src/app/app.ts -B 5 -A 2

Repository: AGI-Corporation/Route.X

Length of output: 2018


🏁 Script executed:

# Check if OAuth implementations use state parameter to track platform context
rg -n "state.*platform|platform.*state|oauth.*state" packages/server/api/src/app/ee/authentication --type ts -i | head -40

# Search for OAuth redirect implementations and see if any track platform
rg -n "getThirdPartyRedirectUrl|oauthRedirectUrl|redirect_uri" packages/server/api/src/app/ee/authentication --type ts -C 3 | head -100

# Check if there's any platform tracking mechanism in OAuth flows
rg -n "window.opener.postMessage|state.*=|platformId" packages/server/api/src/app --type ts -B 2 -A 2

Repository: AGI-Corporation/Route.X

Length of output: 50380


🏁 Script executed:

# Search for how OAuth flows pass context through the OAuth flow
rg -n "state|oauth.*code|code.*oauth" packages/server/api/src/app/ee/authentication --type ts -C 2 | grep -E "(state|oauth)" | head -50

# Check the frontend to see how it handles the postMessage
fd "redirect" packages/server -t ts | head -5

# Search for comments or patterns around platformId: null
rg -n "platformId.*null" packages/server/api/src/app/app.ts -B 5 -A 5

# Check if there's a state parameter or similar mechanism in OAuth flow
rg -n "searchParams|query|state" packages/server/api/src/app/ee/authentication/federated-authn --type ts | head -30

Repository: AGI-Corporation/Route.X

Length of output: 1927


🏁 Script executed:

# Check Google OAuth implementation for state parameter
sed -n '1,100p' packages/server/api/src/app/ee/authentication/federated-authn/google-authn-provider.ts

# Check if state is used anywhere in federated auth
rg -n "state" packages/server/api/src/app/ee/authentication/federated-authn/ --type ts -C 2

# Look at the entire app.ts redirect endpoint context
sed -n '240,270p' packages/server/api/src/app/app.ts

Repository: AGI-Corporation/Route.X

Length of output: 4295


🏁 Script executed:

# Get the full implementation of getThirdPartyRedirectUrl
sed -n '50,60p' packages/server/api/src/app/ee/authentication/federated-authn/federated-authn-service.ts

# Check if there are other redirect endpoints that handle platformId
rg -n "app.get.*redirect|app.post.*redirect" packages/server/api/src/app -C 2 | head -50

Repository: AGI-Corporation/Route.X

Length of output: 296


🏁 Script executed:

# Check if there's a way to extract platformId from request hostname
rg -n "request.hostname|request.host|req.headers.host" packages/server/api/src/app -C 2 | head -40

# Check if customDomainService can lookup platformId by hostname
sed -n '1,50p' packages/server/api/src/app/ee/custom-domains/custom-domain.service.ts

Repository: AGI-Corporation/Route.X

Length of output: 2286


Extract platformId from request hostname instead of hardcoding null.

The endpoint receives the OAuth redirect at a platform-specific URL (built by getThirdPartyRedirectUrl(platformId)), but then discards that platform context by using platformId: null. This causes the postMessage to target the default platform's origin instead of the correct tenant's origin in multi-platform deployments with custom domains.

Use getPlatformIdForHostname() (available in platform.utils) to extract the platformId from request.headers.host, matching the pattern already used elsewhere in the codebase. If platformId cannot be determined from the hostname, fall back to the default.

Minor: Fix typo "succuesfully" β†’ "successfully".

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/server/api/src/app/app.ts` around lines 253 - 260, Replace the
hardcoded null platformId passed to domainHelper.getPublicUrl by extracting
platformId from the request hostname using
getPlatformIdForHostname(request.headers.host) and pass that value (falling back
to default if it returns falsy) into getPublicUrl; update the postMessage HTML
string to fix the "succuesfully" typo to "successfully" and keep using
encodeURIComponent(params.code) and frontendOrigin as before so the OAuth code
is posted to the correct tenant origin.

)
Comment on lines +260 to 261
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟑 Minor

Typo: "succuesfully" β†’ "successfully".

Minor typo in the HTML response message.

πŸ“ Proposed fix
                         )}' }, '${frontendOrigin}')}</script> <html>Redirect succuesfully, this window should close now</html>`,
+                        )}' }, '${frontendOrigin}')}</script> <html>Redirect successfully, this window should close now</html>`,
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
)}' }, '${frontendOrigin}')}</script> <html>Redirect succuesfully, this window should close now</html>`,
)
)}' }, '${frontendOrigin}')}</script> <html>Redirect successfully, this window should close now</html>`,
)
πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/server/api/src/app/app.ts` around lines 260 - 261, Fix the typo in
the HTML response string that currently reads "Redirect succuesfully, this
window should close now" to "Redirect successfully, this window should close
now" where that string is built (near the template using frontendOrigin and the
injected script) so the user-facing message returned by the response in app.ts
is spelled correctly.

}
},
Expand Down
Loading