diff --git a/changelog.d/fixed-proxy-device-flow-signin.md b/changelog.d/fixed-proxy-device-flow-signin.md new file mode 100644 index 0000000000..13be643758 --- /dev/null +++ b/changelog.d/fixed-proxy-device-flow-signin.md @@ -0,0 +1 @@ +- The bundled proxy no longer rejects device-flow sign-in on direct-route spokes: `POST /api/gh-user-auth/start` and `/poll` (exact paths) are forwarded without the shared token, and mutating requests carrying a `hive_session` cookie reach the Go API, which authenticates the session itself. Neither gets `X-Hive-Internal`; all other unauthenticated mutations still return 401 ([#15872](https://github.com/hivecommons/hive/issues/15872)). diff --git a/src/proxy/api_internal_header.test.js b/src/proxy/api_internal_header.test.js index 703eaf0ceb..c85e6ac9ad 100644 --- a/src/proxy/api_internal_header.test.js +++ b/src/proxy/api_internal_header.test.js @@ -184,6 +184,30 @@ async function main() { assert.equal(seen.internal, null); }); + for (const p of ['/api/gh-user-auth/start', '/api/gh-user-auth/poll']) { + await check(`unauthenticated POST ${p} is forwarded WITHOUT X-Hive-Internal`, async () => { + const res = await req('POST', p); + assert.equal(res.status, 200); + assert.equal(JSON.parse(res.body).internal, null); + }); + } + + await check('POST /api/gh-user-auth/startx is still 401 (exact match, not prefix)', async () => { + const res = await req('POST', '/api/gh-user-auth/startx'); + assert.equal(res.status, 401); + }); + + await check('POST to another unauthenticated route is still 401', async () => { + const res = await req('POST', '/api/gh-user-auth/other'); + assert.equal(res.status, 401); + }); + + await check('POST with a hive_session cookie is forwarded WITHOUT X-Hive-Internal', async () => { + const res = await req('POST', '/api/pause', { Cookie: 'hive_session=abc123' }); + assert.equal(res.status, 200); + assert.equal(JSON.parse(res.body).internal, null); + }); + await stopProxy(proxy); // ── hosted mode: no shared token exists at all ────────────────────────── diff --git a/src/proxy/server.js b/src/proxy/server.js index f0638cd703..3339d74e41 100644 --- a/src/proxy/server.js +++ b/src/proxy/server.js @@ -991,6 +991,10 @@ app.use(async (req, res, next) => { }); const PUBLIC_POST_PATHS = ['/api/contribute/register', '/api/contribute/reissue-token']; +// Device-flow sign-in runs before the visitor holds any credential. Matched +// EXACTLY (query string aside), never by prefix, so a sibling route is not +// opened by accident. /poll only completes a flow for the holder of its flow_id. +const PUBLIC_EXACT_POST_PATHS = ['/api/gh-user-auth/start', '/api/gh-user-auth/poll']; app.use((req, res, next) => { // SECURITY (CWE-306): decide ONCE, for every method, whether this request // actually presented the shared dashboard token. apiProxy injects the @@ -1004,6 +1008,11 @@ app.use((req, res, next) => { req.hiveTokenVerified = bearerTokenMatches(req.headers.authorization); if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)) { if (PUBLIC_POST_PATHS.some(p => req.url.startsWith(p))) return next(); + if (PUBLIC_EXACT_POST_PATHS.includes(req.url.split('?')[0])) return next(); + // A hive_session request is authenticated by the Go API itself (it resolves + // the session and re-checks the role). It never gets X-Hive-Internal: + // hiveTokenVerified stays false. The cookie is SameSite=Lax. + if (parseCookies(req.headers.cookie)['hive_session']) return next(); return requireAuth(req, res, next); } next();