-
Notifications
You must be signed in to change notification settings - Fork 54
feat(browser): in-app browser tab with webview security hardening #3181
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
MattPua
wants to merge
8
commits into
main
Choose a base branch
from
posthog-code/browser-tab
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ca9a4e
feat(browser): in-app browser tab with webview security hardening
MattPua 36c33c9
feat(browser): add-tab dropdown in panel tab bars
MattPua 851fac6
feat(browser): loading indicator while pages load
MattPua deae0e5
feat(browser): focus address bar and show placeholder on blank tabs
MattPua d850a44
refactor(browser): quill nav buttons, clearer webview security comments
MattPua a26ba23
docs(browser): correct stale TODO on webview nav method deprecation
MattPua 24af027
fix(browser): close metadata-host SSRF bypasses in webview guard
MattPua 675f339
refactor(panels): collapse three update-one-tab transforms into one h…
MattPua 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| isAllowedWebviewNavigation, | ||
| isBlockedWebviewHost, | ||
| } from "./webview-navigation-guard"; | ||
|
|
||
| describe("isBlockedWebviewHost", () => { | ||
| it.each([ | ||
| ["169.254.169.254", true], | ||
| ["169.254.0.1", true], | ||
| // WHATWG URL folds decimal/hex IPv4 to dotted form before this runs. | ||
| [new URL("http://2852039166/").hostname, true], | ||
| // IPv6-mapped IPv4: OS still connects to the v4 metadata service. | ||
| [new URL("http://[::ffff:169.254.169.254]/").hostname, true], | ||
| ["metadata.google.internal", true], | ||
| ["METADATA.GOOGLE.INTERNAL", true], | ||
| ["localhost", false], | ||
| ["127.0.0.1", false], | ||
| ["192.168.1.5", false], | ||
| ["posthog.com", false], | ||
| // Not the metadata address — a normal 169.253.x host is allowed. | ||
| ["169.253.1.1", false], | ||
| ])("host %j -> blocked %s", (hostname, blocked) => { | ||
| expect(isBlockedWebviewHost(hostname)).toBe(blocked); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isAllowedWebviewNavigation", () => { | ||
| it.each([ | ||
| ["https://posthog.com", true], | ||
| ["http://localhost:3000", true], | ||
| ["about:blank", true], | ||
| // Blocked schemes fall through to search in the renderer; the guard vetoes. | ||
| ["file:///etc/passwd", false], | ||
| ["chrome://settings", false], | ||
| ["javascript:alert(1)", false], | ||
| ["data:text/html,<h1>hi</h1>", false], | ||
| // Metadata endpoint over an allowed scheme is still blocked by host. | ||
| ["http://169.254.169.254/latest/meta-data/", false], | ||
| ["http://metadata.google.internal/computeMetadata/v1/", false], | ||
| ["not a url", false], | ||
| ])("url %j -> allowed %s", (url, allowed) => { | ||
| expect(isAllowedWebviewNavigation(url)).toBe(allowed); | ||
| }); | ||
| }); |
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,55 @@ | ||
| // The authoritative gate for the in-app browser guest. It runs in the main | ||
| // process, where a guest page can't route around it; the renderer's | ||
| // normalizeAddress is only a convenience on top of this. "about:" is allowed | ||
| // solely for about:blank — the src a new blank browser tab mounts with before | ||
| // the user enters a url. | ||
| const ALLOWED_WEBVIEW_SCHEMES = new Set(["http:", "https:", "about:"]); | ||
|
|
||
| // Blocks the cloud instance-metadata endpoint. On cloud VMs it returns IAM / | ||
| // service-account credentials to any local caller, so a hostile page that | ||
| // redirects the webview there could exfiltrate them. Loopback and LAN stay | ||
| // allowed: browsing a local dev server is a first-class use of this browser. | ||
| // | ||
| // WHATWG URL canonicalizes decimal/hex/octal IPv4 (http://2852039166) back to | ||
| // dotted form before this runs, so those are covered by the v4 range. The | ||
| // entries below close the forms it does NOT fold together: the IPv6-mapped | ||
| // address (the OS still connects to the v4 metadata service) and GCP's | ||
| // metadata DNS name. | ||
| // | ||
| // Residual gap this cannot close: DNS rebinding — an attacker domain that | ||
| // resolves to 169.254.169.254 passes, because the real defense is checking the | ||
| // *resolved* IP at connect time, which will-navigate doesn't expose. Egress | ||
| // network policy on the sandbox is the actual boundary for that. | ||
| const BLOCKED_METADATA_HOST = /^169\.254\./; | ||
| const BLOCKED_METADATA_HOST_V6 = /^\[::ffff:a9fe:a9fe\]$/i; | ||
| const BLOCKED_METADATA_HOSTNAMES = new Set(["metadata.google.internal"]); | ||
|
|
||
| export function isBlockedWebviewHost(hostname: string): boolean { | ||
| const host = hostname.toLowerCase(); | ||
| return ( | ||
| BLOCKED_METADATA_HOST.test(host) || | ||
| BLOCKED_METADATA_HOST_V6.test(host) || | ||
| BLOCKED_METADATA_HOSTNAMES.has(host) | ||
| ); | ||
| } | ||
|
|
||
| export function safeProtocol(url: string): string { | ||
| try { | ||
| return new URL(url).protocol; | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| export function isAllowedWebviewNavigation(url: string): boolean { | ||
| let parsed: URL; | ||
| try { | ||
| parsed = new URL(url); | ||
| } catch { | ||
| return false; | ||
| } | ||
| return ( | ||
| ALLOWED_WEBVIEW_SCHEMES.has(parsed.protocol) && | ||
| !isBlockedWebviewHost(parsed.hostname) | ||
| ); | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.