@@ -104,7 +104,23 @@ const withRoutes = <A, E>(
104104 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ;
105105
106106const json = < A > ( response : Response ) => Effect . promise ( ( ) => response . json ( ) as Promise < A > ) ;
107- const text = ( response : Response ) => Effect . promise ( ( ) => response . text ( ) ) ;
107+
108+ /** What the approval page posts: the agent's request plus the user's choice. */
109+ const postJson = ( path : string , body : Record < string , string > , cookie ?: string ) =>
110+ at ( path , {
111+ method : "POST" ,
112+ headers : { "content-type" : "application/json" , ...( cookie ? { cookie } : { } ) } ,
113+ body : encodeJson ( body ) ,
114+ } ) ;
115+
116+ const decide = ( handler : Handler , body : Record < string , string > , cookie ?: string ) =>
117+ handler ( postJson ( "/oauth/mcp/decision" , body , cookie ) ) . pipe (
118+ Effect . flatMap ( ( response ) =>
119+ json < { redirectTo ?: string ; error ?: string } > ( response ) . pipe (
120+ Effect . map ( ( payload ) => ( { status : response . status , ...payload } ) ) ,
121+ ) ,
122+ ) ,
123+ ) ;
108124
109125const register = ( handler : Handler , redirect = REDIRECT ) =>
110126 handler (
@@ -187,12 +203,26 @@ it.live("registers only loopback clients and never redirects for an unverified c
187203 expect ( unregistered . status ) . toBe ( 400 ) ;
188204 expect ( unregistered . headers . get ( "location" ) ) . toBeNull ( ) ;
189205
190- const page = yield * handler (
191- at ( `/oauth/mcp/authorize?${ new URLSearchParams ( authorizeParams ( clientId ) ) } ` ) ,
206+ // A valid request is handed to the web app's approval page, query intact.
207+ const query = new URLSearchParams ( authorizeParams ( clientId ) ) . toString ( ) ;
208+ const handoff = yield * handler ( at ( `/oauth/mcp/authorize?${ query } ` ) ) ;
209+ expect ( handoff . status ) . toBe ( 302 ) ;
210+ expect ( handoff . headers . get ( "location" ) ) . toBe ( `/connect-agent?${ query } ` ) ;
211+ const details = yield * handler (
212+ postJson ( "/oauth/mcp/approval" , authorizeParams ( clientId ) ) ,
213+ ) . pipe ( Effect . flatMap ( json < Record < string , unknown > > ) ) ;
214+ expect ( details ) . toEqual ( {
215+ clientName : "Claude Code" ,
216+ redirectHost : "localhost:51234" ,
217+ environmentHost : "box.example.ts.net" ,
218+ } ) ;
219+
220+ // The approval endpoints re-check the request: a forged client gets a message, not a URL.
221+ const forgedDetails = yield * handler (
222+ postJson ( "/oauth/mcp/approval" , authorizeParams ( `${ clientId . split ( "." ) [ 0 ] } .forged` ) ) ,
192223 ) ;
193- expect ( page . status ) . toBe ( 200 ) ;
194- expect ( page . headers . get ( "content-security-policy" ) ) . toContain ( "frame-ancestors 'none'" ) ;
195- expect ( yield * text ( page ) ) . toContain ( "Pairing code" ) ;
224+ expect ( forgedDetails . status ) . toBe ( 400 ) ;
225+ expect ( yield * json < Record < string , unknown > > ( forgedDetails ) ) . not . toHaveProperty ( "redirectTo" ) ;
196226 } ) ,
197227 ) ,
198228) ;
@@ -204,28 +234,23 @@ it.live("signs in with a pairing code and issues a token only /mcp accepts", ()
204234 const pairing = yield * auth . issuePairingCredential ( ) ;
205235 const params = authorizeParams ( clientId ) ;
206236
207- const wrongCode = yield * handler (
208- at (
209- "/oauth/mcp/authorize ",
210- form ( { ... params , decision : "approve" , runtime_mode : "auto" , pairing_code : "nope" } ) ,
211- ) ,
212- ) ;
237+ const wrongCode = yield * decide ( handler , {
238+ ... params ,
239+ decision : "approve ",
240+ runtime_mode : "auto" ,
241+ pairing_code : "nope" ,
242+ } ) ;
213243 expect ( wrongCode . status ) . toBe ( 400 ) ;
214- expect ( yield * text ( wrongCode ) ) . toContain ( "unknown, expired, or already used" ) ;
244+ expect ( wrongCode . error ) . toContain ( "unknown, expired, or already used" ) ;
215245
216- const approved = yield * handler (
217- at (
218- "/oauth/mcp/authorize" ,
219- form ( {
220- ...params ,
221- decision : "approve" ,
222- runtime_mode : "auto" ,
223- pairing_code : pairing . credential ,
224- } ) ,
225- ) ,
226- ) ;
227- expect ( approved . status ) . toBe ( 302 ) ;
228- const callback = new URL ( approved . headers . get ( "location" ) ! ) ;
246+ const approved = yield * decide ( handler , {
247+ ...params ,
248+ decision : "approve" ,
249+ runtime_mode : "auto" ,
250+ pairing_code : pairing . credential ,
251+ } ) ;
252+ expect ( approved . status ) . toBe ( 200 ) ;
253+ const callback = new URL ( approved . redirectTo ! ) ;
229254 expect ( callback . origin ) . toBe ( "http://localhost:51234" ) ;
230255 expect ( callback . searchParams . get ( "state" ) ) . toBe ( "state-1" ) ;
231256 expect ( callback . searchParams . get ( "iss" ) ) . toBe ( ORIGIN ) ;
@@ -285,18 +310,13 @@ it.live("rejects a wrong PKCE verifier and spends the code", () =>
285310 const clientId = yield * registeredClientId ( handler ) ;
286311 const pairing = yield * auth . issuePairingCredential ( ) ;
287312 const params = authorizeParams ( clientId ) ;
288- const approved = yield * handler (
289- at (
290- "/oauth/mcp/authorize" ,
291- form ( {
292- ...params ,
293- decision : "approve" ,
294- runtime_mode : "approval-required" ,
295- pairing_code : pairing . credential ,
296- } ) ,
297- ) ,
298- ) ;
299- const code = new URL ( approved . headers . get ( "location" ) ! ) . searchParams . get ( "code" ) ! ;
313+ const approved = yield * decide ( handler , {
314+ ...params ,
315+ decision : "approve" ,
316+ runtime_mode : "approval-required" ,
317+ pairing_code : pairing . credential ,
318+ } ) ;
319+ const code = new URL ( approved . redirectTo ! ) . searchParams . get ( "code" ) ! ;
300320 const exchange = ( codeVerifier : string ) =>
301321 handler (
302322 at (
@@ -324,21 +344,18 @@ it.live("denies and refuses codes bound to another client's key or without threa
324344 const clientId = yield * registeredClientId ( handler ) ;
325345 const params = authorizeParams ( clientId ) ;
326346
327- const denied = yield * handler (
328- at ( "/oauth/mcp/authorize" , form ( { ...params , decision : "deny" } ) ) ,
329- ) ;
330- expect ( denied . status ) . toBe ( 302 ) ;
331- const deniedUrl = new URL ( denied . headers . get ( "location" ) ! ) ;
347+ const denied = yield * decide ( handler , { ...params , decision : "deny" } ) ;
348+ const deniedUrl = new URL ( denied . redirectTo ! ) ;
332349 expect ( deniedUrl . searchParams . get ( "error" ) ) . toBe ( "access_denied" ) ;
333350 expect ( deniedUrl . searchParams . get ( "state" ) ) . toBe ( "state-1" ) ;
334351
335352 const approveWith = ( code : string ) =>
336- handler (
337- at (
338- "/oauth/mcp/authorize ",
339- form ( { ... params , decision : "approve" , runtime_mode : "auto" , pairing_code : code } ) ,
340- ) ,
341- ) ;
353+ decide ( handler , {
354+ ... params ,
355+ decision : "approve ",
356+ runtime_mode : "auto" ,
357+ pairing_code : code ,
358+ } ) ;
342359
343360 // A T3 Connect code is bound to a device key: refused, and still usable by its device.
344361 const bound = yield * auth . createPairingLink ( { proofKeyThumbprint : "device-key-thumbprint" } ) ;
@@ -359,7 +376,7 @@ it.live("denies and refuses codes bound to another client's key or without threa
359376 const readOnly = yield * auth . issuePairingCredential ( { scopes : [ "orchestration:read" ] } ) ;
360377 const readOnlyResponse = yield * approveWith ( readOnly . credential ) ;
361378 expect ( readOnlyResponse . status ) . toBe ( 400 ) ;
362- expect ( yield * text ( readOnlyResponse ) ) . toContain ( "cannot control threads" ) ;
379+ expect ( readOnlyResponse . error ) . toContain ( "cannot control threads" ) ;
363380 } ) ,
364381 ) ,
365382) ;
@@ -382,28 +399,36 @@ it.live("offers one-click only to a browser session that holds the scopes it wou
382399 ) ;
383400 return response . headers . getSetCookie ( ) [ 0 ] ! . split ( ";" , 1 ) [ 0 ] ! ;
384401 } ) ;
385- const page = ( cookie : string ) =>
386- handler (
387- at ( `/oauth/mcp/authorize? ${ new URLSearchParams ( params ) } ` , { headers : { cookie } } ) ,
388- ) . pipe ( Effect . flatMap ( text ) ) ;
402+ const details = ( cookie : string ) =>
403+ handler ( postJson ( "/oauth/mcp/approval" , params , cookie ) ) . pipe (
404+ Effect . flatMap ( json < { csrfToken ?: string } > ) ,
405+ ) ;
389406
390407 const admin = yield * browserCookie ( [ ...AuthAdministrativeScopes ] ) ;
391- expect ( yield * page ( admin ) ) . toContain ( 'name="csrf_token"' ) ;
408+ const adminDetails = yield * details ( admin ) ;
409+ expect ( adminDetails . csrfToken ) . toEqual ( expect . any ( String ) ) ;
410+ const oneClick = yield * decide (
411+ handler ,
412+ {
413+ ...params ,
414+ decision : "approve" ,
415+ runtime_mode : "auto" ,
416+ csrf_token : adminDetails . csrfToken ! ,
417+ } ,
418+ admin ,
419+ ) ;
420+ expect ( new URL ( oneClick . redirectTo ! ) . searchParams . get ( "code" ) ) . toEqual ( expect . any ( String ) ) ;
392421
393422 // access:write alone cannot hand an agent thread control it does not hold.
394423 const accessOnly = yield * browserCookie ( [ "access:read" , "access:write" ] ) ;
395- expect ( yield * page ( accessOnly ) ) . not . toContain ( 'name="csrf_token"' ) ;
396- const forged = yield * handler (
397- at ( "/oauth/mcp/authorize" , {
398- ...form ( { ...params , decision : "approve" , runtime_mode : "auto" , csrf_token : "forged" } ) ,
399- headers : {
400- "content-type" : "application/x-www-form-urlencoded" ,
401- cookie : accessOnly ,
402- } ,
403- } ) ,
424+ expect ( ( yield * details ( accessOnly ) ) . csrfToken ) . toBeUndefined ( ) ;
425+ const forged = yield * decide (
426+ handler ,
427+ { ...params , decision : "approve" , runtime_mode : "auto" , csrf_token : "forged" } ,
428+ accessOnly ,
404429 ) ;
405430 expect ( forged . status ) . toBe ( 400 ) ;
406- expect ( yield * text ( forged ) ) . toContain ( "Enter a pairing code instead" ) ;
431+ expect ( forged . error ) . toContain ( "Enter a pairing code instead" ) ;
407432 } ) ,
408433 ) ,
409434) ;
0 commit comments