@cloudflare/workers-oauth-provider 0.0.0-d18b865 → 0.0.0-f8eaa3e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -876,6 +876,9 @@ var OAuthProviderImpl = class {
876
876
  if (!redirectUris || redirectUris.length === 0) {
877
877
  throw new Error("At least one redirect URI is required");
878
878
  }
879
+ for (const uri of redirectUris) {
880
+ validateRedirectUriScheme(uri);
881
+ }
879
882
  clientInfo = {
880
883
  clientId,
881
884
  redirectUris,
@@ -1052,7 +1055,7 @@ async function hashSecret(secret) {
1052
1055
  return generateTokenId(secret);
1053
1056
  }
1054
1057
  function generateRandomString(length) {
1055
- const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1058
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
1056
1059
  let result = "";
1057
1060
  const values = new Uint8Array(length);
1058
1061
  crypto.getRandomValues(values);
@@ -1069,6 +1072,26 @@ async function generateTokenId(token) {
1069
1072
  const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
1070
1073
  return hashHex;
1071
1074
  }
1075
+ function validateRedirectUriScheme(redirectUri) {
1076
+ const dangerousSchemes = ["javascript:", "data:", "vbscript:", "file:", "mailto:", "blob:"];
1077
+ const normalized = redirectUri.trim();
1078
+ for (let i = 0; i < normalized.length; i++) {
1079
+ const code = normalized.charCodeAt(i);
1080
+ if (code >= 0 && code <= 31 || code >= 127 && code <= 159) {
1081
+ throw new Error("Invalid redirect URI");
1082
+ }
1083
+ }
1084
+ const colonIndex = normalized.indexOf(":");
1085
+ if (colonIndex === -1) {
1086
+ throw new Error("Invalid redirect URI");
1087
+ }
1088
+ const scheme = normalized.substring(0, colonIndex + 1).toLowerCase();
1089
+ for (const dangerousScheme of dangerousSchemes) {
1090
+ if (scheme === dangerousScheme) {
1091
+ throw new Error("Invalid redirect URI");
1092
+ }
1093
+ }
1094
+ }
1072
1095
  function base64UrlEncode(str) {
1073
1096
  return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
1074
1097
  }
@@ -1221,9 +1244,7 @@ var OAuthHelpersImpl = class {
1221
1244
  const state = url.searchParams.get("state") || "";
1222
1245
  const codeChallenge = url.searchParams.get("code_challenge") || void 0;
1223
1246
  const codeChallengeMethod = url.searchParams.get("code_challenge_method") || "plain";
1224
- if (!redirectUri.startsWith("http://") && !redirectUri.startsWith("https://")) {
1225
- throw new Error("Invalid redirect URI");
1226
- }
1247
+ validateRedirectUriScheme(redirectUri);
1227
1248
  if (responseType === "token" && !this.provider.options.allowImplicitFlow) {
1228
1249
  throw new Error("The implicit grant flow is not enabled for this provider");
1229
1250
  }
@@ -1266,6 +1287,16 @@ var OAuthHelpersImpl = class {
1266
1287
  * @returns A Promise resolving to an object containing the redirect URL
1267
1288
  */
1268
1289
  async completeAuthorization(options) {
1290
+ const { clientId, redirectUri } = options.request;
1291
+ if (!clientId || !redirectUri) {
1292
+ throw new Error("Client ID and Redirect URI are required in the authorization request.");
1293
+ }
1294
+ const clientInfo = await this.lookupClient(clientId);
1295
+ if (!clientInfo || !clientInfo.redirectUris.includes(redirectUri)) {
1296
+ throw new Error(
1297
+ "Invalid redirect URI. The redirect URI provided does not match any registered URI for this client."
1298
+ );
1299
+ }
1269
1300
  const grantId = generateRandomString(16);
1270
1301
  const { encryptedData, key: encryptionKey } = await encryptProps(options.props);
1271
1302
  const now = Math.floor(Date.now() / 1e3);
@@ -1372,6 +1403,9 @@ var OAuthHelpersImpl = class {
1372
1403
  registrationDate: Math.floor(Date.now() / 1e3),
1373
1404
  tokenEndpointAuthMethod
1374
1405
  };
1406
+ for (const uri of newClient.redirectUris) {
1407
+ validateRedirectUriScheme(uri);
1408
+ }
1375
1409
  let clientSecret;
1376
1410
  if (!isPublicClient) {
1377
1411
  clientSecret = generateRandomString(32);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/workers-oauth-provider",
3
- "version": "0.0.0-d18b865",
3
+ "version": "0.0.0-f8eaa3e",
4
4
  "description": "OAuth provider for Cloudflare Workers",
5
5
  "main": "dist/oauth-provider.js",
6
6
  "types": "dist/oauth-provider.d.ts",