-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: silent pairing for local web-dev servers #3955
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,139 @@ | ||
| import type { ServerAuthDescriptor } from "@t3tools/contracts"; | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
|
|
||
| import { isDevPairingEligible, validateDevPairingRequestHeaders } from "./devPairing.ts"; | ||
|
|
||
| const descriptorWithPolicy = (policy: ServerAuthDescriptor["policy"]): ServerAuthDescriptor => ({ | ||
| policy, | ||
| bootstrapMethods: ["one-time-token"], | ||
| sessionMethods: ["browser-session-cookie", "bearer-access-token", "dpop-access-token"], | ||
| sessionCookieName: "t3_session", | ||
| }); | ||
|
|
||
| const devUrl = new URL("http://localhost:5733"); | ||
|
|
||
| describe("isDevPairingEligible", () => { | ||
| it("is eligible only for loopback web-dev servers", () => { | ||
| assert.isTrue( | ||
| isDevPairingEligible({ mode: "web", devUrl }, descriptorWithPolicy("loopback-browser")), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects production configurations (no devUrl)", () => { | ||
| assert.isFalse( | ||
| isDevPairingEligible( | ||
| { mode: "web", devUrl: undefined }, | ||
| descriptorWithPolicy("loopback-browser"), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects desktop mode even with a loopback devUrl", () => { | ||
| assert.isFalse( | ||
| isDevPairingEligible( | ||
| { mode: "desktop", devUrl }, | ||
| descriptorWithPolicy("desktop-managed-local"), | ||
| ), | ||
| ); | ||
| assert.isFalse( | ||
| isDevPairingEligible({ mode: "desktop", devUrl }, descriptorWithPolicy("loopback-browser")), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects remote-reachable policies", () => { | ||
| assert.isFalse( | ||
| isDevPairingEligible({ mode: "web", devUrl }, descriptorWithPolicy("remote-reachable")), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects non-loopback dev URLs so --dev-url cannot become an auth root", () => { | ||
| assert.isFalse( | ||
| isDevPairingEligible( | ||
| { mode: "web", devUrl: new URL("https://some-site.example") }, | ||
| descriptorWithPolicy("loopback-browser"), | ||
| ), | ||
| ); | ||
| assert.isFalse( | ||
| isDevPairingEligible( | ||
| { mode: "web", devUrl: new URL("http://192.168.1.20:5733") }, | ||
| descriptorWithPolicy("loopback-browser"), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("accepts loopback dev URL variants", () => { | ||
| for (const url of ["http://127.0.0.1:5733", "http://[::1]:5733"]) { | ||
| assert.isTrue( | ||
| isDevPairingEligible( | ||
| { mode: "web", devUrl: new URL(url) }, | ||
| descriptorWithPolicy("loopback-browser"), | ||
| ), | ||
| url, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("validateDevPairingRequestHeaders", () => { | ||
| it("accepts an exact dev-origin Origin with a loopback Host", () => { | ||
| assert.equal( | ||
| validateDevPairingRequestHeaders({ | ||
| originHeader: "http://localhost:5733", | ||
| hostHeader: "localhost:13773", | ||
| devUrl, | ||
| }), | ||
| null, | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects missing, null, or foreign Origins", () => { | ||
| for (const originHeader of [undefined, "", "null", "https://evil.example"]) { | ||
| assert.equal( | ||
| validateDevPairingRequestHeaders({ | ||
| originHeader, | ||
| hostHeader: "localhost:13773", | ||
| devUrl, | ||
| }), | ||
| "origin_mismatch", | ||
| String(originHeader), | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("rejects Origins that differ from the dev origin only by port or scheme", () => { | ||
| for (const originHeader of [ | ||
| "http://localhost:5734", | ||
| "https://localhost:5733", | ||
| "http://127.0.0.1:5733", | ||
| ]) { | ||
| assert.equal( | ||
| validateDevPairingRequestHeaders({ | ||
| originHeader, | ||
| hostHeader: "localhost:13773", | ||
| devUrl, | ||
| }), | ||
| "origin_mismatch", | ||
| originHeader, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("rejects non-loopback Hosts (DNS rebinding) even with a matching Origin", () => { | ||
| assert.equal( | ||
| validateDevPairingRequestHeaders({ | ||
| originHeader: "http://localhost:5733", | ||
| hostHeader: "evil.example:13773", | ||
| devUrl, | ||
| }), | ||
| "host_not_loopback", | ||
| ); | ||
| assert.equal( | ||
| validateDevPairingRequestHeaders({ | ||
| originHeader: "http://localhost:5733", | ||
| hostHeader: undefined, | ||
| devUrl, | ||
| }), | ||
| "host_not_loopback", | ||
| ); | ||
| }); | ||
| }); |
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,47 @@ | ||
| /** | ||
| * devPairing - Eligibility and request validation for dev-mode silent pairing. | ||
| * | ||
| * The dev pairing endpoint mints an administrative one-time credential with no | ||
| * prior authentication, so eligibility is deliberately narrow: a web-mode dev | ||
| * server, bound to loopback (policy "loopback-browser"), whose configured dev | ||
| * URL is itself loopback. Request-level checks then require a browser-attached | ||
| * Origin matching the dev origin exactly — the Origin header survives the Vite | ||
| * dev proxy unmodified, which Host-based checks do not — plus a loopback Host | ||
| * as anti-DNS-rebinding defense for direct requests. | ||
| * | ||
| * @module devPairing | ||
| */ | ||
| import type { ServerAuthDescriptor } from "@t3tools/contracts"; | ||
|
|
||
| import type { ServerConfig } from "../config.ts"; | ||
| import { isLoopbackHost, isLoopbackHostHeader, normalizeHost } from "../netHost.ts"; | ||
|
|
||
| export interface DevPairingConfigInput { | ||
| readonly mode: ServerConfig["Service"]["mode"]; | ||
| readonly devUrl: URL | undefined; | ||
| } | ||
|
|
||
| export const isDevPairingEligible = ( | ||
| config: DevPairingConfigInput, | ||
| descriptor: ServerAuthDescriptor, | ||
| ): boolean => | ||
| config.mode === "web" && | ||
| config.devUrl !== undefined && | ||
| isLoopbackHost(normalizeHost(config.devUrl.hostname)) && | ||
| descriptor.policy === "loopback-browser"; | ||
|
|
||
| export type DevPairingRequestRejection = "origin_mismatch" | "host_not_loopback"; | ||
|
|
||
| export const validateDevPairingRequestHeaders = (input: { | ||
| readonly originHeader: string | undefined; | ||
| readonly hostHeader: string | undefined; | ||
| readonly devUrl: URL; | ||
| }): DevPairingRequestRejection | null => { | ||
| if (!input.originHeader || input.originHeader.trim() !== input.devUrl.origin) { | ||
| return "origin_mismatch"; | ||
| } | ||
| if (!isLoopbackHostHeader(input.hostHeader)) { | ||
| return "host_not_loopback"; | ||
| } | ||
| return null; | ||
| }; | ||
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.
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.
Proxied Origin forge bypass
Medium Severity
The Host loopback check is defeated for requests through the Vite
/apiproxy becausechangeOriginrewritesHostto the loopback upstream. Eligibility only constrains the server bind address, not the Vite listen address. If Vite is LAN-exposed (HOST=0.0.0.0), a remote client can forgeOriginto the configureddevUrlorigin and mint an administrative pairing credential without local filesystem access.Additional Locations (1)
apps/server/src/auth/http.ts#L353-L367Reviewed by Cursor Bugbot for commit 929d35a. Configure here.