@@ -17,7 +17,8 @@ import {
1717 refreshAuthorization ,
1818 registerClient ,
1919 selectClientAuthMethod ,
20- startAuthorization
20+ startAuthorization ,
21+ validateClientMetadataUrl
2122} from '../../src/client/auth.js' ;
2223import { createPrivateKeyJwtAuth } from '../../src/client/authExtensions.js' ;
2324
@@ -1993,6 +1994,28 @@ describe('OAuth Authorization', () => {
19931994 vi . clearAllMocks ( ) ;
19941995 } ) ;
19951996
1997+ it ( 'throws OAuthError with InvalidClientMetadata before any network request when clientMetadataUrl is invalid' , async ( ) => {
1998+ const providerWithInvalidUrl : OAuthClientProvider = {
1999+ ...mockProvider ,
2000+ clientMetadataUrl : 'http://invalid.example.com/metadata'
2001+ } ;
2002+
2003+ await expect (
2004+ auth ( providerWithInvalidUrl , {
2005+ serverUrl : 'https://server.example.com'
2006+ } )
2007+ ) . rejects . toThrow ( OAuthError ) ;
2008+
2009+ await expect (
2010+ auth ( providerWithInvalidUrl , {
2011+ serverUrl : 'https://server.example.com'
2012+ } )
2013+ ) . rejects . toMatchObject ( { code : OAuthErrorCode . InvalidClientMetadata } ) ;
2014+
2015+ // Verify no fetch was called
2016+ expect ( mockFetch ) . not . toHaveBeenCalled ( ) ;
2017+ } ) ;
2018+
19962019 it ( 'performs client_credentials with private_key_jwt when provider has addClientAuthentication' , async ( ) => {
19972020 // Arrange: metadata discovery for PRM and AS
19982021 mockFetch . mockImplementation ( url => {
@@ -3406,6 +3429,85 @@ describe('OAuth Authorization', () => {
34063429 } ) ;
34073430 } ) ;
34083431
3432+ describe ( 'validateClientMetadataUrl' , ( ) => {
3433+ it ( 'passes for valid HTTPS URL with path' , ( ) => {
3434+ expect ( ( ) => validateClientMetadataUrl ( 'https://client.example.com/.well-known/oauth-client' ) ) . not . toThrow ( ) ;
3435+ } ) ;
3436+
3437+ it ( 'passes for valid HTTPS URL with multi-segment path' , ( ) => {
3438+ expect ( ( ) => validateClientMetadataUrl ( 'https://example.com/clients/metadata.json' ) ) . not . toThrow ( ) ;
3439+ } ) ;
3440+
3441+ it ( 'throws OAuthError for HTTP URL' , ( ) => {
3442+ expect ( ( ) => validateClientMetadataUrl ( 'http://client.example.com/.well-known/oauth-client' ) ) . toThrow ( OAuthError ) ;
3443+ try {
3444+ validateClientMetadataUrl ( 'http://client.example.com/.well-known/oauth-client' ) ;
3445+ } catch ( error ) {
3446+ expect ( error ) . toBeInstanceOf ( OAuthError ) ;
3447+ expect ( ( error as OAuthError ) . code ) . toBe ( OAuthErrorCode . InvalidClientMetadata ) ;
3448+ expect ( ( error as OAuthError ) . message ) . toContain ( 'http://client.example.com/.well-known/oauth-client' ) ;
3449+ }
3450+ } ) ;
3451+
3452+ it ( 'throws OAuthError for non-URL string' , ( ) => {
3453+ expect ( ( ) => validateClientMetadataUrl ( 'not-a-url' ) ) . toThrow ( OAuthError ) ;
3454+ try {
3455+ validateClientMetadataUrl ( 'not-a-url' ) ;
3456+ } catch ( error ) {
3457+ expect ( error ) . toBeInstanceOf ( OAuthError ) ;
3458+ expect ( ( error as OAuthError ) . code ) . toBe ( OAuthErrorCode . InvalidClientMetadata ) ;
3459+ expect ( ( error as OAuthError ) . message ) . toContain ( 'not-a-url' ) ;
3460+ }
3461+ } ) ;
3462+
3463+ it ( 'passes silently for empty string' , ( ) => {
3464+ // Empty string is falsy, so it passes through without throwing
3465+ expect ( ( ) => validateClientMetadataUrl ( '' ) ) . not . toThrow ( ) ;
3466+ } ) ;
3467+
3468+ it ( 'throws OAuthError for root-path HTTPS URL with trailing slash' , ( ) => {
3469+ expect ( ( ) => validateClientMetadataUrl ( 'https://client.example.com/' ) ) . toThrow ( OAuthError ) ;
3470+ try {
3471+ validateClientMetadataUrl ( 'https://client.example.com/' ) ;
3472+ } catch ( error ) {
3473+ expect ( error ) . toBeInstanceOf ( OAuthError ) ;
3474+ expect ( ( error as OAuthError ) . code ) . toBe ( OAuthErrorCode . InvalidClientMetadata ) ;
3475+ expect ( ( error as OAuthError ) . message ) . toContain ( 'https://client.example.com/' ) ;
3476+ }
3477+ } ) ;
3478+
3479+ it ( 'throws OAuthError for root-path HTTPS URL without trailing slash' , ( ) => {
3480+ expect ( ( ) => validateClientMetadataUrl ( 'https://client.example.com' ) ) . toThrow ( OAuthError ) ;
3481+ try {
3482+ validateClientMetadataUrl ( 'https://client.example.com' ) ;
3483+ } catch ( error ) {
3484+ expect ( error ) . toBeInstanceOf ( OAuthError ) ;
3485+ expect ( ( error as OAuthError ) . code ) . toBe ( OAuthErrorCode . InvalidClientMetadata ) ;
3486+ expect ( ( error as OAuthError ) . message ) . toContain ( 'https://client.example.com' ) ;
3487+ }
3488+ } ) ;
3489+
3490+ it ( 'passes silently for undefined' , ( ) => {
3491+ expect ( ( ) => validateClientMetadataUrl ( undefined ) ) . not . toThrow ( ) ;
3492+ } ) ;
3493+
3494+ it ( 'passes silently when called with no arguments' , ( ) => {
3495+ // eslint-disable-next-line unicorn/no-useless-undefined
3496+ expect ( ( ) => validateClientMetadataUrl ( undefined ) ) . not . toThrow ( ) ;
3497+ } ) ;
3498+
3499+ it ( 'error message matches expected format' , ( ) => {
3500+ try {
3501+ validateClientMetadataUrl ( 'http://example.com/path' ) ;
3502+ } catch ( error ) {
3503+ expect ( error ) . toBeInstanceOf ( OAuthError ) ;
3504+ expect ( ( error as OAuthError ) . message ) . toBe (
3505+ 'clientMetadataUrl must be a valid HTTPS URL with a non-root pathname, got: http://example.com/path'
3506+ ) ;
3507+ }
3508+ } ) ;
3509+ } ) ;
3510+
34093511 describe ( 'SEP-991: URL-based Client ID fallback logic' , ( ) => {
34103512 const validClientMetadata = {
34113513 redirect_uris : [ 'http://localhost:3000/callback' ] ,
0 commit comments