okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1// #3208: the local-network share listener answers browser CORS the way the app's own API does - when an API key is set. 2// The reporter's chatbot page (a browser on another machine) could not get past the preflight: OPTIONS carries no key 3// (the spec says so), openai() checks the key first, so every preflight got 401 and no answer had a CORS header. 4// Without a key the listener sends no CORS headers, so it stays "a device on this network" (SECURITY.md). 5import test from 'node:test' ; 6import assert from 'node:assert/strict' ; 7import { createLocalShare } from '../server/local-share.mjs' ; 8 9/** openai() as index.mjs runs it: the key check first (no key set means every client passes), then path and method. */ 10const openaiLike = key => ( req , res , path , method ) => { 11if ( key && req . headers . authorization !== 'Bearer test-only-key' ) { 12res . writeHead ( 401 , { 'content-type' :'application/json' }); 13res . end ( '{"error":"Invalid API key"}' ); 14return ; 15} 16if ( path === '/v1/models' && method === 'GET' ) { 17res . writeHead ( 200 , { 'content-type' :'application/json' }); 18res . end ( '{"object":"list","data":[]}' ); 19return ; 20} 21res . writeHead ( 404 , { 'content-type' :'application/json' }); 22res . end ( '{"error":"No route"}' ); 23}; 24 25const preflight = ( root , origin , extra = {}) => fetch ( ` ${ root } /v1/models` , { 26method :'OPTIONS' , 27headers :{ origin, 'access-control-request-method' :'GET' , ...extra }, 28}); 29 30test ( 'with a key, the shared listener answers the preflight without one and allows any origin' , async () => { 31const share = createLocalShare ({ handle :openaiLike ( true ), hasKey :() => true , host :'127.0.0.1' , port :0 }); 32try { 33const { url} = await share . start (); 34const root = url . replace ( / \/v1$ / , '' ); 35const res = await preflight ( root , 'http://192.168.1.20:9999' , { 'access-control-request-headers' :'authorization,content-type' }); 36assert . equal ( res . status , 204 ); 37assert . equal ( res . headers . get ( 'access-control-allow-origin' ), '*' ); 38assert . match ( res . headers . get ( 'access-control-allow-methods' ), / OPTIONS / ); 39assert . equal ( res . headers . get ( 'access-control-allow-headers' ), 'authorization,content-type' ); 40assert . equal ( res . headers . get ( 'access-control-max-age' ), '600' ); 41// a preflight that asks for nothing still names the headers the SDKs and Anthropic browser clients send 42const plain = await preflight ( root , 'http://example.org' ); 43assert . match ( plain . headers . get ( 'access-control-allow-headers' ), / authorization / ); 44assert . match ( plain . headers . get ( 'access-control-allow-headers' ), / anthropic-version / ); 45} finally { await share . stop (); } 46}); 47 48test ( 'with a key, real answers carry Access-Control-Allow-Origin too - the 401 for a missing key as well' , async () => { 49const share = createLocalShare ({ handle :openaiLike ( true ), hasKey :() => true , host :'127.0.0.1' , port :0 }); 50try { 51const { url} = await share . start (); 52const ok = await fetch ( url + '/models' , { headers :{ origin :'http://example.org' , authorization :'Bearer test-only-key' } }); 53assert . equal ( ok . status , 200 ); 54assert . equal ( ok . headers . get ( 'access-control-allow-origin' ), '*' ); 55const denied = await fetch ( url + '/models' , { headers :{ origin :'http://example.org' } }); 56assert . equal ( denied . status , 401 ); 57assert . equal ( denied . headers . get ( 'access-control-allow-origin' ), '*' , 'the browser must see the real 401, not a CORS wall' ); 58} finally { await share . stop (); } 59}); 60 61test ( 'a preflight grants nothing: the shared listener still answers no host control, only inference' , async () => { 62const share = createLocalShare ({ handle :openaiLike ( true ), hasKey :() => true , host :'127.0.0.1' , port :0 }); 63try { 64const { url} = await share . start (); 65const root = url . replace ( / \/v1$ / , '' ); 66assert . equal (( await fetch ( root + '/api/tasks' , { method :'OPTIONS' , headers :{ origin :'http://example.org' } })). status , 204 ); 67assert . equal (( await fetch ( root + '/api/tasks' , { headers :{ authorization :'Bearer test-only-key' } })). status , 404 ); 68} finally { await share . stop (); } 69}); 70 71test ( 'without a key nothing changes: no CORS headers, and the preflight falls through as before' , async () => { 72const share = createLocalShare ({ handle :openaiLike ( false ), hasKey :() => false , host :'127.0.0.1' , port :0 }); 73try { 74const { url} = await share . start (); 75const root = url . replace ( / \/v1$ / , '' ); 76const pre = await preflight ( root , 'http://example.org' , { 'access-control-request-headers' :'authorization,content-type' }); 77assert . equal ( pre . status , 404 , 'OPTIONS /v1/models reaches the route check, as before the fix' ); 78assert . equal ( pre . headers . get ( 'access-control-allow-origin' ), null ); 79const get = await fetch ( url + '/models' , { headers :{ origin :'http://example.org' } }); 80assert . equal ( get . status , 200 , 'a device on the network still uses the keyless share' ); 81assert . equal ( get . headers . get ( 'access-control-allow-origin' ), null , 'a keyless share never lets a web page of another site read the model' ); 82} finally { await share . stop (); } 83});