-
Notifications
You must be signed in to change notification settings - Fork 61
Send attestation tokens on login-server requests #736
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
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,18 +1,48 @@ | ||
| /** | ||
| * We only accept *.edge.app or localhost as valid domain names. | ||
| * We only accept *.edge.app, localhost, or (for http/ws only) private LAN IPv4. | ||
| * https/wss still require localhost or *.edge(test)?.app; private IPs are not | ||
| * accepted on secure schemes. | ||
| */ | ||
| export function validateServer(server: string): void { | ||
| const url = new URL(server) | ||
|
|
||
| if (url.protocol === 'http:' || url.protocol === 'ws:') { | ||
| if (url.hostname === 'localhost') return | ||
| if (isPrivateHost(url.hostname)) return | ||
| } | ||
| if (url.protocol === 'https:' || url.protocol === 'wss:') { | ||
| if (url.hostname === 'localhost') return | ||
| if (/^([A-Za-z0-9_-]+\.)*edge(test)?\.app$/.test(url.hostname)) return | ||
| } | ||
|
|
||
| throw new Error( | ||
| `Only *.edge.app or localhost are valid login domain names, not ${url.hostname}` | ||
| `Only *.edge.app, localhost, or private LAN addresses (http/ws) are valid login domain names, not ${url.hostname}` | ||
| ) | ||
| } | ||
|
|
||
| function isPrivateHost(hostname: string): boolean { | ||
| if (hostname === 'localhost') return true | ||
| const octets = parseIpv4(hostname) | ||
| if (octets == null) return false | ||
| const [a, b] = octets | ||
| if (a === 127) return true | ||
| if (a === 10) return true | ||
| if (a === 192 && b === 168) return true | ||
| if (a === 172 && b >= 16 && b <= 31) return true | ||
| return false | ||
| } | ||
|
|
||
| function parseIpv4(hostname: string): [number, number, number, number] | null { | ||
| const parts = hostname.split('.') | ||
| if (parts.length !== 4) return null | ||
| const octets: number[] = [] | ||
| for (const part of parts) { | ||
| if (!/^\d{1,3}$/.test(part)) return null | ||
| const n = Number(part) | ||
| if (!Number.isInteger(n) || n < 0 || n > 255) return null | ||
| // Reject leading zeros like 010.0.0.1 which are not canonical dotted-quad | ||
| // when they reach this helper (URL parsing may already rewrite some forms). | ||
| if (part.length > 1 && part.startsWith('0')) return null | ||
| octets.push(n) | ||
| } | ||
| return octets as [number, number, number, number] | ||
| } | ||
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,56 @@ | ||
| import { expect } from 'chai' | ||
| import { describe, it } from 'mocha' | ||
|
|
||
| import { getInternalStuff } from '../../../src/core/context/internal-api' | ||
| import { makeFakeWorld } from '../../../src/core/core' | ||
| import { makeFakeIo } from '../../../src/index' | ||
| import { | ||
| EdgeFetchFunction, | ||
| EdgeFetchOptions, | ||
| EdgeFetchResponse | ||
| } from '../../../src/types/types' | ||
| import { fakeUser } from '../../fake/fake-user' | ||
|
|
||
| const contextOptions = { apiKey: '', appId: '' } | ||
| const quiet = { onLog() {} } | ||
|
|
||
| describe('attestation header', function () { | ||
| it('attaches and clears x-attestation-token on login-server requests', async function () { | ||
| // Use unbridged makeFakeWorld so we can spy on the context io.fetch | ||
| // that loginFetchInner calls (makeFakeEdgeWorld's yaob bridge hides `_ai`). | ||
| const world = makeFakeWorld({ io: makeFakeIo(), nativeIo: {} }, quiet, [ | ||
| fakeUser | ||
| ]) | ||
| const context = await world.makeEdgeContext(contextOptions) | ||
|
|
||
| const stuff = getInternalStuff(context) as any | ||
| const io = stuff._ai.props.io | ||
| const originalFetch: EdgeFetchFunction = io.fetch.bind(io) | ||
| let lastHeaders: EdgeFetchOptions['headers'] | ||
| io.fetch = async ( | ||
| uri: string, | ||
| opts?: EdgeFetchOptions | ||
| ): Promise<EdgeFetchResponse> => { | ||
| if (uri.includes('/api/')) { | ||
| lastHeaders = opts?.headers | ||
| } | ||
| return await originalFetch(uri, opts) | ||
| } | ||
|
|
||
| await context.setAttestationToken('jwt') | ||
| await context.usernameAvailable('unknown user') | ||
| expect(lastHeaders?.['x-attestation-token']).equals('jwt') | ||
|
|
||
| await context.setAttestationToken(undefined) | ||
| await context.usernameAvailable('unknown user') | ||
| expect(lastHeaders).to.not.have.property('x-attestation-token') | ||
|
|
||
| await context.setAttestationToken('jwt-again') | ||
| await context.usernameAvailable('unknown user') | ||
| expect(lastHeaders?.['x-attestation-token']).equals('jwt-again') | ||
|
|
||
| await context.setAttestationToken('') | ||
| await context.usernameAvailable('unknown user') | ||
| expect(lastHeaders).to.not.have.property('x-attestation-token') | ||
| }) | ||
| }) |
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.
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.
Side effect on fake-world routing:
makeFakeWorldwithallowNetworkAccess: trueroutes fetches by "passes validateServer -> fakeFetch, throws -> real network" (src/core/fake/fake-world.ts). Private-IP URIs now pass, so a fake-world test pointed at a real LAN dev server (the exact use case this PR enables) silently gets answered by the in-memory fake server instead of the network. If that is not intended, the fake-world path may want to keep the old localhost-only check.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.
Fixed in 552ba21, and in a better place than I suggested: fake-world no longer consults
validateServerat all, so test routing stops tracking a production policy function. The explicitisFakeAccountInfrastructureallowlist is the right decoupling.I checked the regex against the hosts this repo actually ships:
login1/login2,info1/info2,sync-us1throughsync-us6,sync-eu(root.ts) and the fake server's owninfo-fake1,sync-fake1..3all match.Two intentional behavior changes worth naming, both consistent with the updated doc comment: under
allowNetworkAccess,localhostand non-account*.edge.apphosts (thecors1..4.edge.appproxies, for instance) now reach the real network instead of the fake server.