@@ -12,7 +12,7 @@ import {
12
12
OpenIdProviderDiscoveryMetadataSchema
13
13
} from "../shared/auth.js" ;
14
14
import { OAuthClientInformationFullSchema , OAuthMetadataSchema , OAuthProtectedResourceMetadataSchema , OAuthTokensSchema } from "../shared/auth.js" ;
15
- import { checkResourceAllowed , resourceUrlFromServerUrl , isValidOAuthScheme } from "../shared/auth-utils.js" ;
15
+ import { checkResourceAllowed , resourceUrlFromServerUrl , isAuthorizationEndpointSafe } from "../shared/auth-utils.js" ;
16
16
import {
17
17
InvalidClientError ,
18
18
InvalidGrantError ,
@@ -774,10 +774,19 @@ export async function discoverAuthorizationServerMetadata(
774
774
}
775
775
776
776
// Parse and validate based on type
777
+ const responseData = await response . json ( ) ;
778
+
777
779
if ( type === 'oauth' ) {
778
- return OAuthMetadataSchema . parse ( await response . json ( ) ) ;
780
+ const metadata = OAuthMetadataSchema . parse ( responseData ) ;
781
+ if ( ! isAuthorizationEndpointSafe ( metadata ) ) {
782
+ throw new Error ( `Invalid OAuth metadata from ${ endpointUrl } : authorization_endpoint uses javascript: scheme which is not allowed for security reasons.` ) ;
783
+ }
784
+ return metadata ;
779
785
} else {
780
- const metadata = OpenIdProviderDiscoveryMetadataSchema . parse ( await response . json ( ) ) ;
786
+ const metadata = OpenIdProviderDiscoveryMetadataSchema . parse ( responseData ) ;
787
+ if ( ! isAuthorizationEndpointSafe ( metadata ) ) {
788
+ throw new Error ( `Invalid OIDC metadata from ${ endpointUrl } : authorization_endpoint uses javascript: scheme which is not allowed for security reasons.` ) ;
789
+ }
781
790
782
791
// MCP spec requires OIDC providers to support S256 PKCE
783
792
if ( ! metadata . code_challenge_methods_supported ?. includes ( 'S256' ) ) {
@@ -820,9 +829,6 @@ export async function startAuthorization(
820
829
let authorizationUrl : URL ;
821
830
if ( metadata ) {
822
831
authorizationUrl = new URL ( metadata . authorization_endpoint ) ;
823
- if ( ! isValidOAuthScheme ( authorizationUrl ) ) {
824
- throw new Error ( `Invalid authorization_endpoint URL scheme: ${ authorizationUrl . protocol } . Only http: and https: are allowed.` ) ;
825
- }
826
832
827
833
if ( ! metadata . response_types_supported . includes ( responseType ) ) {
828
834
throw new Error (
@@ -914,15 +920,9 @@ export async function exchangeAuthorization(
914
920
) : Promise < OAuthTokens > {
915
921
const grantType = "authorization_code" ;
916
922
917
- let tokenUrl : URL ;
918
- if ( metadata ?. token_endpoint ) {
919
- tokenUrl = new URL ( metadata . token_endpoint ) ;
920
- if ( ! isValidOAuthScheme ( tokenUrl ) ) {
921
- throw new Error ( `Invalid token_endpoint URL scheme: ${ tokenUrl . protocol } . Only http: and https: are allowed.` ) ;
922
- }
923
- } else {
924
- tokenUrl = new URL ( "/token" , authorizationServerUrl ) ;
925
- }
923
+ const tokenUrl = metadata ?. token_endpoint
924
+ ? new URL ( metadata . token_endpoint )
925
+ : new URL ( "/token" , authorizationServerUrl ) ;
926
926
927
927
if (
928
928
metadata ?. grant_types_supported &&
@@ -1007,9 +1007,6 @@ export async function refreshAuthorization(
1007
1007
let tokenUrl : URL ;
1008
1008
if ( metadata ) {
1009
1009
tokenUrl = new URL ( metadata . token_endpoint ) ;
1010
- if ( ! isValidOAuthScheme ( tokenUrl ) ) {
1011
- throw new Error ( `Invalid token_endpoint URL scheme: ${ tokenUrl . protocol } . Only http: and https: are allowed.` ) ;
1012
- }
1013
1010
1014
1011
if (
1015
1012
metadata . grant_types_supported &&
@@ -1081,9 +1078,6 @@ export async function registerClient(
1081
1078
}
1082
1079
1083
1080
registrationUrl = new URL ( metadata . registration_endpoint ) ;
1084
- if ( ! isValidOAuthScheme ( registrationUrl ) ) {
1085
- throw new Error ( `Invalid registration_endpoint URL scheme: ${ registrationUrl . protocol } . Only http: and https: are allowed.` ) ;
1086
- }
1087
1081
} else {
1088
1082
registrationUrl = new URL ( "/register" , authorizationServerUrl ) ;
1089
1083
}
0 commit comments