-
Notifications
You must be signed in to change notification settings - Fork 293
Review fixes for the store signup JWT handoff #8445
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
Changes from all commits
dc58104
db219b2
e386785
65cdb02
d12057e
eeaf2f6
61a5234
ada18cb
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 |
|---|---|---|
|
|
@@ -5,17 +5,27 @@ import {AbortError} from '@shopify/cli-kit/node/error' | |
| import {outputContent, outputDebug, outputToken} from '@shopify/cli-kit/node/output' | ||
| import {timingSafeEqual} from 'crypto' | ||
| import {createServer} from 'http' | ||
| import type {IncomingHttpHeaders} from 'http' | ||
|
|
||
| export interface AuthorizationRedirect { | ||
| nonce: string | ||
| authorizationUrl: string | ||
| } | ||
|
|
||
| export interface WaitForAuthCodeOptions { | ||
| store: string | ||
| state: string | ||
| port: number | ||
| timeoutMs?: number | ||
| onListening?: () => void | Promise<void> | ||
| authorizationRedirect?: { | ||
| nonce: string | ||
| authorizationUrl: string | ||
| } | ||
| authorizationRedirect?: AuthorizationRedirect | ||
| } | ||
|
|
||
| // Browsers announce a speculative fetch so servers can decline side effects. Serving one would spend | ||
| // the single-use handoff before the navigation it is speculating about ever arrives. | ||
| function isSpeculativeRequest(headers: IncomingHttpHeaders): boolean { | ||
| const purpose = [headers['sec-purpose'], headers.purpose, headers['x-moz']].flat().join(' ') | ||
| return purpose.includes('prefetch') || purpose.includes('prerender') | ||
| } | ||
|
|
||
| function renderAuthCallbackPage(title: string, message: string): string { | ||
|
|
@@ -119,21 +129,26 @@ export async function waitForStoreAuthCode({ | |
| const server = createServer((req, res) => { | ||
| const requestUrl = new URL(req.url ?? '/', `http://127.0.0.1:${port}`) | ||
|
|
||
| if (requestUrl.pathname === STORE_AUTH_HANDOFF_PATH && authorizationRedirect) { | ||
| const returnedNonce = requestUrl.searchParams.get('nonce') | ||
| if (!returnedNonce || !constantTimeEqual(returnedNonce, authorizationRedirect.nonce)) { | ||
| res.statusCode = 403 | ||
| res.setHeader('Cache-Control', 'no-store') | ||
| res.setHeader('Connection', 'close') | ||
| res.end('Forbidden') | ||
| return | ||
| } | ||
| const notFound = () => { | ||
| res.statusCode = 404 | ||
| res.setHeader('Cache-Control', 'no-store') | ||
| res.setHeader('Connection', 'close') | ||
| res.end('Not found') | ||
| } | ||
|
|
||
| if (authorizationRedirectUsed) { | ||
| res.statusCode = 410 | ||
| res.setHeader('Cache-Control', 'no-store') | ||
| res.setHeader('Connection', 'close') | ||
| res.end('Authorization handoff already used') | ||
| if (requestUrl.pathname === STORE_AUTH_HANDOFF_PATH) { | ||
| const returnedNonce = requestUrl.searchParams.get('nonce') | ||
| // Every rejection answers 404 so a local prober cannot tell a wrong nonce from a spent | ||
|
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. Just checking my understanding - the point is to drop the 410 case to make it harder for a local prober to know whether the nonce is wrong or spent?
Contributor
Author
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. yeah - having a uniform 404 also hides whether an auth is in flight at all. every rejection looks like any other unknown path. the real browser flow doesn't notice since it only follows the one valid link. |
||
| // handoff, or either from a port with no store auth in flight. | ||
| const servable = | ||
| authorizationRedirect !== undefined && | ||
| !authorizationRedirectUsed && | ||
| req.method === 'GET' && | ||
| returnedNonce !== null && | ||
| constantTimeEqual(returnedNonce, authorizationRedirect.nonce) | ||
|
|
||
| if (!servable || isSpeculativeRequest(req.headers)) { | ||
| notFound() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -148,8 +163,7 @@ export async function waitForStoreAuthCode({ | |
| } | ||
|
|
||
| if (requestUrl.pathname !== STORE_AUTH_CALLBACK_PATH) { | ||
| res.statusCode = 404 | ||
| res.end('Not found') | ||
| notFound() | ||
| return | ||
| } | ||
|
|
||
|
|
||
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.
Definitely an improvement!