@vitemcp/server 1.6.2 → 1.6.4

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.
@@ -2318,31 +2318,54 @@ var OAuthProxy = class {
2318
2318
  return response;
2319
2319
  }
2320
2320
  /**
2321
- * Match a URI against an allow-list glob: `*` spans any run of characters,
2322
- * `?` exactly one, and everything else is literal.
2321
+ * Match a parsed URI against an allow-list pattern, one URI component at a
2322
+ * time: scheme, host, port, then everything from the path onwards. A `*`
2323
+ * therefore stops where its component stops.
2323
2324
  *
2324
- * "Everything else is literal" is the security-relevant half. Left unescaped,
2325
- * a `.` reaches the RegExp as "any character", so the pattern an operator
2326
- * wrote for one host — `https://client.example.com/*` — also admits
2327
- * `https://clientXexampleYcom/steal`, a name an attacker can register. DCR
2328
- * would accept it and `handleCallback()` would later 302 a fresh
2329
- * authorization code to it, reopening the CWE-601 path this allow-list
2330
- * exists to close. `+ ^ $ ( ) [ ] { } |` widen a pattern the same way.
2325
+ * That containment is the whole point. Matching the pattern against the raw
2326
+ * string let a wildcard run straight through the delimiters that decide
2327
+ * where the browser actually navigates:
2331
2328
  *
2332
- * Escaping and wildcard expansion happen in one pass, so a backslash this
2333
- * inserts can never be re-read as the metacharacter of a later pass.
2329
+ * - `http://localhost:*` matched `http://localhost:@evil.com/cb`, where
2330
+ * `localhost:` is userinfo and the real host is `evil.com`.
2331
+ * - `https://*.example.com/*` matched
2332
+ * `https://evil.com/a.example.com/cb`, because the wildcard covered
2333
+ * `evil.com/a` — authority and path at once.
2334
+ *
2335
+ * Both register cleanly under the old matcher, and `handleCallback()` would
2336
+ * then 302 a fresh authorization code to the attacker's host: the CWE-601
2337
+ * theft path this allow-list exists to close. Comparing against `URL`'s
2338
+ * parse makes the delimiters structural rather than incidental, so no
2339
+ * pattern can span them however it is written.
2340
+ *
2341
+ * A pattern that omits the path (`http://localhost:*`, the loopback default)
2342
+ * constrains scheme, host and port only — an ephemeral client chooses its
2343
+ * own callback path. Write a path to pin one.
2334
2344
  */
2335
2345
  matchesPattern(uri, pattern) {
2336
- const source = pattern.replace(/[.*+?^${}()|[\]\\]/g, (character) => {
2337
- if (character === "*") {
2338
- return ".*";
2346
+ const parsed = parseUriPattern(pattern);
2347
+ if (!parsed) {
2348
+ return false;
2349
+ }
2350
+ if (uri.protocol.slice(0, -1) !== parsed.scheme) {
2351
+ return false;
2352
+ }
2353
+ if (parsed.authority) {
2354
+ if (!globComponent(parsed.authority.host).test(uri.hostname)) {
2355
+ return false;
2339
2356
  }
2340
- if (character === "?") {
2341
- return ".";
2357
+ if (!globComponent(parsed.authority.port).test(uri.port)) {
2358
+ return false;
2342
2359
  }
2343
- return `\\${character}`;
2344
- });
2345
- return new RegExp(`^${source}$`).test(uri);
2360
+ } else if (uri.host !== "") {
2361
+ return false;
2362
+ }
2363
+ if (parsed.rest === "") {
2364
+ return true;
2365
+ }
2366
+ return globComponent(parsed.rest).test(
2367
+ `${uri.pathname}${uri.search}${uri.hash}`
2368
+ );
2346
2369
  }
2347
2370
  /**
2348
2371
  * Parse token response that can be either JSON or URL-encoded
@@ -2566,11 +2589,15 @@ var OAuthProxy = class {
2566
2589
  * theft. Do not loosen the default beyond loopback addresses.
2567
2590
  */
2568
2591
  validateRedirectUri(uri) {
2592
+ let url;
2569
2593
  try {
2570
- new URL(uri);
2594
+ url = new URL(uri);
2571
2595
  } catch {
2572
2596
  return false;
2573
2597
  }
2598
+ if (url.username !== "" || url.password !== "") {
2599
+ return false;
2600
+ }
2574
2601
  const patterns = this.config.allowedRedirectUriPatterns;
2575
2602
  if (Array.isArray(patterns) && patterns.length === 0) {
2576
2603
  return false;
@@ -2580,10 +2607,49 @@ var OAuthProxy = class {
2580
2607
  "http://127.0.0.1:*"
2581
2608
  ];
2582
2609
  return effectivePatterns.some(
2583
- (pattern) => this.matchesPattern(uri, pattern)
2610
+ (pattern) => this.matchesPattern(url, pattern)
2584
2611
  );
2585
2612
  }
2586
2613
  };
2614
+ var globComponent = (glob) => {
2615
+ const source = glob.replace(/[.*+?^${}()|[\]\\]/g, (character) => {
2616
+ if (character === "*") {
2617
+ return ".*";
2618
+ }
2619
+ if (character === "?") {
2620
+ return ".";
2621
+ }
2622
+ return `\\${character}`;
2623
+ });
2624
+ return new RegExp(`^${source}$`);
2625
+ };
2626
+ var parseUriPattern = (pattern) => {
2627
+ const schemeEnd = pattern.indexOf(":");
2628
+ if (schemeEnd <= 0) {
2629
+ return null;
2630
+ }
2631
+ const scheme = pattern.slice(0, schemeEnd).toLowerCase();
2632
+ const afterScheme = pattern.slice(schemeEnd + 1);
2633
+ if (!afterScheme.startsWith("//")) {
2634
+ return { authority: null, rest: afterScheme, scheme };
2635
+ }
2636
+ const hierarchical = afterScheme.slice(2);
2637
+ const restStart = hierarchical.search(/[#/?]/);
2638
+ const authority = restStart === -1 ? hierarchical : hierarchical.slice(0, restStart);
2639
+ if (authority.includes("@")) {
2640
+ return null;
2641
+ }
2642
+ const hostEnd = authority.startsWith("[") ? authority.indexOf("]") + 1 : 0;
2643
+ const portSeparator = authority.indexOf(":", hostEnd);
2644
+ return {
2645
+ authority: {
2646
+ host: (portSeparator === -1 ? authority : authority.slice(0, portSeparator)).toLowerCase(),
2647
+ port: portSeparator === -1 ? "" : authority.slice(portSeparator + 1)
2648
+ },
2649
+ rest: restStart === -1 ? "" : hierarchical.slice(restStart),
2650
+ scheme
2651
+ };
2652
+ };
2587
2653
  var isLoopbackRedirectUri = (uri) => {
2588
2654
  try {
2589
2655
  const url = new URL(uri);
@@ -2878,6 +2944,7 @@ import {
2878
2944
  } from "fs/promises";
2879
2945
  import { join } from "path";
2880
2946
  var DiskStore = class {
2947
+ cleanupInFlight = null;
2881
2948
  cleanupInterval = null;
2882
2949
  directory;
2883
2950
  fileExtension;
@@ -2891,35 +2958,19 @@ var DiskStore = class {
2891
2958
  }, cleanupIntervalMs);
2892
2959
  }
2893
2960
  /**
2894
- * Clean up expired entries
2961
+ * Clean up expired entries.
2962
+ *
2963
+ * A sweep walks the whole directory and reads every entry, so on a large
2964
+ * store it can outlast the cleanup interval. Callers arriving while one is
2965
+ * running — the timer below, the OAuth proxy's own cleanup timer, or a
2966
+ * direct call — join the sweep in flight instead of starting a second walk
2967
+ * over the same files, which would only make a slow sweep slower.
2895
2968
  */
2896
2969
  async cleanup() {
2897
- try {
2898
- await this.ensureDirectory();
2899
- const files = await readdir(this.directory);
2900
- const now = Date.now();
2901
- for (const file of files) {
2902
- if (!file.endsWith(this.fileExtension)) {
2903
- continue;
2904
- }
2905
- try {
2906
- const filePath = join(this.directory, file);
2907
- const content = await readFile(filePath, "utf-8");
2908
- const entry = JSON.parse(content);
2909
- if (entry.expiresAt < now) {
2910
- await rm(filePath);
2911
- }
2912
- } catch (error) {
2913
- console.warn(`Failed to read/parse file ${file}, deleting:`, error);
2914
- try {
2915
- await rm(join(this.directory, file));
2916
- } catch {
2917
- }
2918
- }
2919
- }
2920
- } catch (error) {
2921
- console.error("Cleanup failed:", error);
2922
- }
2970
+ this.cleanupInFlight ??= this.runCleanup().finally(() => {
2971
+ this.cleanupInFlight = null;
2972
+ });
2973
+ return this.cleanupInFlight;
2923
2974
  }
2924
2975
  /**
2925
2976
  * Delete a value
@@ -3051,6 +3102,38 @@ var DiskStore = class {
3051
3102
  const sanitizedKey = key.replace(/[^a-zA-Z0-9_-]/g, "_");
3052
3103
  return join(this.directory, `${sanitizedKey}${this.fileExtension}`);
3053
3104
  }
3105
+ /**
3106
+ * One directory sweep. Always call through {@link cleanup}, which keeps
3107
+ * concurrent sweeps from overlapping.
3108
+ */
3109
+ async runCleanup() {
3110
+ try {
3111
+ await this.ensureDirectory();
3112
+ const files = await readdir(this.directory);
3113
+ const now = Date.now();
3114
+ for (const file of files) {
3115
+ if (!file.endsWith(this.fileExtension)) {
3116
+ continue;
3117
+ }
3118
+ try {
3119
+ const filePath = join(this.directory, file);
3120
+ const content = await readFile(filePath, "utf-8");
3121
+ const entry = JSON.parse(content);
3122
+ if (entry.expiresAt < now) {
3123
+ await rm(filePath);
3124
+ }
3125
+ } catch (error) {
3126
+ console.warn(`Failed to read/parse file ${file}, deleting:`, error);
3127
+ try {
3128
+ await rm(join(this.directory, file));
3129
+ } catch {
3130
+ }
3131
+ }
3132
+ }
3133
+ } catch (error) {
3134
+ console.error("Cleanup failed:", error);
3135
+ }
3136
+ }
3054
3137
  };
3055
3138
 
3056
3139
  // src/auth/utils/jwks.ts
@@ -3217,4 +3300,4 @@ export {
3217
3300
  DiskStore,
3218
3301
  JWKSVerifier
3219
3302
  };
3220
- //# sourceMappingURL=chunk-3XFM6H44.js.map
3303
+ //# sourceMappingURL=chunk-J2FAZRSL.js.map