-
-
Notifications
You must be signed in to change notification settings - Fork 2
Auto-provision the admin-login Turnstile widget (security F1 — wizard half) #207
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
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,267 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import type { CfApiResult } from './setup-lib.ts'; | ||
| import { | ||
| provisionTurnstileWidget, | ||
| buildCreateBody, | ||
| WIDGET_NAME, | ||
| WIDGET_MODE | ||
| } from './turnstile-lib.ts'; | ||
|
|
||
| const TOKEN = 'cf-secret-token-value-should-never-leak'; | ||
| const WIDGET_SECRET = 'turnstile-widget-secret-should-never-leak'; | ||
| const ACCT = 'acct123'; | ||
| const SITEKEY = '0x4AAAAAAAsitekey'; | ||
|
|
||
| interface Call { | ||
| token: string; | ||
| path: string; | ||
| method: string; | ||
| body?: unknown; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a fake `cfApi` that never touches the network: it records every call and | ||
| * answers from a path+method → result map. Any path not in the map returns a 500, | ||
| * which surfaces as an unexpected-call failure in assertions. | ||
| */ | ||
| function fakeApi(routes: Record<string, CfApiResult>) { | ||
| const calls: Call[] = []; | ||
| const api = async ( | ||
| token: string, | ||
| path: string, | ||
| init: { method?: string; body?: unknown } = {} | ||
| ): Promise<CfApiResult> => { | ||
| const method = init.method ?? 'GET'; | ||
| calls.push({ token, path, method, body: init.body }); | ||
| return routes[`${method} ${path}`] ?? routes[path] ?? { ok: false, status: 500 }; | ||
| }; | ||
| return { api, calls }; | ||
| } | ||
|
|
||
| const listPath = `GET /accounts/${ACCT}/challenges/widgets?per_page=50`; | ||
| const createPath = `POST /accounts/${ACCT}/challenges/widgets`; | ||
| const getPath = `GET /accounts/${ACCT}/challenges/widgets/${SITEKEY}`; | ||
|
|
||
| describe('buildCreateBody', () => { | ||
| it('encodes the stable name, the domain, and managed mode', () => { | ||
| expect(buildCreateBody('akito.dog')).toEqual({ | ||
| name: WIDGET_NAME, | ||
| domains: ['akito.dog'], | ||
| mode: WIDGET_MODE | ||
| }); | ||
| expect(WIDGET_NAME).toBe('sona-admin-login'); | ||
| expect(WIDGET_MODE).toBe('managed'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('provisionTurnstileWidget — creates when absent', () => { | ||
| it('POSTs a new widget when none of ours exists, returning sitekey + secret', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { ok: true, status: 200, result: [] }, | ||
| [createPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('created'); | ||
| expect(res.sitekey).toBe(SITEKEY); | ||
| expect(res.secret).toBe(WIDGET_SECRET); | ||
|
|
||
| const post = calls.find((c) => c.method === 'POST'); | ||
| expect(post?.path).toBe(`/accounts/${ACCT}/challenges/widgets`); | ||
| expect(post?.body).toEqual({ name: WIDGET_NAME, domains: ['akito.dog'], mode: WIDGET_MODE }); | ||
| // No get-by-sitekey when we just created it. | ||
| expect(calls.some((c) => c.path.endsWith(`/widgets/${SITEKEY}`))).toBe(false); | ||
| }); | ||
|
|
||
| it('ignores widgets with a different name and creates ours', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [{ name: 'someone-elses-widget', sitekey: 'other-key' }] | ||
| }, | ||
| [createPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('created'); | ||
| // Never fetched the unrelated widget's secret. | ||
| expect(calls.some((c) => c.path.includes('other-key'))).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('provisionTurnstileWidget — reuses when present (idempotent)', () => { | ||
| it('finds our widget by name + host and reads its secret via the single-widget GET', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [{ name: WIDGET_NAME, sitekey: SITEKEY, domains: ['akito.dog'] }] | ||
| }, | ||
| [getPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('exists'); | ||
| expect(res.sitekey).toBe(SITEKEY); | ||
| expect(res.secret).toBe(WIDGET_SECRET); | ||
| // Reuse must NOT create a duplicate. | ||
| expect(calls.some((c) => c.method === 'POST')).toBe(false); | ||
| const get = calls.find((c) => c.path === `/accounts/${ACCT}/challenges/widgets/${SITEKEY}`); | ||
| expect(get?.method).toBe('GET'); | ||
| }); | ||
|
|
||
| // One Cloudflare account can hold several forks, and every fork's widget carries | ||
| // the same stable name — so the host, not the name alone, is what identifies ours. | ||
| // Reusing a sibling fork's widget would hand this fork a sitekey scoped to the | ||
| // wrong domain: every Turnstile verify then fails and, F1 being fail-closed, the | ||
| // admin login locks. A duplicate widget is the acceptable failure; this is not. | ||
| it('ignores a same-name widget issued for a SIBLING fork and creates ours', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [{ name: WIDGET_NAME, sitekey: 'sibling-fork-key', domains: ['sparky.ink'] }] | ||
| }, | ||
| [createPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('created'); | ||
| expect(res.sitekey).toBe(SITEKEY); | ||
| // Never adopted the sibling's sitekey, and never read its secret. | ||
| expect(res.sitekey).not.toBe('sibling-fork-key'); | ||
| expect(calls.some((c) => c.path.includes('sibling-fork-key'))).toBe(false); | ||
| const post = calls.find((c) => c.method === 'POST'); | ||
| expect(post?.body).toEqual({ name: WIDGET_NAME, domains: ['akito.dog'], mode: WIDGET_MODE }); | ||
| }); | ||
|
|
||
| it('picks OUR host out of a multi-fork account listing several of our widgets', async () => { | ||
| const { api } = fakeApi({ | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [ | ||
| { name: WIDGET_NAME, sitekey: 'sparky-key', domains: ['sparky.ink'] }, | ||
| { name: WIDGET_NAME, sitekey: SITEKEY, domains: ['akito.dog'] } | ||
| ] | ||
| }, | ||
| [getPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('exists'); | ||
| // The FIRST listed widget is a sibling's — order must not decide the match. | ||
| expect(res.sitekey).toBe(SITEKEY); | ||
| }); | ||
|
|
||
| it('treats a widget with no domains field as not ours (creates rather than reuses)', async () => { | ||
| const { api } = fakeApi({ | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [{ name: WIDGET_NAME, sitekey: 'domainless-key' }] | ||
| }, | ||
| [createPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('created'); | ||
| expect(res.sitekey).toBe(SITEKEY); | ||
| }); | ||
|
|
||
| it('errors (no mutation) when the existing widget’s secret cannot be read', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [{ name: WIDGET_NAME, sitekey: SITEKEY, domains: ['akito.dog'] }] | ||
| }, | ||
| // GET succeeds but returns no secret (e.g. a partial/blank body). | ||
| [getPath]: { ok: true, status: 200, result: { sitekey: SITEKEY } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('error'); | ||
| expect(res.secret).toBeUndefined(); | ||
| expect(res.detail).toContain('could not read its secret'); | ||
| expect(calls.some((c) => c.method === 'POST')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('provisionTurnstileWidget — clear errors, no mutation', () => { | ||
| it('token lacks Turnstile scope (list 403) → error naming the scope, no create', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { ok: false, status: 403 } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('error'); | ||
| expect(res.detail).toContain('Turnstile: Edit'); | ||
| // Only the list GET happened — never proceeded to create. | ||
| expect(calls).toHaveLength(1); | ||
| expect(calls[0].method).toBe('GET'); | ||
| }); | ||
|
|
||
| it('create call fails → scoped error, sitekey/secret absent', async () => { | ||
| const { api } = fakeApi({ | ||
| [listPath]: { ok: true, status: 200, result: [] }, | ||
| [createPath]: { ok: false, status: 403 } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('error'); | ||
| expect(res.detail).toContain('failed to create'); | ||
| expect(res.sitekey).toBeUndefined(); | ||
| expect(res.secret).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('create returns ok but a body with no sitekey/secret → error', async () => { | ||
| const { api } = fakeApi({ | ||
| [listPath]: { ok: true, status: 200, result: [] }, | ||
| [createPath]: { ok: true, status: 200, result: {} } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.status).toBe('error'); | ||
| }); | ||
|
|
||
| it('empty domain → error before any network call', async () => { | ||
| const { api, calls } = fakeApi({}); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, ' ', api); | ||
| expect(res.status).toBe('error'); | ||
| expect(calls).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('provisionTurnstileWidget — never leaks the token or the widget secret', () => { | ||
| it('the CF token appears in no returned detail and only ever rides as the first arg', async () => { | ||
| const { api, calls } = fakeApi({ | ||
| [listPath]: { ok: true, status: 200, result: [] }, | ||
| [createPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.detail).not.toContain(TOKEN); | ||
| for (const c of calls) { | ||
| expect(c.token).toBe(TOKEN); | ||
| expect(c.path).not.toContain(TOKEN); | ||
| expect(JSON.stringify(c.body ?? '')).not.toContain(TOKEN); | ||
| } | ||
| }); | ||
|
|
||
| it('the widget secret never appears in a detail string, across create and reuse', async () => { | ||
| const scenarios: Record<string, CfApiResult>[] = [ | ||
| // created | ||
| { | ||
| [listPath]: { ok: true, status: 200, result: [] }, | ||
| [createPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| }, | ||
| // reused | ||
| { | ||
| [listPath]: { | ||
| ok: true, | ||
| status: 200, | ||
| result: [{ name: WIDGET_NAME, sitekey: SITEKEY, domains: ['akito.dog'] }] | ||
| }, | ||
| [getPath]: { ok: true, status: 200, result: { sitekey: SITEKEY, secret: WIDGET_SECRET } } | ||
| } | ||
| ]; | ||
| for (const routes of scenarios) { | ||
| const { api } = fakeApi(routes); | ||
| const res = await provisionTurnstileWidget(TOKEN, ACCT, 'akito.dog', api); | ||
| expect(res.detail).not.toContain(WIDGET_SECRET); | ||
| // The secret is still returned for wiring — just never in the printable detail. | ||
| expect(res.secret).toBe(WIDGET_SECRET); | ||
| } | ||
| }); | ||
| }); |
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.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
Do not report Turnstile as enabled before its credentials are actually wired.
turnstileStatusreflects provisioning only (Line 336). A failed Pages PATCH or swallowedputSecretfailure still reaches this success message, and the GitHub-secret list omitsTURNSTILE_SECRETfor the first CI deployment. Track successful sitekey and secret writes separately, include the secret in CI wiring/deploy sync, and print “configured” only when both succeed.🤖 Prompt for AI Agents