@@ -116,6 +116,8 @@ interface RpOptions {
116116 provider ?: Record < string , unknown > ;
117117 /** Extra `AuthManager` config (e.g. `audience`). */
118118 config ?: Record < string , unknown > ;
119+ /** Extra stored SAML configuration members (e.g. `idpInitiatedCallbackUrl`). */
120+ samlConfig ?: Record < string , unknown > ;
119121}
120122
121123async function makeRp ( opts : RpOptions = { } ) : Promise < Rp > {
@@ -150,6 +152,7 @@ async function makeRp(opts: RpOptions = {}): Promise<Rp> {
150152 identifierFormat : EMAIL_FORMAT ,
151153 idpMetadata : { entityID : IDP_ENTITY } ,
152154 spMetadata : { entityID : SP_ENTITY } ,
155+ ...opts . samlConfig ,
153156 } ) ,
154157 ...opts . provider ,
155158 } ,
@@ -329,10 +332,19 @@ describe('reading the landing from the relay state', () => {
329332 expect ( await readSamlAcsLanding ( ctx , PROVIDER_ID , acsPost ( 'RelayState=rs' ) ) ) . toEqual ( { success : AFTER , error : CONSOLE_LOGIN } ) ;
330333 } ) ;
331334
332- it ( 'no landing without a relay state, a row, or a readable row' , async ( ) => {
333- expect ( await readSamlAcsLanding ( context ( stateRow ( { callbackURL : AFTER } ) ) , PROVIDER_ID , acsPost ( 'SAMLResponse=x' ) ) ) . toBeUndefined ( ) ;
334- expect ( await readSamlAcsLanding ( context ( null ) , PROVIDER_ID , acsPost ( 'RelayState=rs' ) ) ) . toBeUndefined ( ) ;
335- expect ( await readSamlAcsLanding ( context ( { value : '{not json' } ) , PROVIDER_ID , acsPost ( 'RelayState=rs' ) ) ) . toBeUndefined ( ) ;
335+ it ( 'with no relay state, no row, or no readable row (IdP-initiated): the platform error page, for the bare fallback' , async ( ) => {
336+ const idpInitiatedLanding = { fallback : BASE , error : CONSOLE_LOGIN } ;
337+ expect ( await readSamlAcsLanding ( context ( stateRow ( { callbackURL : AFTER } ) ) , PROVIDER_ID , acsPost ( 'SAMLResponse=x' ) ) ) . toEqual ( idpInitiatedLanding ) ;
338+ expect ( await readSamlAcsLanding ( context ( null ) , PROVIDER_ID , acsPost ( 'RelayState=rs' ) ) ) . toEqual ( idpInitiatedLanding ) ;
339+ expect ( await readSamlAcsLanding ( context ( { value : '{not json' } ) , PROVIDER_ID , acsPost ( 'RelayState=rs' ) ) ) . toEqual ( idpInitiatedLanding ) ;
340+ expect ( await readSamlAcsLanding ( context ( stateRow ( { errorURL : FAILED } ) ) , PROVIDER_ID , acsPost ( 'RelayState=rs' ) ) ) . toEqual ( idpInitiatedLanding ) ;
341+ } ) ;
342+
343+ it ( 'with no relay-state row and no platform error page configured, the vendor default; an unsafe one, no landing' , async ( ) => {
344+ const bare = { ...context ( null ) , options : { } } ;
345+ expect ( await readSamlAcsLanding ( bare , PROVIDER_ID , acsPost ( 'SAMLResponse=x' ) ) ) . toEqual ( { fallback : BASE , error : `${ AUTH } /error` } ) ;
346+ const elsewhere = { ...context ( null ) , options : { onAPIError : { errorURL : 'https://elsewhere.example/login' } } } ;
347+ expect ( await readSamlAcsLanding ( elsewhere , PROVIDER_ID , acsPost ( 'SAMLResponse=x' ) ) ) . toBeUndefined ( ) ;
336348 } ) ;
337349
338350 it ( 'applies the vendor redirect safety rule to the error URL' , async ( ) => {
@@ -375,6 +387,31 @@ describe('putting an ACS answer on its landing', () => {
375387 expect ( landSamlRefusal ( json , landingOf ) ) . toBe ( json ) ;
376388 } ) ;
377389
390+ it ( 'IdP-initiated: a refusal on the bare app-origin fallback is re-based onto the platform error page, every header kept' , ( ) => {
391+ const idp = { fallback : BASE , error : CONSOLE_LOGIN } ;
392+ const res = landSamlRefusal ( redirect ( `${ BASE } /?error=unsolicited_response&error_description=IdP-initiated+SSO+not+allowed` ) , idp ) ;
393+ expect ( res . status ) . toBe ( 302 ) ;
394+ expect ( res . headers . get ( 'location' ) ) . toBe ( `${ CONSOLE_LOGIN } ?error=unsolicited_response&error_description=IdP-initiated+SSO+not+allowed` ) ;
395+ expect ( res . headers . get ( 'set-cookie' ) ) . toBe ( 'a=1' ) ;
396+ expect ( landSamlRefusal ( redirect ( '/?error=account_not_linked' ) , idp ) . headers . get ( 'location' ) ) . toBe ( `${ CONSOLE_LOGIN } ?error=account_not_linked` ) ;
397+ } ) ;
398+
399+ it ( 'IdP-initiated: a success on the fallback, and anything not on the bare fallback, are left alone' , ( ) => {
400+ const idp = { fallback : BASE , error : CONSOLE_LOGIN } ;
401+ for ( const location of [
402+ BASE , // the vendor's success on its fallback
403+ `${ BASE } /` ,
404+ `${ BASE } /sso-landing?error=unsolicited_response` , // a configured IdP-initiated callback
405+ `${ BASE } /?tenant=acme&error=unsolicited_response` , // the root, with a query of its own
406+ `${ BASE } /?error=unsolicited_response#top` ,
407+ `https://elsewhere.example/?error=unsolicited_response` ,
408+ `//elsewhere.example/?error=unsolicited_response` ,
409+ ] ) {
410+ const res = redirect ( location ) ;
411+ expect ( landSamlRefusal ( res , idp ) , location ) . toBe ( res ) ;
412+ }
413+ } ) ;
414+
378415 it ( 'with no landing read, the vendor answer stands' , async ( ) => {
379416 const vendor = redirect ( `${ AFTER } ?error=account_not_linked` ) ;
380417 const saml = { protocol : 'saml' as const , providerId : PROVIDER_ID , request : new Request ( ACS , { method : 'POST' } ) } ;
@@ -393,7 +430,11 @@ const seedAdmin = async (rp: Rp) => {
393430} ;
394431
395432/** An IdP-initiated Response: the IdP posts an assertion no AuthnRequest asked for (no InResponseTo, no RelayState). */
396- async function idpInitiated ( rp : Rp , email : string ) : Promise < Response > {
433+ async function idpInitiated (
434+ rp : Rp ,
435+ email : string ,
436+ shapeResponse : ( samlResponse : string ) => string = ( r ) => r ,
437+ ) : Promise < Response > {
397438 const spMetadata = await ( await rp . manager . handleRequest (
398439 new Request ( `${ AUTH } /sso/saml2/sp/metadata?providerId=${ PROVIDER_ID } ` ) ,
399440 ) ) . text ( ) ;
@@ -411,19 +452,20 @@ async function idpInitiated(rp: Rp, email: string): Promise<Response> {
411452 new Request ( ACS , {
412453 method : 'POST' ,
413454 headers : { 'content-type' : 'application/x-www-form-urlencoded' } ,
414- body : new URLSearchParams ( { SAMLResponse : context } ) . toString ( ) ,
455+ body : new URLSearchParams ( { SAMLResponse : shapeResponse ( context ) } ) . toString ( ) ,
415456 } ) ,
416457 ) ;
417458}
418459
419460describe ( 'SAML IdP-initiated sign-in is a per-provider toggle, off by default' , ( ) => {
420- it ( 'off (the default): an unsolicited Response is refused 302 error=unsolicited_response, and signs nobody in' , async ( ) => {
461+ it ( 'off (the default): an unsolicited Response is refused 302 error=unsolicited_response on the platform error page , and signs nobody in' , async ( ) => {
421462 const rp = await makeRp ( { provider : { allowJit : true } } ) ;
422463 await seedAdmin ( rp ) ;
423464 const res = await idpInitiated ( rp , 'new.person@acme.com' ) ;
424465 expect ( res . status ) . toBe ( 302 ) ;
425- expect ( landing ( res ) . error ) . toBe ( 'unsolicited_response' ) ;
466+ expect ( landing ( res ) ) . toEqual ( { base : CONSOLE_LOGIN , error : 'unsolicited_response' } ) ;
426467 expect ( cookiesOf ( res ) ) . not . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
468+ expect ( rp . engine . tables . get ( 'sys_user' ) ?. map ( ( u : any ) => u . email ) ) . toEqual ( [ 'admin@acme.com' ] ) ;
427469 } ) ;
428470
429471 it ( 'on: the same Response signs the person in' , async ( ) => {
@@ -445,7 +487,8 @@ describe('SAML IdP-initiated sign-in is a per-provider toggle, off by default',
445487 data : { providerId : 'other-idp' , issuer : 'https://other.example/entity' , domain : 'other.example' , allowIdpInitiated : true } ,
446488 } ) ;
447489 const res = await idpInitiated ( rp , 'new.person@acme.com' ) ;
448- expect ( landing ( res ) . error ) . toBe ( 'unsolicited_response' ) ;
490+ expect ( landing ( res ) ) . toEqual ( { base : CONSOLE_LOGIN , error : 'unsolicited_response' } ) ;
491+ expect ( cookiesOf ( res ) ) . not . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
449492 } ) ;
450493
451494 it ( 'CONTROL: an SP-initiated sign-in is answered as before with the toggle off' , async ( ) => {
@@ -457,6 +500,57 @@ describe('SAML IdP-initiated sign-in is a per-provider toggle, off by default',
457500 } ) ;
458501} ) ;
459502
503+ describe ( 'a refused IdP-initiated SAML sign-in lands on the platform error page, as one with no errorCallbackURL does' , ( ) => {
504+ // With no relay state the vendor lands every IdP-initiated answer on its
505+ // fallback, the bare app origin, whose console redirect drops the query.
506+
507+ it ( 'on, but the person may not be provisioned: error=SELF_REGISTRATION_CLOSED on the platform error page, no user created' , async ( ) => {
508+ const rp = await makeRp ( { provider : { allowIdpInitiated : true } } ) ;
509+ await seedAdmin ( rp ) ;
510+ const res = await idpInitiated ( rp , 'new.person@acme.com' ) ;
511+ expect ( res . status ) . toBe ( 302 ) ;
512+ expect ( landing ( res ) ) . toEqual ( { base : CONSOLE_LOGIN , error : 'SELF_REGISTRATION_CLOSED' } ) ;
513+ expect ( cookiesOf ( res ) ) . not . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
514+ expect ( rp . engine . tables . get ( 'sys_user' ) ?. map ( ( u : any ) => u . email ) ) . toEqual ( [ 'admin@acme.com' ] ) ;
515+ } ) ;
516+
517+ it ( 'on, and the email belongs to an account this provider is not linked to: error=account_not_linked on the platform error page' , async ( ) => {
518+ const rp = await makeRp ( { provider : { allowIdpInitiated : true , allowJit : true } } ) ;
519+ await rp . engine . insert ( 'sys_user' , { id : 'usr_known' , email : KNOWN_EMAIL , name : 'Mia Chen' , email_verified : true } ) ;
520+ const res = await idpInitiated ( rp , KNOWN_EMAIL ) ;
521+ expect ( res . status ) . toBe ( 302 ) ;
522+ expect ( landing ( res ) ) . toEqual ( { base : CONSOLE_LOGIN , error : 'account_not_linked' } ) ;
523+ expect ( cookiesOf ( res ) ) . not . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
524+ } ) ;
525+
526+ it ( "a Response the vendor's signature check refuses: error=saml_error on the platform error page" , async ( ) => {
527+ const rp = await makeRp ( { provider : { allowIdpInitiated : true , allowJit : true } } ) ;
528+ await seedAdmin ( rp ) ;
529+ const res = await idpInitiated ( rp , 'someone@acme.com' , tamper ) ;
530+ expect ( res . status ) . toBe ( 302 ) ;
531+ expect ( landing ( res ) ) . toEqual ( { base : CONSOLE_LOGIN , error : 'saml_error' } ) ;
532+ expect ( cookiesOf ( res ) ) . not . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
533+ } ) ;
534+
535+ it ( 'CONTROL: an allowed IdP-initiated sign-in still lands on the app origin, signed in' , async ( ) => {
536+ const rp = await makeRp ( { provider : { allowIdpInitiated : true , allowJit : true } } ) ;
537+ await seedAdmin ( rp ) ;
538+ const res = await idpInitiated ( rp , 'new.person@acme.com' ) ;
539+ expect ( res . status ) . toBe ( 302 ) ;
540+ expect ( res . headers . get ( 'location' ) ) . toBe ( BASE ) ;
541+ expect ( cookiesOf ( res ) ) . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
542+ } ) ;
543+
544+ it ( "CONTROL: a provider's own IdP-initiated callback keeps its refusals, as the operator configured" , async ( ) => {
545+ const rp = await makeRp ( { provider : { allowJit : true } , samlConfig : { idpInitiatedCallbackUrl : `${ BASE } /sso-landing` } } ) ;
546+ await seedAdmin ( rp ) ;
547+ const res = await idpInitiated ( rp , 'new.person@acme.com' ) ;
548+ expect ( res . status ) . toBe ( 302 ) ;
549+ expect ( landing ( res ) ) . toEqual ( { base : `${ BASE } /sso-landing` , error : 'unsolicited_response' } ) ;
550+ expect ( cookiesOf ( res ) ) . not . toMatch ( / s e s s i o n _ t o k e n = [ ^ ; ] / ) ;
551+ } ) ;
552+ } ) ;
553+
460554describe ( 'invite_only refuses an unknown SAML user unless the provider allows JIT' , ( ) => {
461555 it ( 'invite_only (the default) + no account + no invitation: refused 302 error=SELF_REGISTRATION_CLOSED on the error URL, no user created' , async ( ) => {
462556 const rp = await makeRp ( ) ;
0 commit comments