@@ -180,12 +180,20 @@ it.live("derives discovery metadata from the origin the client reached", () =>
180180 ) ,
181181) ;
182182
183- it . live ( "registers only loopback clients and never redirects for an unverified client" , ( ) =>
183+ it . live ( "registers loopback and https clients and never redirects for an unverified client" , ( ) =>
184184 withRoutes ( ( handler ) =>
185185 Effect . gen ( function * ( ) {
186- const remote = yield * register ( handler , "https://attacker.example/callback" ) ;
187- expect ( remote . status ) . toBe ( 400 ) ;
188- expect ( yield * json < unknown > ( remote ) ) . toMatchObject ( { error : "invalid_redirect_uri" } ) ;
186+ for ( const refused of [
187+ "http://bot.example/callback" ,
188+ "https://user:[email protected] /callback" , 189+ "https://bot.example/callback#" ,
190+ "http://localhost/callback#" ,
191+ "javascript:alert(1)" ,
192+ ] ) {
193+ const response = yield * register ( handler , refused ) ;
194+ expect ( response . status ) . toBe ( 400 ) ;
195+ expect ( yield * json < unknown > ( response ) ) . toMatchObject ( { error : "invalid_redirect_uri" } ) ;
196+ }
189197
190198 const registered = yield * register ( handler ) ;
191199 expect ( registered . status ) . toBe ( 201 ) ;
@@ -308,6 +316,70 @@ it.live("signs in with a pairing code and issues a token only /mcp accepts", ()
308316 ) ,
309317) ;
310318
319+ it . live ( "signs in a hosted agent with an https callback that asked for a client secret" , ( ) =>
320+ withRoutes ( ( handler , auth ) =>
321+ Effect . gen ( function * ( ) {
322+ const callbackUrl = "https://bot.example/oauth/callback" ;
323+ const registered = yield * handler (
324+ at ( "/oauth/mcp/register" , {
325+ method : "POST" ,
326+ headers : { "content-type" : "application/json" } ,
327+ body : encodeJson ( {
328+ client_name : "Hosted bot" ,
329+ redirect_uris : [ callbackUrl ] ,
330+ token_endpoint_auth_method : "client_secret_post" ,
331+ } ) ,
332+ } ) ,
333+ ) ;
334+ expect ( registered . status ) . toBe ( 201 ) ;
335+ const client = ( yield * json < unknown > ( registered ) ) as {
336+ client_id : string ;
337+ token_endpoint_auth_method : string ;
338+ } ;
339+ // Registered as a public client: PKCE, not a secret, proves it at the token endpoint.
340+ expect ( client . token_endpoint_auth_method ) . toBe ( "none" ) ;
341+
342+ const params = authorizeParams ( client . client_id , callbackUrl ) ;
343+ const details = yield * handler ( postJson ( "/oauth/mcp/approval" , params ) ) . pipe (
344+ Effect . flatMap ( json < Record < string , unknown > > ) ,
345+ ) ;
346+ expect ( details ) . toMatchObject ( { clientName : "Hosted bot" , redirectHost : "bot.example" } ) ;
347+
348+ const pairing = yield * auth . issuePairingCredential ( ) ;
349+ const approved = yield * decide ( handler , params , {
350+ _tag : "pairing-code" ,
351+ access : "approval-required" ,
352+ code : pairing . credential ,
353+ } ) ;
354+ const callback = new URL ( approved . redirectTo ! ) ;
355+ expect ( `${ callback . origin } ${ callback . pathname } ` ) . toBe ( callbackUrl ) ;
356+
357+ const tokenResponse = yield * handler (
358+ at (
359+ "/oauth/mcp/token" ,
360+ form ( {
361+ grant_type : "authorization_code" ,
362+ code : callback . searchParams . get ( "code" ) ! ,
363+ redirect_uri : callbackUrl ,
364+ client_id : client . client_id ,
365+ // Sent by a client that asked for one; there is none to check.
366+ client_secret : "unused" ,
367+ code_verifier : verifier ,
368+ } ) ,
369+ ) ,
370+ ) ;
371+ expect ( tokenResponse . status ) . toBe ( 200 ) ;
372+ const token = ( yield * json < unknown > ( tokenResponse ) ) as { access_token : string } ;
373+ const signedIn = yield * auth . authenticateMcpClient (
374+ HttpServerRequest . fromWeb (
375+ at ( "/mcp" , { headers : { authorization : `Bearer ${ token . access_token } ` } } ) ,
376+ ) ,
377+ ) ;
378+ expect ( signedIn ) . toMatchObject ( { label : "Hosted bot" , access : "approval-required" } ) ;
379+ } ) ,
380+ ) ,
381+ ) ;
382+
311383it . live ( "rejects a wrong PKCE verifier and spends the code" , ( ) =>
312384 withRoutes ( ( handler , auth ) =>
313385 Effect . gen ( function * ( ) {
@@ -489,17 +561,14 @@ it.live("one-click approves only access the browser session holds the scopes for
489561 ) ,
490562) ;
491563
492- it ( "matches loopback redirects on everything but the port" , ( ) => {
493- expect (
494- McpOAuth . loopbackRedirectMatches ( "http://localhost/callback" , "http://localhost:9/callback" ) ,
495- ) . toBe ( true ) ;
496- expect (
497- McpOAuth . loopbackRedirectMatches ( "http://127.0.0.1:1/callback" , "http://127.0.0.1:2/callback" ) ,
498- ) . toBe ( true ) ;
499- expect (
500- McpOAuth . loopbackRedirectMatches ( "http://localhost/callback" , "http://127.0.0.1/callback" ) ,
501- ) . toBe ( false ) ;
502- expect (
503- McpOAuth . loopbackRedirectMatches ( "http://localhost/callback" , "https://localhost/callback" ) ,
504- ) . toBe ( false ) ;
564+ it ( "matches loopback redirects on everything but the port, and https redirects exactly" , ( ) => {
565+ const matches = McpOAuth . redirectMatches ;
566+ expect ( matches ( "http://localhost/callback" , "http://localhost:9/callback" ) ) . toBe ( true ) ;
567+ expect ( matches ( "http://127.0.0.1:1/callback" , "http://127.0.0.1:2/callback" ) ) . toBe ( true ) ;
568+ expect ( matches ( "http://localhost/callback" , "http://127.0.0.1/callback" ) ) . toBe ( false ) ;
569+ expect ( matches ( "http://localhost/callback" , "https://localhost/callback" ) ) . toBe ( false ) ;
570+ expect ( matches ( "https://bot.example/cb" , "https://bot.example/cb" ) ) . toBe ( true ) ;
571+ expect ( matches ( "https://bot.example/cb" , "https://bot.example:8443/cb" ) ) . toBe ( false ) ;
572+ expect ( matches ( "https://bot.example/cb" , "https://bot.example/cb?x=1" ) ) . toBe ( false ) ;
573+ expect ( matches ( "https://bot.example/cb" , "https://evil.example/cb" ) ) . toBe ( false ) ;
505574} ) ;
0 commit comments