xploitscan-shared-rules 1.20.0 → 1.21.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
@@ -940,6 +940,69 @@ var TEST_FILE_PATTERN = /(?:\.test\.|\.spec\.|__tests__|__mocks__|\.stories\.|\.
940
940
  function isTestFile(filePath) {
941
941
  return TEST_FILE_PATTERN.test(filePath);
942
942
  }
943
+ var HANDLER_HEADER_RE = /(?:\b(?:async\s+)?function\b|=>\s*$|=>\s*\{?\s*$|\.(?:get|post|put|patch|delete|use|all)\s*\(|\bexport\s+(?:async\s+)?(?:function|const)\s+(?:GET|POST|PUT|PATCH|DELETE)\b|\b(?:Deno\.)?serve\s*\()/i;
944
+ function maskComments(content) {
945
+ return content.replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, " ")).replace(/(^|[^:])\/\/[^\n]*/gm, (m, p1) => p1 + " ".repeat(m.length - p1.length));
946
+ }
947
+ function queryScope(content, index) {
948
+ const open = content.indexOf("(", index);
949
+ if (open === -1 || open - index > 80) return null;
950
+ let depth = 0;
951
+ for (let i = open; i < content.length; i++) {
952
+ const c = content[i];
953
+ if (c === "(") depth++;
954
+ else if (c === ")") {
955
+ depth--;
956
+ if (depth === 0) return content.slice(open, i + 1);
957
+ }
958
+ }
959
+ return null;
960
+ }
961
+ function enclosingHandler(content, index) {
962
+ const MAX_LEVELS = 6;
963
+ let searchFrom = index;
964
+ for (let level = 0; level < MAX_LEVELS; level++) {
965
+ let depth = 0;
966
+ let open = -1;
967
+ for (let i = searchFrom; i >= 0; i--) {
968
+ const c = content[i];
969
+ if (c === "}") depth++;
970
+ else if (c === "{") {
971
+ if (depth === 0) {
972
+ open = i;
973
+ break;
974
+ }
975
+ depth--;
976
+ }
977
+ }
978
+ if (open === -1) return null;
979
+ let fwd = 0;
980
+ let close = -1;
981
+ for (let i = open; i < content.length; i++) {
982
+ const c = content[i];
983
+ if (c === "{") fwd++;
984
+ else if (c === "}") {
985
+ fwd--;
986
+ if (fwd === 0) {
987
+ close = i;
988
+ break;
989
+ }
990
+ }
991
+ }
992
+ if (close === -1 || close < index) return null;
993
+ const rawStart = Math.max(0, open - 240);
994
+ const window = content.slice(rawStart, open);
995
+ const cut = Math.max(window.lastIndexOf("}"), window.lastIndexOf(";"));
996
+ const headerStart = cut === -1 ? rawStart : rawStart + cut + 1;
997
+ const header = content.slice(headerStart, open);
998
+ if (HANDLER_HEADER_RE.test(header)) {
999
+ return content.slice(headerStart, close + 1);
1000
+ }
1001
+ searchFrom = open - 1;
1002
+ if (searchFrom < 0) return null;
1003
+ }
1004
+ return null;
1005
+ }
943
1006
  var SERVER_SIDE_PATH_RE = new RegExp(
944
1007
  [
945
1008
  // Directory-style anchors — match with OR without leading slash so
@@ -8465,15 +8528,22 @@ var insecureDirectObjectReference = {
8465
8528
  if (!filePath.match(/\.(js|ts|jsx|tsx)$/)) return [];
8466
8529
  if (isTestFile(filePath)) return [];
8467
8530
  if (/admin/i.test(filePath)) return [];
8468
- if (/userId|ownerId|createdBy|user_id|owner_id|AND.*user|where.*user|authorize|canAccess|checkPermission|isOwner|belongsTo/i.test(content)) return [];
8469
- if (/requireUser|requireUserForApi|requireAuth|withAuth|getServerSession|auth\(\)|req\.user\b|passport|session\.user|isAuthenticated/i.test(content)) return [];
8470
- if (!/findUnique|findFirst|findById|\.findOne|WHERE.*=.*(?:params|req\.(?:body|query))|\.get\(.*(?:params|req\.(?:body|query))/i.test(content)) return [];
8531
+ const scan = maskComments(content);
8532
+ const LOOKUP_RE = /findUnique|findFirst|findById|\.findOne\b|\.find\s*\(\s*\{|WHERE[\s\S]{0,120}?(?:params|req\.(?:body|query))|\.get\([^)]*(?:params|req\.(?:body|query))/i;
8533
+ const lookup = scan.match(LOOKUP_RE);
8534
+ if (!lookup || lookup.index === void 0) return [];
8535
+ const handlerScope = enclosingHandler(scan, lookup.index) ?? scan;
8536
+ const qScope = queryScope(scan, lookup.index) ?? handlerScope;
8537
+ if (/userId|ownerId|createdBy|user_id|owner_id|AND.*user|where.*user|authorize|canAccess|checkPermission|isOwner|belongsTo/i.test(qScope)) return [];
8538
+ if (/requireUser|requireUserForApi|requireAuth|withAuth|getServerSession|auth\(\)|passport|isAuthenticated/i.test(handlerScope)) return [];
8471
8539
  const isNextApiIdor = /\/api\//i.test(filePath) && /export\s+(?:async\s+)?function\s+(?:GET|PUT|PATCH|DELETE)/i.test(content) && /params\.id|params\.(?:slug|uuid)|req\.params\./i.test(content);
8472
8540
  const hasExpressHandler = /\.(get|post|put|patch|delete)\s*\(\s*["'`][^"'`]*["'`]\s*,/i.test(content);
8473
8541
  const readsObjectRef = /req\.params\b|req\.(?:body|query)\.\w*id\b/i.test(content);
8474
8542
  const isExpressIdor = hasExpressHandler && readsObjectRef;
8475
- if (!isNextApiIdor && !isExpressIdor) return [];
8476
- const m = content.match(/params\.id|params\.(?:slug|uuid)|req\.params\.\w+|req\.params\b|req\.(?:body|query)\.\w*id/i);
8543
+ const hasExportedHandler = /(?:module\.)?exports\.\w+\s*=\s*(?:async\s+)?(?:function\s*)?\(\s*req\b[^)]*\)/i.test(content) || /(?:module\.)?exports\.\w+\s*=\s*(?:async\s*)?\(\s*req\b[^)]*\)\s*=>/i.test(content);
8544
+ const isControllerIdor = hasExportedHandler && readsObjectRef;
8545
+ if (!isNextApiIdor && !isExpressIdor && !isControllerIdor) return [];
8546
+ const m = scan.match(/params\.id|params\.(?:slug|uuid)|req\.params\.\w+|req\.params\b|req\.(?:body|query)\.\w*id/i);
8477
8547
  if (!m || m.index === void 0) return [];
8478
8548
  const lineNum = lineNumberAt(content, m.index);
8479
8549
  return [{