xploitscan-shared-rules 1.24.0 → 1.25.0

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.
package/dist/index.js CHANGED
@@ -418,6 +418,346 @@ function vendoredReason(filePath) {
418
418
  return null;
419
419
  }
420
420
 
421
+ // src/routes.ts
422
+ var API_RE = /^\/api(\/|$)|^\/_|\.(json|js|xml|txt|ico)$/;
423
+ var DYNAMIC_RE = /:[A-Za-z_]|\[[^\]]+\]|<[^>]+>|\$[A-Za-z_]|\*/;
424
+ function hasDep(files, name) {
425
+ const pkg = files.find(
426
+ (f) => f.path === "package.json" || f.path.endsWith("/package.json")
427
+ );
428
+ if (!pkg) return false;
429
+ try {
430
+ const json = JSON.parse(pkg.content);
431
+ return Boolean({ ...json.dependencies ?? {}, ...json.devDependencies ?? {} }[name]);
432
+ } catch {
433
+ return false;
434
+ }
435
+ }
436
+ function normalize(p) {
437
+ if (!p.startsWith("/")) return "";
438
+ if (p.length > 1 && p.endsWith("/")) return p.replace(/\/+$/, "");
439
+ return p;
440
+ }
441
+ function extractRoutes(files) {
442
+ const routes = /* @__PURE__ */ new Map();
443
+ const frameworks = /* @__PURE__ */ new Set();
444
+ const add = (raw, framework, source, kindOverride) => {
445
+ const path = normalize(raw);
446
+ if (!path || routes.has(path)) return;
447
+ routes.set(path, {
448
+ path,
449
+ // The URL prefix is a fallback, not the authority. A Next `route.ts` is a
450
+ // handler wherever it lives: /free-trust-page/claim/start/route.ts is an
451
+ // endpoint, not a page, and calling it a page mislabels what an attacker
452
+ // would actually hit.
453
+ kind: kindOverride ?? (API_RE.test(path) ? "api" : "page"),
454
+ dynamic: DYNAMIC_RE.test(path),
455
+ framework,
456
+ source
457
+ });
458
+ frameworks.add(framework);
459
+ };
460
+ const isNext = hasDep(files, "next");
461
+ const isExpress = hasDep(files, "express") || hasDep(files, "hono") || hasDep(files, "koa");
462
+ const isTanstack = hasDep(files, "@tanstack/react-router") || hasDep(files, "@tanstack/react-start");
463
+ if (isTanstack) {
464
+ const gen = files.find((f) => f.path.endsWith("routeTree.gen.ts"));
465
+ if (gen) {
466
+ for (const m of gen.content.matchAll(/'(\/[A-Za-z0-9_\-/$.]*)'/g)) {
467
+ if (m[1].split("/").some((seg) => seg.startsWith("_"))) continue;
468
+ add(m[1], "tanstack-router", gen.path);
469
+ }
470
+ }
471
+ }
472
+ for (const file of files) {
473
+ const rel = file.path;
474
+ if (isNext) {
475
+ const page = rel.match(/(?:^|\/)app\/(?:(.*)\/)?page\.(tsx?|jsx?)$/);
476
+ if (page) {
477
+ const segs = (page[1] ?? "").split("/").filter(Boolean).filter((s) => !/^\(.*\)$/.test(s));
478
+ add("/" + segs.join("/"), "next-app", rel);
479
+ }
480
+ const handler = rel.match(/(?:^|\/)app\/(?:(.*)\/)?route\.(tsx?|jsx?)$/);
481
+ if (handler) {
482
+ const segs = (handler[1] ?? "").split("/").filter(Boolean).filter((s) => !/^\(.*\)$/.test(s));
483
+ add("/" + segs.join("/"), "next-app", rel, "api");
484
+ }
485
+ const pages = rel.match(/(?:^|\/)pages\/(.+)\.(tsx?|jsx?)$/);
486
+ if (pages && !/^_/.test(pages[1].split("/").pop() ?? "")) {
487
+ const p = "/" + pages[1].replace(/\/index$/, "").replace(/^index$/, "");
488
+ add(p === "/" ? "/" : p, "next-pages", rel);
489
+ }
490
+ }
491
+ if (!/\.(tsx?|jsx?|mjs|cjs|py)$/.test(rel)) continue;
492
+ const src = file.content;
493
+ for (const m of src.matchAll(/<Route\s[^>]*path\s*=\s*["'`]([^"'`]+)["'`]/g)) {
494
+ add(m[1].startsWith("/") ? m[1] : "/" + m[1], "react-router", rel);
495
+ }
496
+ if (/createBrowserRouter|createHashRouter|useRoutes/.test(src)) {
497
+ for (const m of src.matchAll(/path\s*:\s*["'`]([^"'`]+)["'`]/g)) {
498
+ add(m[1].startsWith("/") ? m[1] : "/" + m[1], "react-router", rel);
499
+ }
500
+ }
501
+ for (const m of src.matchAll(
502
+ /@\w+\.(?:route|get|post|put|patch|delete)\(\s*["']([^"']+)["']/g
503
+ )) {
504
+ add(m[1], "flask-fastapi", rel);
505
+ }
506
+ for (const m of src.matchAll(/add_url_rule\(\s*["']([^"']+)["']/g)) {
507
+ add(m[1], "flask-fastapi", rel);
508
+ }
509
+ if (isExpress) {
510
+ for (const m of src.matchAll(
511
+ /\b(?:app|router)\.(?:get|post|put|patch|delete|all)\(\s*["'`]([^"'`]+)["'`]/g
512
+ )) {
513
+ add(m[1], "express", rel);
514
+ }
515
+ }
516
+ }
517
+ return {
518
+ frameworks: [...frameworks],
519
+ routes: [...routes.values()].sort((a, b) => a.path.localeCompare(b.path))
520
+ };
521
+ }
522
+ function routeForFile(table, filePath) {
523
+ const norm = filePath.replace(/^\.\//, "");
524
+ return table.routes.find((r) => r.source === norm || r.source.endsWith("/" + norm));
525
+ }
526
+
527
+ // src/intent.ts
528
+ var INTENT_SOURCES = [
529
+ { test: /(^|\/)\.xploitscan-intent$/i, origin: "declared" },
530
+ { test: /(^|\/)(CLAUDE|AGENTS)\.md$/i, origin: "agent-instructions" },
531
+ { test: /(^|\/)\.cursorrules$/i, origin: "agent-instructions" },
532
+ { test: /(^|\/)\.cursor\/rules\/[^/]+\.mdc$/i, origin: "agent-instructions" },
533
+ { test: /(^|\/)README(\.md)?$/i, origin: "readme" }
534
+ ];
535
+ function isProse(line) {
536
+ const t = line.trim();
537
+ if (t.length < 15 || t.length > 400) return false;
538
+ if (/^[#>|`~\-=*_+[\]]{3,}/.test(t)) return false;
539
+ if (/^\s*[|]/.test(t)) return false;
540
+ if (/^\s*(import|export|const|let|var|function|class|return|if|for)\b/.test(t)) return false;
541
+ if (/^https?:\/\//.test(t)) return false;
542
+ return /[a-z]{3}/i.test(t);
543
+ }
544
+ function clean(line) {
545
+ return line.replace(/^\s*(?:[-*+]|\d+\.)\s+/, "").replace(/^#+\s*/, "").replace(/[*_`]/g, "").trim();
546
+ }
547
+ function harvestIntent(files) {
548
+ const out = [];
549
+ for (const file of files) {
550
+ const match = INTENT_SOURCES.find((s) => s.test.test(file.path));
551
+ if (!match) continue;
552
+ let inFence = false;
553
+ const lines = file.content.split("\n");
554
+ for (let i = 0; i < lines.length; i++) {
555
+ const raw = lines[i];
556
+ if (/^\s*(```|~~~)/.test(raw)) {
557
+ inFence = !inFence;
558
+ continue;
559
+ }
560
+ if (inFence) continue;
561
+ if (!isProse(raw)) continue;
562
+ for (const sentence of clean(raw).split(/(?<=[.!?])\s+(?=[A-Z])/)) {
563
+ const text = sentence.trim();
564
+ if (text.length >= 15) {
565
+ out.push({ text, source: file.path, line: i + 1, origin: match.origin });
566
+ }
567
+ }
568
+ }
569
+ }
570
+ return out;
571
+ }
572
+ var INTENT_EXPECTATIONS = [
573
+ {
574
+ id: "owner-scoped",
575
+ claims: /\b(only|just)\b[^.]{0,40}\b(their own|his own|her own|its own|the(ir)? own)\b|\bown (data|records?|orders?|posts?|files?|documents?|projects?)\b|\bcan(?:'|no)?t (?:see|access|view|read) (?:other|another)\b/i,
576
+ expectation: "each user can only reach rows that belong to them",
577
+ contradictedBy: ["VC158"]
578
+ },
579
+ {
580
+ id: "requires-auth",
581
+ claims: /\b(require|requires|required|must)\b[^.]{0,40}\b(log ?in|logged ?in|sign(ed)? ?in|authenticat\w+|an account|be a user)\b|\b(only|restricted to)\b[^.]{0,30}\b(authenticated|logged.?in|signed.?in|registered)\b|\bbehind (a )?(login|auth)\b/i,
582
+ expectation: "the endpoint rejects unauthenticated callers",
583
+ contradictedBy: ["VC003", "VC064", "VC127", "VC210"]
584
+ },
585
+ {
586
+ id: "admin-only",
587
+ // "nobody else can" is included because it is how people actually write
588
+ // this. "Admins can refund an order; nobody else can" states a privilege
589
+ // boundary as plainly as "admin-only" does, and the first draft of this
590
+ // pattern missed it — caught by the sentence battery in intent.test.ts,
591
+ // which is why that battery exists.
592
+ claims: /\b(admin|administrator|moderator|staff)s?[- ]?(only|can only)\b|\bonly\b[^.]{0,25}\b(admins?|administrators?|moderators?)\b|\brole[- ]based\b|\brequires? (?:the )?(?:admin|moderator) role\b|\b(?:nobody|no ?one) else (?:can|may|should|is able to)\b/i,
593
+ expectation: "the privilege is decided by the server, not the browser",
594
+ contradictedBy: ["VC010", "VC085", "VC213", "VC214"]
595
+ },
596
+ {
597
+ id: "private-data",
598
+ claims: /\b(private|confidential|not public|never public|internal only)\b/i,
599
+ expectation: "the data is not reachable without authentication",
600
+ contradictedBy: ["VC003", "VC085"]
601
+ },
602
+ {
603
+ id: "verified-webhooks",
604
+ claims: /\bwebhooks?\b[^.]{0,60}\b(verif\w+|signed|signature|authenticat\w+)\b|\bverif\w+[^.]{0,30}\bwebhooks?\b/i,
605
+ expectation: "webhook payloads are signature-verified before use",
606
+ contradictedBy: ["VC005", "VC209"]
607
+ },
608
+ {
609
+ id: "rate-limited",
610
+ claims: /\brate[- ]?limit\w*\b|\bthrottl\w+\b|\bprevents? (?:abuse|brute[- ]?force)\b/i,
611
+ expectation: "the endpoint enforces a request cap",
612
+ contradictedBy: ["VC008", "VC130"]
613
+ }
614
+ ];
615
+ var STOPWORDS = /* @__PURE__ */ new Set([
616
+ "the",
617
+ "a",
618
+ "an",
619
+ "and",
620
+ "or",
621
+ "but",
622
+ "for",
623
+ "with",
624
+ "that",
625
+ "this",
626
+ "their",
627
+ "them",
628
+ "they",
629
+ "from",
630
+ "into",
631
+ "only",
632
+ "own",
633
+ "can",
634
+ "cannot",
635
+ "must",
636
+ "will",
637
+ "should",
638
+ "have",
639
+ "has",
640
+ "not",
641
+ "all",
642
+ "any",
643
+ "each",
644
+ "every",
645
+ "user",
646
+ "users",
647
+ "app",
648
+ "application",
649
+ "page",
650
+ "pages",
651
+ "data",
652
+ "code",
653
+ "site",
654
+ "web",
655
+ "api",
656
+ "our",
657
+ "your",
658
+ "you",
659
+ "when",
660
+ "where",
661
+ "which",
662
+ "what",
663
+ "who",
664
+ "are",
665
+ "was",
666
+ "were",
667
+ "been",
668
+ "being",
669
+ "does",
670
+ "doing",
671
+ "done",
672
+ "just",
673
+ "other",
674
+ "another",
675
+ "public",
676
+ "private",
677
+ "access",
678
+ "accessible"
679
+ ]);
680
+ function topics(text) {
681
+ const out = /* @__PURE__ */ new Set();
682
+ for (const w of text.toLowerCase().match(/[a-z][a-z-]{2,}/g) ?? []) {
683
+ if (STOPWORDS.has(w)) continue;
684
+ out.add(w);
685
+ if (w.endsWith("ies") && w.length > 4) out.add(`${w.slice(0, -3)}y`);
686
+ else if (w.endsWith("ses") && w.length > 4) out.add(w.slice(0, -2));
687
+ else if (w.endsWith("s") && !w.endsWith("ss") && w.length > 3) out.add(w.slice(0, -1));
688
+ }
689
+ return out;
690
+ }
691
+ var ABSENCE = /\b(?:without|missing|lacks?|lacking|absent|no|not?)\b[\w\s,/-]{0,30}\b(?:verification|verified|signature|auth(?:entication|orization)?|rate[- ]?limits?|rate[- ]?limiting|validation|checks?|guards?)\b/i;
692
+ function describesAbsence(text) {
693
+ return ABSENCE.test(text);
694
+ }
695
+ function isGlobalClaim(text) {
696
+ return /\b(all|every|any|each|no one|nobody|everything)\b/i.test(text) || /\ball (?:api )?(?:routes?|endpoints?|pages?)\b/i.test(text);
697
+ }
698
+ function pathTokens(p) {
699
+ return new Set(
700
+ (p.toLowerCase().match(/[a-z][a-z-]{2,}/g) ?? []).filter((t) => !STOPWORDS.has(t))
701
+ );
702
+ }
703
+ function checkIntent(files, findings) {
704
+ const statements = harvestIntent(files);
705
+ const sources = [...new Set(statements.map((s) => s.source))];
706
+ const routes = extractRoutes(files).routes;
707
+ const mismatches = [];
708
+ const unchallenged = [];
709
+ const byRule = /* @__PURE__ */ new Map();
710
+ for (const f of findings) {
711
+ const list = byRule.get(f.rule);
712
+ if (list) list.push(f);
713
+ else byRule.set(f.rule, [f]);
714
+ }
715
+ const seen = /* @__PURE__ */ new Set();
716
+ for (const statement of statements) {
717
+ const global = isGlobalClaim(statement.text);
718
+ const subject = topics(statement.text);
719
+ if (describesAbsence(statement.text)) continue;
720
+ for (const exp of INTENT_EXPECTATIONS) {
721
+ if (!exp.claims.test(statement.text)) continue;
722
+ const candidates = exp.contradictedBy.flatMap((r) => byRule.get(r) ?? []);
723
+ if (candidates.length === 0) {
724
+ unchallenged.push({ statement, expectation: exp.expectation });
725
+ continue;
726
+ }
727
+ const relevant = candidates.filter((f) => {
728
+ if (global) return true;
729
+ const fileTokens = pathTokens(f.file);
730
+ const route2 = routes.find((r) => r.source === f.file);
731
+ const routeTokens = route2 ? pathTokens(route2.path) : /* @__PURE__ */ new Set();
732
+ for (const t of subject) {
733
+ if (fileTokens.has(t) || routeTokens.has(t)) return true;
734
+ }
735
+ return false;
736
+ });
737
+ if (relevant.length === 0) {
738
+ unchallenged.push({ statement, expectation: exp.expectation });
739
+ continue;
740
+ }
741
+ const route = routes.find((r) => r.source === relevant[0].file);
742
+ const id = `INTENT-${exp.id}-${statement.source}:${statement.line}-${relevant[0].file}:${relevant[0].line}`;
743
+ if (seen.has(id)) continue;
744
+ seen.add(id);
745
+ const where = route ? `${route.path} (${relevant[0].file}:${relevant[0].line})` : `${relevant[0].file}:${relevant[0].line}`;
746
+ mismatches.push({
747
+ id,
748
+ statement,
749
+ expectationId: exp.id,
750
+ expectation: exp.expectation,
751
+ evidence: relevant.slice(0, 5),
752
+ route,
753
+ basis: global ? "global" : "topic",
754
+ message: `${statement.source}:${statement.line} says "${statement.text}" \u2014 so ${exp.expectation}. ${where} does not: ${relevant[0].title}.`
755
+ });
756
+ }
757
+ }
758
+ return { statements, mismatches, sources, unchallenged };
759
+ }
760
+
421
761
  // src/silencing.ts
422
762
  function isInlineSilenced(content, matchIndex, ruleId) {
423
763
  const lines = content.split("\n");
@@ -9379,112 +9719,6 @@ function scanEntropy(files) {
9379
9719
  return findings;
9380
9720
  }
9381
9721
 
9382
- // src/routes.ts
9383
- var API_RE = /^\/api(\/|$)|^\/_|\.(json|js|xml|txt|ico)$/;
9384
- var DYNAMIC_RE = /:[A-Za-z_]|\[[^\]]+\]|<[^>]+>|\$[A-Za-z_]|\*/;
9385
- function hasDep(files, name) {
9386
- const pkg = files.find(
9387
- (f) => f.path === "package.json" || f.path.endsWith("/package.json")
9388
- );
9389
- if (!pkg) return false;
9390
- try {
9391
- const json = JSON.parse(pkg.content);
9392
- return Boolean({ ...json.dependencies ?? {}, ...json.devDependencies ?? {} }[name]);
9393
- } catch {
9394
- return false;
9395
- }
9396
- }
9397
- function normalize(p) {
9398
- if (!p.startsWith("/")) return "";
9399
- if (p.length > 1 && p.endsWith("/")) return p.replace(/\/+$/, "");
9400
- return p;
9401
- }
9402
- function extractRoutes(files) {
9403
- const routes = /* @__PURE__ */ new Map();
9404
- const frameworks = /* @__PURE__ */ new Set();
9405
- const add = (raw, framework, source, kindOverride) => {
9406
- const path = normalize(raw);
9407
- if (!path || routes.has(path)) return;
9408
- routes.set(path, {
9409
- path,
9410
- // The URL prefix is a fallback, not the authority. A Next `route.ts` is a
9411
- // handler wherever it lives: /free-trust-page/claim/start/route.ts is an
9412
- // endpoint, not a page, and calling it a page mislabels what an attacker
9413
- // would actually hit.
9414
- kind: kindOverride ?? (API_RE.test(path) ? "api" : "page"),
9415
- dynamic: DYNAMIC_RE.test(path),
9416
- framework,
9417
- source
9418
- });
9419
- frameworks.add(framework);
9420
- };
9421
- const isNext = hasDep(files, "next");
9422
- const isExpress = hasDep(files, "express") || hasDep(files, "hono") || hasDep(files, "koa");
9423
- const isTanstack = hasDep(files, "@tanstack/react-router") || hasDep(files, "@tanstack/react-start");
9424
- if (isTanstack) {
9425
- const gen = files.find((f) => f.path.endsWith("routeTree.gen.ts"));
9426
- if (gen) {
9427
- for (const m of gen.content.matchAll(/'(\/[A-Za-z0-9_\-/$.]*)'/g)) {
9428
- if (m[1].split("/").some((seg) => seg.startsWith("_"))) continue;
9429
- add(m[1], "tanstack-router", gen.path);
9430
- }
9431
- }
9432
- }
9433
- for (const file of files) {
9434
- const rel = file.path;
9435
- if (isNext) {
9436
- const page = rel.match(/(?:^|\/)app\/(?:(.*)\/)?page\.(tsx?|jsx?)$/);
9437
- if (page) {
9438
- const segs = (page[1] ?? "").split("/").filter(Boolean).filter((s) => !/^\(.*\)$/.test(s));
9439
- add("/" + segs.join("/"), "next-app", rel);
9440
- }
9441
- const handler = rel.match(/(?:^|\/)app\/(?:(.*)\/)?route\.(tsx?|jsx?)$/);
9442
- if (handler) {
9443
- const segs = (handler[1] ?? "").split("/").filter(Boolean).filter((s) => !/^\(.*\)$/.test(s));
9444
- add("/" + segs.join("/"), "next-app", rel, "api");
9445
- }
9446
- const pages = rel.match(/(?:^|\/)pages\/(.+)\.(tsx?|jsx?)$/);
9447
- if (pages && !/^_/.test(pages[1].split("/").pop() ?? "")) {
9448
- const p = "/" + pages[1].replace(/\/index$/, "").replace(/^index$/, "");
9449
- add(p === "/" ? "/" : p, "next-pages", rel);
9450
- }
9451
- }
9452
- if (!/\.(tsx?|jsx?|mjs|cjs|py)$/.test(rel)) continue;
9453
- const src = file.content;
9454
- for (const m of src.matchAll(/<Route\s[^>]*path\s*=\s*["'`]([^"'`]+)["'`]/g)) {
9455
- add(m[1].startsWith("/") ? m[1] : "/" + m[1], "react-router", rel);
9456
- }
9457
- if (/createBrowserRouter|createHashRouter|useRoutes/.test(src)) {
9458
- for (const m of src.matchAll(/path\s*:\s*["'`]([^"'`]+)["'`]/g)) {
9459
- add(m[1].startsWith("/") ? m[1] : "/" + m[1], "react-router", rel);
9460
- }
9461
- }
9462
- for (const m of src.matchAll(
9463
- /@\w+\.(?:route|get|post|put|patch|delete)\(\s*["']([^"']+)["']/g
9464
- )) {
9465
- add(m[1], "flask-fastapi", rel);
9466
- }
9467
- for (const m of src.matchAll(/add_url_rule\(\s*["']([^"']+)["']/g)) {
9468
- add(m[1], "flask-fastapi", rel);
9469
- }
9470
- if (isExpress) {
9471
- for (const m of src.matchAll(
9472
- /\b(?:app|router)\.(?:get|post|put|patch|delete|all)\(\s*["'`]([^"'`]+)["'`]/g
9473
- )) {
9474
- add(m[1], "express", rel);
9475
- }
9476
- }
9477
- }
9478
- return {
9479
- frameworks: [...frameworks],
9480
- routes: [...routes.values()].sort((a, b) => a.path.localeCompare(b.path))
9481
- };
9482
- }
9483
- function routeForFile(table, filePath) {
9484
- const norm = filePath.replace(/^\.\//, "");
9485
- return table.routes.find((r) => r.source === norm || r.source.endsWith("/" + norm));
9486
- }
9487
-
9488
9722
  // src/url-recon.ts
9489
9723
  var BLOCKED_HOSTNAMES = /* @__PURE__ */ new Set([
9490
9724
  "localhost",
@@ -9659,6 +9893,7 @@ function extractScriptUrls(html, pageUrl, limit = 12) {
9659
9893
  }
9660
9894
  export {
9661
9895
  FREE_RULE_COUNT,
9896
+ INTENT_EXPECTATIONS,
9662
9897
  MODEL_ANALYSIS,
9663
9898
  MODEL_POLICY_REVIEWED,
9664
9899
  MODEL_REVIEW_INTERVAL_DAYS,
@@ -9679,6 +9914,7 @@ export {
9679
9914
  calculateGrade,
9680
9915
  callSpreads,
9681
9916
  callbackHell,
9917
+ checkIntent,
9682
9918
  classifyExposure,
9683
9919
  clickjacking,
9684
9920
  clientComponentSecret,
@@ -9786,6 +10022,7 @@ export {
9786
10022
  hardcodedVaultToken,
9787
10023
  hardcodedVercelToken,
9788
10024
  hardcodedWeaviateKey,
10025
+ harvestIntent,
9789
10026
  hostHeaderRedirect,
9790
10027
  httpRequestSmuggling,
9791
10028
  insecureCookies,