circle-ir 4.21.0 → 4.21.1

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.
@@ -18753,6 +18753,7 @@ var LanguageSourcesPass = class {
18753
18753
  additionalSanitizers.push(
18754
18754
  ...findJsPathResolveStartsWithGuardSanitizers(code)
18755
18755
  );
18756
+ additionalSanitizers.push(...findJsRelativeRedirectGuardSanitizers(code));
18756
18757
  additionalSanitizers.push(
18757
18758
  ...findJsPathAllowlistHelperSanitizers(code)
18758
18759
  );
@@ -23732,7 +23733,7 @@ function findJsPathResolveStartsWithGuardSanitizers(code) {
23732
23733
  line: c.line,
23733
23734
  sanitizes: ["path_traversal", "external_taint_escape"]
23734
23735
  });
23735
- for (let l = c.line; l < lines.length; l++) {
23736
+ for (const l of jsGuardCreditLines(lines, c.line, c.fullVar)) {
23736
23737
  if (!varRefRe.test(lines[l])) continue;
23737
23738
  sanitizers.push({
23738
23739
  type: "js_path_resolve_startswith_guard",
@@ -23744,6 +23745,83 @@ function findJsPathResolveStartsWithGuardSanitizers(code) {
23744
23745
  }
23745
23746
  return sanitizers;
23746
23747
  }
23748
+ function findJsRelativeRedirectGuardSanitizers(code) {
23749
+ const sanitizers = [];
23750
+ if (!code.includes("startsWith") && !code.includes(".test(")) return sanitizers;
23751
+ const lines = code.split("\n");
23752
+ const quotes = ["'", '"', "`"];
23753
+ const lit = (q, body2) => `${q}${body2}${q}`;
23754
+ const anyQ = (cond, f) => quotes.some((q) => cond.includes(f(q)));
23755
+ const strictRegexes = ["/^\\/(?![\\/\\\\])/.test(", "/^\\/(?!\\/|\\\\)/.test(", "/^\\/[^\\/\\\\]/.test("];
23756
+ const terminatorRe = /\b(?:throw|return|res\s*\.\s*status\s*\([^)]*\)\s*\.\s*(?:send|end|json))/;
23757
+ const varRefFor = (v) => new RegExp(`(?<![\\w$.])${v.replace(/[.$]/g, "\\$&")}(?![\\w$])`);
23758
+ for (let i2 = 0; i2 < lines.length; i2++) {
23759
+ const raw = lines[i2];
23760
+ const cond = raw.replace(/\s+/g, "");
23761
+ const isIf = /\bif\s*\(/.test(raw);
23762
+ if (!isIf && !(cond.includes("?") && cond.includes(":"))) continue;
23763
+ let v;
23764
+ for (const q of quotes) {
23765
+ const at = cond.indexOf(`.startsWith(${lit(q, "//")})`);
23766
+ if (at > 0) {
23767
+ let k = at;
23768
+ while (k > 0 && /[\w$.]/.test(cond[k - 1])) k--;
23769
+ v = cond.slice(k, at).replace(/^!+/, "");
23770
+ break;
23771
+ }
23772
+ }
23773
+ const regexAccept = strictRegexes.find((r) => cond.includes(r));
23774
+ if (!v && regexAccept) {
23775
+ const after = cond.slice(cond.indexOf(regexAccept) + regexAccept.length);
23776
+ const m = /^([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\)/.exec(after);
23777
+ v = m?.[1];
23778
+ }
23779
+ if (!v || !/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(v)) continue;
23780
+ const slash = (q) => `${v}.startsWith(${lit(q, "/")})`;
23781
+ const dbl = (q) => `${v}.startsWith(${lit(q, "//")})`;
23782
+ const back = (q) => `${v}.startsWith(${lit(q, "/\\\\")})`;
23783
+ const reject = cond.includes("||") && !cond.includes("&&") && anyQ(cond, (q) => `!${slash(q)}`) && anyQ(cond, dbl) && anyQ(cond, back) && !anyQ(cond, (q) => `!${dbl(q)}`) && !anyQ(cond, (q) => `!${back(q)}`);
23784
+ const accept = cond.includes("&&") && !cond.includes("||") && anyQ(cond, slash) && !anyQ(cond, (q) => `!${slash(q)}`) && anyQ(cond, (q) => `!${dbl(q)}`) && anyQ(cond, (q) => `!${back(q)}`) || !!regexAccept && !cond.includes(`!${regexAccept}`) && !cond.includes("||");
23785
+ const ref = varRefFor(v);
23786
+ if (!isIf) {
23787
+ if (accept && cond.includes(`?${v}:`)) sanitizers.push({ type: "js_relative_redirect_guard", method: "startsWith", line: i2 + 1, sanitizes: ["open_redirect"] });
23788
+ continue;
23789
+ }
23790
+ if (reject && guardRejects(lines, i2, terminatorRe, 2)) {
23791
+ for (const l of jsGuardCreditLines(lines, i2, v)) {
23792
+ if (ref.test(lines[l])) sanitizers.push({ type: "js_relative_redirect_guard", method: "startsWith", line: l + 1, sanitizes: ["open_redirect"] });
23793
+ }
23794
+ } else if (accept) {
23795
+ const block = jsBracedBlockLines(lines, i2);
23796
+ const body2 = block ?? [raw];
23797
+ for (let k = 0; k < body2.length; k++) {
23798
+ if (ref.test(k === 0 ? raw.slice(raw.indexOf(")", raw.indexOf("if")) + 1) : body2[k])) {
23799
+ sanitizers.push({ type: "js_relative_redirect_guard", method: "startsWith", line: i2 + k + 1, sanitizes: ["open_redirect"] });
23800
+ }
23801
+ }
23802
+ }
23803
+ }
23804
+ return sanitizers;
23805
+ }
23806
+ function jsGuardCreditLines(lines, from, v) {
23807
+ const esc = v.replace(/[.$]/g, "\\$&");
23808
+ const reassign = new RegExp(`(?:^|[^\\w$.])(?:(?:let|const|var)\\s+)?${esc}\\s*=(?!=)`);
23809
+ const out2 = [];
23810
+ let depth = 0;
23811
+ for (let l = from; l < lines.length; l++) {
23812
+ const line = lines[l];
23813
+ if (l > from && reassign.test(line)) break;
23814
+ out2.push(l);
23815
+ for (const c of line) {
23816
+ if (c === "{") depth++;
23817
+ else if (c === "}") {
23818
+ depth--;
23819
+ if (depth < 0) return out2;
23820
+ }
23821
+ }
23822
+ }
23823
+ return out2;
23824
+ }
23747
23825
  function jsBracedBlockLines(lines, startLineIdx) {
23748
23826
  let depth = 0;
23749
23827
  let started = false;
@@ -27773,10 +27851,96 @@ function findPythonJinjaTemplateSstiFindings(code, file) {
27773
27851
  }
27774
27852
  return findings;
27775
27853
  }
27854
+ function firstJsCallArg(line, start2) {
27855
+ let depth = 0;
27856
+ for (let k = start2; k < line.length; k++) {
27857
+ const ch = line[k];
27858
+ if (ch === "'" || ch === '"' || ch === "`") {
27859
+ const end = line.indexOf(ch, k + 1);
27860
+ if (end < 0) return line.slice(start2);
27861
+ let e = end;
27862
+ while (e > 0 && line[e - 1] === "\\") {
27863
+ const next = line.indexOf(ch, e + 1);
27864
+ if (next < 0) return line.slice(start2);
27865
+ e = next;
27866
+ }
27867
+ k = e;
27868
+ continue;
27869
+ }
27870
+ if (ch === "(" || ch === "[" || ch === "{") depth++;
27871
+ else if (ch === ")" || ch === "]" || ch === "}") {
27872
+ if (depth === 0) return line.slice(start2, k);
27873
+ depth--;
27874
+ } else if (ch === "," && depth === 0) return line.slice(start2, k);
27875
+ }
27876
+ return line.slice(start2);
27877
+ }
27878
+ function jsDestructuredFromRequest(stmt, reqRe) {
27879
+ const kw = ["const ", "let ", "var "].find((k) => stmt.startsWith(k));
27880
+ if (!kw) return null;
27881
+ const t = stmt.slice(kw.length).trimStart();
27882
+ if (!t.startsWith("{")) return null;
27883
+ const close = t.indexOf("}");
27884
+ if (close < 0) return null;
27885
+ const rest = t.slice(close + 1).trimStart();
27886
+ if (!rest.startsWith("=") || !reqRe.test(blankJsStringText(rest.slice(1)))) return null;
27887
+ const names = [];
27888
+ for (const raw of t.slice(1, close).split(",")) {
27889
+ let p = raw.trim();
27890
+ const eq = p.indexOf("=");
27891
+ if (eq >= 0) p = p.slice(0, eq).trim();
27892
+ const colon = p.indexOf(":");
27893
+ const bound = (colon >= 0 ? p.slice(colon + 1) : p).trim();
27894
+ if (/^[A-Za-z_$][\w$]*$/.test(bound)) names.push(bound);
27895
+ }
27896
+ return names.length > 0 ? names : null;
27897
+ }
27898
+ function blankJsStringText(expr) {
27899
+ let out2 = "";
27900
+ let k = 0;
27901
+ while (k < expr.length) {
27902
+ const ch = expr[k];
27903
+ if (ch === "'" || ch === '"') {
27904
+ let e = k + 1;
27905
+ while (e < expr.length && expr[e] !== ch) e += expr[e] === "\\" ? 2 : 1;
27906
+ out2 += "''";
27907
+ k = e + 1;
27908
+ continue;
27909
+ }
27910
+ if (ch === "`") {
27911
+ let e = k + 1;
27912
+ let subs = "";
27913
+ while (e < expr.length && expr[e] !== "`") {
27914
+ if (expr[e] === "\\") {
27915
+ e += 2;
27916
+ continue;
27917
+ }
27918
+ if (expr[e] === "$" && expr[e + 1] === "{") {
27919
+ const close = expr.indexOf("}", e + 2);
27920
+ if (close < 0) {
27921
+ subs += ` ${expr.slice(e + 2)}`;
27922
+ e = expr.length;
27923
+ break;
27924
+ }
27925
+ subs += ` ${expr.slice(e + 2, close)}`;
27926
+ e = close + 1;
27927
+ continue;
27928
+ }
27929
+ e++;
27930
+ }
27931
+ out2 += `''${subs}`;
27932
+ k = e + 1;
27933
+ continue;
27934
+ }
27935
+ out2 += ch;
27936
+ k++;
27937
+ }
27938
+ return out2;
27939
+ }
27776
27940
  function findJsTemplateInjectionSstiFindings(code, file) {
27777
27941
  const findings = [];
27778
27942
  if (typeof code !== "string" || code.length === 0) return findings;
27779
- if (!/\bHandlebars\s*\.\s*compile\s*\(/.test(code) && !/\bejs\s*\.\s*(?:render|compile)\s*\(/.test(code)) {
27943
+ if (!/\b[Hh]andlebars\s*\.\s*compile\s*\(/.test(code) && !/\b(?:ejs|pug)\s*\.\s*(?:render|compile)\s*\(/.test(code) && !/\bnunjucks\s*\.\s*renderString\s*\(/.test(code)) {
27780
27944
  return findings;
27781
27945
  }
27782
27946
  if (!/\breq\s*\.\s*(?:query|body|params|headers|cookies)\b/.test(code)) {
@@ -27790,10 +27954,15 @@ function findJsTemplateInjectionSstiFindings(code, file) {
27790
27954
  for (let i2 = 0; i2 < lines.length; i2++) {
27791
27955
  const t = lines[i2].trim();
27792
27956
  if (t.startsWith("//")) continue;
27957
+ const destructured = jsDestructuredFromRequest(t, reqExtractRe);
27958
+ if (destructured) {
27959
+ for (const name2 of destructured) taintedVars.add(name2);
27960
+ continue;
27961
+ }
27793
27962
  const m = t.match(/^(?:const|let|var)\s+(\w+)(?:\s*:\s*[^=]+)?\s*=\s*(.+?);?$/);
27794
27963
  if (!m) continue;
27795
27964
  const lhs = m[1];
27796
- const rhs = m[2];
27965
+ const rhs = blankJsStringText(m[2]);
27797
27966
  if (taintedVars.has(lhs)) continue;
27798
27967
  if (reqExtractRe.test(rhs)) {
27799
27968
  taintedVars.add(lhs);
@@ -27809,14 +27978,15 @@ function findJsTemplateInjectionSstiFindings(code, file) {
27809
27978
  if (taintedVars.size === before) break;
27810
27979
  }
27811
27980
  if (taintedVars.size === 0) return findings;
27812
- const callRe = /\b(Handlebars\s*\.\s*compile|ejs\s*\.\s*(?:render|compile))\s*\(\s*([^,)]+)/;
27981
+ const callRe = /\b([Hh]andlebars\s*\.\s*compile|(?:ejs|pug)\s*\.\s*(?:render|compile)|nunjucks\s*\.\s*renderString)\s*\(/;
27813
27982
  const seen = /* @__PURE__ */ new Set();
27814
27983
  for (let i2 = 0; i2 < lines.length; i2++) {
27815
27984
  const line = lines[i2];
27816
27985
  if (line.trim().startsWith("//")) continue;
27817
27986
  const m = line.match(callRe);
27818
- if (!m) continue;
27819
- const arg = m[2].trim();
27987
+ if (!m || m.index === void 0) continue;
27988
+ const arg = blankJsStringText(firstJsCallArg(line, m.index + m[0].length)).trim();
27989
+ if (arg === "") continue;
27820
27990
  let tainted = reqExtractRe.test(arg);
27821
27991
  if (!tainted) {
27822
27992
  for (const v of taintedVars) {
@@ -27837,7 +28007,7 @@ function findJsTemplateInjectionSstiFindings(code, file) {
27837
28007
  cwe: "CWE-1336",
27838
28008
  severity: "critical",
27839
28009
  level: "error",
27840
- message: "Server-side template injection: compiling/rendering a template whose source is attacker-controlled (`Handlebars.compile(...)` / `ejs.render(...)`) opens helper-shadowing and prototype-gadget RCE paths. Use a fixed template and pass user data as context.",
28010
+ message: "Server-side template injection: compiling/rendering a template whose source is attacker-controlled (`Handlebars.compile(...)` / `ejs.render(...)` / `pug.render(...)` / `nunjucks.renderString(...)`) opens helper-shadowing, prototype-gadget and template-code RCE paths. Use a fixed template and pass user data as context.",
27841
28011
  file,
27842
28012
  line: i2 + 1,
27843
28013
  snippet: line.trim()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "circle-ir",
3
- "version": "4.21.0",
3
+ "version": "4.21.1",
4
4
  "description": "High-performance Static Application Security Testing (SAST) library for detecting security vulnerabilities through taint analysis",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",