Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(desktop): SSRF hardening for the browser agent + review fixes#5763
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c38475
feat(desktop): SSRF hardening for the browser agent + review fixes
waleedlatif1 486f918
fix(desktop): address review findings (handoff callback gate, fail-cl…
waleedlatif1 c1f9a84
refactor: desktop cleanup, shared loopback helpers, api-validation re…
waleedlatif1 a266eba
fix(desktop): fail closed on DNS resolution failure in the agent SSRF…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
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 |
|---|---|---|
| @@ -3,6 +3,7 @@ import { createLogger } from '@sim/logger' | ||
| import type { BrowserWindow, Session, WebContents } from 'electron' | ||
| import { WebContentsView } from 'electron' | ||
| import { registerAgentWebContents } from '@/main/browser-agent/registry' | ||
| import { checkAgentUrl, isBlockedRequestUrl } from '@/main/browser-agent/url-guard' | ||
| const logger = createLogger('BrowserAgentSession') | ||
| @@ -24,7 +25,7 @@ export interface AgentSessionEvents { | ||
| /** The active tab changed (new tab, switch, close). */ | ||
| onActiveTabChanged: (contents: WebContents) => void | ||
| /** A download was blocked on the agent partition. */ | ||
| onDownloadBlocked: (filename: string, url: string) => void | ||
| onDownloadBlocked: (filename: string) => void | ||
| } | ||
| /** | ||
| @@ -68,12 +69,38 @@ function configureAgentPartition(ses: Session): void { | ||
| partitionConfigured = true | ||
| ses.setPermissionRequestHandler((_wc, _permission, callback) => callback(false)) | ||
| ses.setPermissionCheckHandler(() => false) | ||
| // The SSRF choke point for the agent partition. Document navigations | ||
| // (top-level + iframes) get the full DNS-resolving check — this is the ONE | ||
| // seam every navigation passes through, including the page-initiated ones the | ||
| // driver never sees (server redirects, link clicks, location.href, | ||
| // meta-refresh), so an internal-resolving hostname can't slip in that way. | ||
| // Subresources take the cheap synchronous literal-IP backstop instead of a | ||
| // DNS lookup per asset (a hostname subresource is already gated by its | ||
| // document's check). | ||
| ses.webRequest.onBeforeRequest((details, callback) => { | ||
| if (details.resourceType === 'mainFrame' || details.resourceType === 'subFrame') { | ||
| void checkAgentUrl(details.url) | ||
| .then((guard) => { | ||
| if (!guard.ok) { | ||
| logger.warn('Blocked agent document navigation to a private host') | ||
| } | ||
| callback({ cancel: !guard.ok }) | ||
| }) | ||
| .catch((error) => { | ||
| // Fail closed: an unexpected rejection must cancel, never leave the | ||
| // request suspended with no callback. | ||
| logger.error('Agent SSRF check failed; cancelling request', { error }) | ||
| callback({ cancel: true }) | ||
| }) | ||
| return | ||
| } | ||
| callback({ cancel: isBlockedRequestUrl(details.url) }) | ||
| }) | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ses.on('will-download', (_event, item) => { | ||
| const filename = item.getFilename() | ||
| const url = item.getURL() | ||
| logger.info('Blocked download in agent browser', { filename }) | ||
| item.cancel() | ||
| events?.onDownloadBlocked(filename, url) | ||
| events?.onDownloadBlocked(filename) | ||
| }) | ||
| } | ||
| @@ -97,7 +124,9 @@ function createTabView(): WebContentsView { | ||
| configureAgentPartition(contents.session) | ||
| // Popup-neutralize: window.open and target=_blank navigate the SAME view | ||
| // instead of spawning windows — the agent browses one page per tab. | ||
| // instead of spawning windows — the agent browses one page per tab. The | ||
| // resulting navigation is a document request, so the partition's | ||
| // onBeforeRequest SSRF check gates it; no per-call guard is needed here. | ||
| contents.setWindowOpenHandler((details) => { | ||
| if (/^https?:\/\//i.test(details.url)) { | ||
| void contents.loadURL(details.url).catch(() => {}) | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.