Skip to content

Commit c748ffa

Browse files
committed
fix: throw error on auth fallback for non-root AS paths
Fixes #1716 When authorization server metadata discovery fails and the server URL has a non-root path, the fallback to /authorize, /token, and /register endpoints silently constructs wrong URLs (losing the path prefix). This fix throws a descriptive error instead of silently redirecting to nonexistent endpoints. Affected locations: - startAuthorization: /authorize fallback - executeTokenRequest: /token fallback - registerClient: /register fallback
1 parent ccb78f2 commit c748ffa

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

packages/client/src/client/auth.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,11 @@ export async function startAuthorization(
12001200
) {
12011201
throw new Error(`Incompatible auth server: does not support code challenge method ${AUTHORIZATION_CODE_CHALLENGE_METHOD}`);
12021202
}
1203+
} else if (authorizationServerUrl.pathname !== '/') {
1204+
throw new Error(
1205+
`Authorization server metadata discovery failed and the server URL (${authorizationServerUrl}) has a non-root path. ` +
1206+
`Cannot determine the authorization endpoint. Please ensure the authorization server is reachable and supports metadata discovery.`
1207+
);
12031208
} else {
12041209
authorizationUrl = new URL('/authorize', authorizationServerUrl);
12051210
}
@@ -1283,7 +1288,14 @@ async function executeTokenRequest(
12831288
fetchFn?: FetchLike;
12841289
}
12851290
): Promise<OAuthTokens> {
1286-
const tokenUrl = metadata?.token_endpoint ? new URL(metadata.token_endpoint) : new URL('/token', authorizationServerUrl);
1291+
const tokenUrl = metadata?.token_endpoint
1292+
? new URL(metadata.token_endpoint)
1293+
: authorizationServerUrl.pathname !== '/'
1294+
? (() => { throw new Error(
1295+
`Authorization server metadata discovery failed and the server URL (${authorizationServerUrl}) has a non-root path. ` +
1296+
`Cannot determine the token endpoint.`
1297+
); })()
1298+
: new URL('/token', authorizationServerUrl);
12871299

12881300
const headers = new Headers({
12891301
'Content-Type': 'application/x-www-form-urlencoded',
@@ -1530,6 +1542,12 @@ export async function registerClient(
15301542

15311543
registrationUrl = new URL(metadata.registration_endpoint);
15321544
} else {
1545+
if (authorizationServerUrl.pathname !== '/') {
1546+
throw new Error(
1547+
`Authorization server metadata discovery failed and the server URL (${authorizationServerUrl}) has a non-root path. ` +
1548+
`Cannot determine the registration endpoint.`
1549+
);
1550+
}
15331551
registrationUrl = new URL('/register', authorizationServerUrl);
15341552
}
15351553

0 commit comments

Comments
 (0)