|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { createRequire } from 'node:module'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import path from 'node:path'; |
| 6 | + |
| 7 | +// Run with Wrangler's bundled Miniflare, or an installed compatible Miniflare. |
| 8 | +// Never skip: this gate must execute in workerd, not Node's fetch polyfill. |
| 9 | +const require = createRequire(import.meta.url); |
| 10 | +const { Miniflare, convertV4MiniflareOptions } = require(process.env.PYTHONIDE_MINIFLARE_MODULE || 'miniflare'); |
| 11 | +const runtime = (options) => new Miniflare(convertV4MiniflareOptions(options)); |
| 12 | +const root = process.env.PYTHONIDE_WORKERD_SOURCE_ROOT |
| 13 | + || fileURLToPath(new URL('../', import.meta.url)); |
| 14 | +const modules = ['worker.js', 'github-oauth.js', 'github-oauth-rate-limit.js'] |
| 15 | + .map((name) => ({ type: 'ESModule', path: path.join(root, name) })); |
| 16 | +const origin = 'https://link.pythonide.xin'; |
| 17 | +const headers = { 'Content-Type': 'application/json', 'CF-Connecting-IP': '192.0.2.25' }; |
| 18 | +const grant = { grant_type: 'authorization_code', client_id: 'Iv1_fixture', |
| 19 | + code: 'fixture-code', code_verifier: 'v'.repeat(43) }; |
| 20 | + |
| 21 | +test('real workerd crosses the private binding and rejects upstream redirects', { timeout: 30_000 }, async () => { |
| 22 | + let mode = 'success'; |
| 23 | + const calls = []; |
| 24 | + const mf = runtime({ |
| 25 | + workers: [ |
| 26 | + { name: 'broker', modules, modulesRoot: root, compatibilityDate: '2026-08-16', |
| 27 | + bindings: { GITHUB_MCP_CLIENT_ID: 'Iv1_fixture', GITHUB_MCP_CLIENT_SECRET: 'fixture-secret' }, |
| 28 | + serviceBindings: { GITHUB_MCP_RATE_LIMIT: 'limiter' }, |
| 29 | + outboundService: async (request) => { |
| 30 | + calls.push(request.url); |
| 31 | + if (request.url === 'https://github.com/login/oauth/access_token') { |
| 32 | + const body = new URLSearchParams(await request.text()); |
| 33 | + assert.equal(body.get('client_secret'), 'fixture-secret'); |
| 34 | + if (mode === 'redirect') return new Response(null, { status: 302, headers: { Location: 'https://evil.example/token' } }); |
| 35 | + if (mode === 'invalid_code') return Response.json({ error: 'bad_verification_code' }); |
| 36 | + return Response.json({ access_token: 'ghu_fixture', token_type: 'bearer', expires_in: 28_800, refresh_token: 'ghr_fixture' }); |
| 37 | + } |
| 38 | + assert.equal(request.url, 'https://api.github.com/user'); |
| 39 | + assert.equal(request.headers.get('Authorization'), 'Bearer ghu_fixture'); |
| 40 | + return Response.json({ id: 1234 }); |
| 41 | + } }, |
| 42 | + { name: 'limiter', modules: [modules[2]], modulesRoot: root, compatibilityDate: '2026-08-16', |
| 43 | + ratelimits: { GITHUB_MCP_AUTH_LIMIT: { namespace_id: '1', simple: { limit: 120, period: 60 } } } }, |
| 44 | + ], |
| 45 | + }); |
| 46 | + try { |
| 47 | + const response = await mf.dispatchFetch(`${origin}/mcp-oauth/github/token`, { |
| 48 | + method: 'POST', headers, body: JSON.stringify(grant), |
| 49 | + }); |
| 50 | + assert.equal(response.status, 200, await response.clone().text()); |
| 51 | + assert.equal((await response.json()).account_id, '1234'); |
| 52 | + assert.deepEqual(calls, ['https://github.com/login/oauth/access_token', 'https://api.github.com/user']); |
| 53 | + for (const nextMode of ['invalid_code', 'redirect']) { |
| 54 | + mode = nextMode; |
| 55 | + calls.length = 0; |
| 56 | + const refused = await mf.dispatchFetch(`${origin}/mcp-oauth/github/token`, { |
| 57 | + method: 'POST', headers, body: JSON.stringify(grant), |
| 58 | + }); |
| 59 | + assert.equal(refused.status, nextMode === 'redirect' ? 502 : 400); |
| 60 | + assert.deepEqual(calls, ['https://github.com/login/oauth/access_token']); |
| 61 | + } |
| 62 | + } finally { await mf.dispose(); } |
| 63 | +}); |
| 64 | + |
| 65 | +test('real workerd enforces native admission before allowing further grants', { timeout: 30_000 }, async () => { |
| 66 | + const mf = runtime({ modules: [modules[2]], modulesRoot: root, compatibilityDate: '2026-08-16', |
| 67 | + ratelimits: { GITHUB_MCP_AUTH_LIMIT: { namespace_id: '2', simple: { limit: 2, period: 60 } } } }); |
| 68 | + try { |
| 69 | + const statuses = []; |
| 70 | + for (let index = 0; index < 3; index += 1) { |
| 71 | + const response = await mf.dispatchFetch('https://pythonide-mcp-auth-limiter.internal/consume', { |
| 72 | + method: 'POST', headers: { 'X-PythonIDE-Limit-Key': `github-mcp:${'a'.repeat(64)}` }, |
| 73 | + }); |
| 74 | + statuses.push(response.status); |
| 75 | + } |
| 76 | + assert.deepEqual(statuses, [204, 204, 429]); |
| 77 | + } finally { await mf.dispose(); } |
| 78 | +}); |
0 commit comments