-
Notifications
You must be signed in to change notification settings - Fork 62
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side effect on fake-world routing: |
||
| } | ||
| 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] | ||
| } | ||
| 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') | ||
| }) | ||
| }) |
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.
Nit: em-dash in the comment; repo convention is zero em-dashes in committed code (comma or semicolon instead). Full ruleset: https://github.com/EdgeApp/edge-dev-agents/blob/main/.cursor/skills/no-slop/SKILL.md