Skip to content

fix(auth): prevent javascript url injection in oauth endpoints #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
OpenIdProviderDiscoveryMetadataSchema
} from "../shared/auth.js";
import { OAuthClientInformationFullSchema, OAuthMetadataSchema, OAuthProtectedResourceMetadataSchema, OAuthTokensSchema } from "../shared/auth.js";
import { checkResourceAllowed, resourceUrlFromServerUrl } from "../shared/auth-utils.js";
import { checkResourceAllowed, resourceUrlFromServerUrl, isValidOAuthScheme } from "../shared/auth-utils.js";
import {
InvalidClientError,
InvalidGrantError,
Expand Down Expand Up @@ -820,6 +820,9 @@ export async function startAuthorization(
let authorizationUrl: URL;
if (metadata) {
authorizationUrl = new URL(metadata.authorization_endpoint);
if (!isValidOAuthScheme(authorizationUrl)) {
throw new Error(`Invalid authorization_endpoint URL scheme: ${authorizationUrl.protocol}. Only http: and https: are allowed.`);
}

if (!metadata.response_types_supported.includes(responseType)) {
throw new Error(
Expand Down Expand Up @@ -911,9 +914,15 @@ export async function exchangeAuthorization(
): Promise<OAuthTokens> {
const grantType = "authorization_code";

const tokenUrl = metadata?.token_endpoint
? new URL(metadata.token_endpoint)
: new URL("/token", authorizationServerUrl);
let tokenUrl: URL;
if (metadata?.token_endpoint) {
tokenUrl = new URL(metadata.token_endpoint);
if (!isValidOAuthScheme(tokenUrl)) {
throw new Error(`Invalid token_endpoint URL scheme: ${tokenUrl.protocol}. Only http: and https: are allowed.`);
}
} else {
tokenUrl = new URL("/token", authorizationServerUrl);
}

if (
metadata?.grant_types_supported &&
Expand Down Expand Up @@ -998,6 +1007,9 @@ export async function refreshAuthorization(
let tokenUrl: URL;
if (metadata) {
tokenUrl = new URL(metadata.token_endpoint);
if (!isValidOAuthScheme(tokenUrl)) {
throw new Error(`Invalid token_endpoint URL scheme: ${tokenUrl.protocol}. Only http: and https: are allowed.`);
}

if (
metadata.grant_types_supported &&
Expand Down Expand Up @@ -1069,6 +1081,9 @@ export async function registerClient(
}

registrationUrl = new URL(metadata.registration_endpoint);
if (!isValidOAuthScheme(registrationUrl)) {
throw new Error(`Invalid registration_endpoint URL scheme: ${registrationUrl.protocol}. Only http: and https: are allowed.`);
}
} else {
registrationUrl = new URL("/register", authorizationServerUrl);
}
Expand Down
16 changes: 15 additions & 1 deletion src/shared/auth-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resourceUrlFromServerUrl, checkResourceAllowed } from './auth-utils.js';
import { resourceUrlFromServerUrl, checkResourceAllowed, isValidOAuthScheme } from './auth-utils.js';

describe('auth-utils', () => {
describe('resourceUrlFromServerUrl', () => {
Expand Down Expand Up @@ -58,4 +58,18 @@ describe('auth-utils', () => {
expect(checkResourceAllowed({ requestedResource: 'https://example.com/folder', configuredResource: 'https://example.com/folder/' })).toBe(false);
});
});

describe('isValidOAuthScheme', () => {
it('should accept http and https URLs', () => {
expect(isValidOAuthScheme(new URL('https://auth.example.com/oauth'))).toBe(true);
expect(isValidOAuthScheme(new URL('http://localhost:8080/token'))).toBe(true);
});

it('should reject dangerous schemes', () => {
expect(isValidOAuthScheme(new URL('javascript:alert("XSS")'))).toBe(false);
expect(isValidOAuthScheme(new URL('data:text/html,<script>alert("XSS")</script>'))).toBe(false);
expect(isValidOAuthScheme(new URL('file:///etc/passwd'))).toBe(false);
expect(isValidOAuthScheme(new URL('ftp://malicious.com/file'))).toBe(false);
});
});
});
17 changes: 16 additions & 1 deletion src/shared/auth-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Utilities for handling OAuth resource URIs.
* Utilities for handling OAuth resource URIs and security validation.
*/

/**
Expand Down Expand Up @@ -52,3 +52,18 @@ export function resourceUrlFromServerUrl(url: URL | string ): URL {

return requestedPath.startsWith(configuredPath);
}

/**
* Validates that a URL uses a safe scheme for OAuth endpoints (http or https only).
*
* This prevents XSS (Cross-Site Scripting) and RCE (Remote Code Execution) attacks
* where malicious authorization servers could return endpoints with dangerous schemes
* like javascript:, data:, file:, etc. that could lead to code execution when
* processed by OAuth clients.
*
* @param url - The URL to validate
* @returns true if the URL scheme is safe (http: or https:), false otherwise
*/
export function isValidOAuthScheme(url: URL): boolean {
return ['https:', 'http:'].includes(url.protocol);
}