flagrix 0.1.4 → 0.3.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.
@@ -519,6 +519,211 @@ async function githubApiError(response, fallback = "GitHub API error") {
519
519
  return new Error(apiMessage ? `${fallback}: ${response.status} \u2014 ${apiMessage}` : `${fallback}: ${response.status}`);
520
520
  }
521
521
 
522
+ // node_modules/@flagrix/scanner-core/dist/github/history-signals.js
523
+ var BACKDATED_PUSH_DAYS = 30;
524
+ var RECENT_FORCE_PUSH_DAYS = 90;
525
+ var DAY_MS = 24 * 60 * 60 * 1e3;
526
+ var NULL_SHA = /^0+$/;
527
+ function detectHistoryTampering(activity, commits, branch, now = Date.now()) {
528
+ const ref = `refs/heads/${branch}`;
529
+ const onBranch = activity.filter((event) => event.ref === ref);
530
+ const commitsBySha = new Map(commits.map((commit) => [commit.sha, commit]));
531
+ let worst = null;
532
+ for (const event of onBranch) {
533
+ if (event.activity_type !== "push" && event.activity_type !== "force_push")
534
+ continue;
535
+ if (!event.before || NULL_SHA.test(event.before))
536
+ continue;
537
+ const commit = commitsBySha.get(event.after);
538
+ if (!commit || commit.commit.verification?.verified)
539
+ continue;
540
+ const committed = commit.commit.committer?.date;
541
+ if (!committed)
542
+ continue;
543
+ const committedAt = new Date(committed);
544
+ const lagDays = Math.floor((Date.parse(event.timestamp) - committedAt.getTime()) / DAY_MS);
545
+ if (lagDays >= BACKDATED_PUSH_DAYS && (!worst || lagDays > worst.lagDays)) {
546
+ worst = { event, committedAt, lagDays };
547
+ }
548
+ }
549
+ if (worst) {
550
+ const force = worst.event.activity_type === "force_push";
551
+ const sha = worst.event.after.slice(0, 7);
552
+ return [{
553
+ severity: force ? "high" : "medium",
554
+ confidence: "medium",
555
+ type: "GIT_HISTORY_TAMPERING",
556
+ pattern: "BACKDATED_PUSH",
557
+ description: `Unsigned commit ${sha} on ${branch} is dated ${worst.committedAt.toISOString().slice(0, 10)} but was ${force ? "force-pushed" : "pushed"} ${worst.lagDays} days later (${worst.event.timestamp.slice(0, 10)})`,
558
+ codeExplanation: "\u{1F570}\uFE0F The commit's date says it is old, but GitHub only received it recently. Account-takeover campaigns (PolinRider) backdate commits and force-push them so new loader code looks like long-standing history. Check what this commit changed before cloning."
559
+ }];
560
+ }
561
+ const forcePushes = onBranch.filter((event) => event.activity_type === "force_push" && now - Date.parse(event.timestamp) < RECENT_FORCE_PUSH_DAYS * DAY_MS);
562
+ if (forcePushes.length > 0) {
563
+ const latest = forcePushes[0];
564
+ return [{
565
+ severity: "low",
566
+ confidence: "low",
567
+ cloneBlocking: false,
568
+ type: "GIT_HISTORY_TAMPERING",
569
+ pattern: "FORCE_PUSH",
570
+ description: `${branch} history was rewritten by a force push on ${latest.timestamp.slice(0, 10)}` + (forcePushes.length > 1 ? ` (${forcePushes.length} force pushes in the last ${RECENT_FORCE_PUSH_DAYS} days)` : ""),
571
+ codeExplanation: "Force pushes are common in personal repositories, but they also hide which code is new. Compare the current code with what you expected before running it."
572
+ }];
573
+ }
574
+ return [];
575
+ }
576
+ async function fetchHistorySignals(repoPath, branch, commitSha, headers) {
577
+ try {
578
+ const ref = encodeURIComponent(`refs/heads/${branch}`);
579
+ const [activityResponse, commitsResponse] = await Promise.all([
580
+ fetch(`https://api.github.com/repos/${repoPath}/activity?ref=${ref}&per_page=100`, { headers }),
581
+ fetch(`https://api.github.com/repos/${repoPath}/commits?sha=${commitSha}&per_page=100`, { headers })
582
+ ]);
583
+ if (!activityResponse.ok || !commitsResponse.ok)
584
+ return [];
585
+ const activity = await activityResponse.json();
586
+ const commits = await commitsResponse.json();
587
+ if (!Array.isArray(activity) || !Array.isArray(commits))
588
+ return [];
589
+ return detectHistoryTampering(activity, commits, branch);
590
+ } catch {
591
+ return [];
592
+ }
593
+ }
594
+
595
+ // node_modules/@flagrix/scanner-core/dist/github/osv-feed.js
596
+ var INDEXES = /* @__PURE__ */ new WeakMap();
597
+ function indexFeed(feed) {
598
+ let index = INDEXES.get(feed);
599
+ if (!index) {
600
+ const build = (ecosystem) => ({
601
+ all: new Set(ecosystem?.all ?? []),
602
+ versions: new Map(Object.entries(ecosystem?.versions ?? {}))
603
+ });
604
+ index = { npm: build(feed.npm), pypi: build(feed.pypi) };
605
+ INDEXES.set(feed, index);
606
+ }
607
+ return index;
608
+ }
609
+ function normalizePypiName(name) {
610
+ return name.toLowerCase().replace(/[-_.]+/g, "-");
611
+ }
612
+ var LOCAL_OR_URL_SPEC = /^(?:workspace:|file:|link:|portal:|git\+|git:|github:|gitlab:|bitbucket:|https?:)/;
613
+ function compareVersions(a, b) {
614
+ const [mainA = "", preA] = a.split("-", 2);
615
+ const [mainB = "", preB] = b.split("-", 2);
616
+ const partsA = mainA.split(".");
617
+ const partsB = mainB.split(".");
618
+ for (let index = 0; index < Math.max(partsA.length, partsB.length); index++) {
619
+ const diff = (Number.parseInt(partsA[index] ?? "0", 10) || 0) - (Number.parseInt(partsB[index] ?? "0", 10) || 0);
620
+ if (diff !== 0)
621
+ return Math.sign(diff);
622
+ }
623
+ if (preA === preB)
624
+ return 0;
625
+ if (preA === void 0)
626
+ return 1;
627
+ if (preB === void 0)
628
+ return -1;
629
+ return preA < preB ? -1 : 1;
630
+ }
631
+ var VERSION_TOKEN = /\d+(?:\.\d+){0,3}(?:-[0-9A-Za-z.]+)?/;
632
+ var RANGE_PART = /(>=|<=|<|>)\s*(\S+)/g;
633
+ function inAffected(version, entry) {
634
+ if (!/[<>]/.test(entry))
635
+ return compareVersions(version, entry) === 0;
636
+ for (const [, operator, bound] of entry.matchAll(RANGE_PART)) {
637
+ const order = compareVersions(version, bound);
638
+ if (operator === ">=" && order < 0 || operator === ">" && order <= 0 || operator === "<=" && order > 0 || operator === "<" && order >= 0) {
639
+ return false;
640
+ }
641
+ }
642
+ return true;
643
+ }
644
+ function declaredMatch(spec, affected) {
645
+ let specifier = spec.trim();
646
+ const alias = specifier.match(/^npm:.+@([^@]+)$/);
647
+ if (alias)
648
+ specifier = alias[1];
649
+ let result = null;
650
+ for (const part of specifier.split(/\s*\|\|\s*/)) {
651
+ const base = part.match(VERSION_TOKEN)?.[0];
652
+ if (!base || !affected.some((entry) => inAffected(base, entry)))
653
+ continue;
654
+ if (/^\s*(?:={1,3}|v)?\s*\d/.test(part) && !/[\s,]/.test(part.trim().replace(/^={1,3}\s*/, "")))
655
+ return "exact";
656
+ result = "range";
657
+ }
658
+ return result;
659
+ }
660
+ function matchOsvFeed(feed, dependencies, localPackageNames, alreadyFlagged) {
661
+ const index = indexFeed(feed);
662
+ const findings = [];
663
+ const reported = /* @__PURE__ */ new Set();
664
+ for (const dependency of dependencies) {
665
+ const { ecosystem, name, spec, file } = dependency;
666
+ const key = `${ecosystem}:${name}`;
667
+ if (reported.has(key) || alreadyFlagged.has(name) || localPackageNames.has(name))
668
+ continue;
669
+ if (ecosystem === "npm" && LOCAL_OR_URL_SPEC.test(spec.trim()))
670
+ continue;
671
+ const ecosystemIndex = index[ecosystem];
672
+ const lookup = ecosystem === "pypi" ? normalizePypiName(name) : name;
673
+ const everyVersion = ecosystemIndex.all.has(lookup);
674
+ const affected = ecosystemIndex.versions.get(lookup);
675
+ const match = everyVersion ? "exact" : affected ? declaredMatch(spec, affected) : null;
676
+ if (!match)
677
+ continue;
678
+ reported.add(key);
679
+ const registry = ecosystem === "npm" ? "npm" : "PyPI";
680
+ findings.push({
681
+ severity: "critical",
682
+ // A caret/tilde/>= range may resolve past the malicious versions.
683
+ confidence: match === "exact" ? "high" : "medium",
684
+ type: "SUSPICIOUS_DEPENDENCY",
685
+ pattern: "OSV_MALICIOUS_PACKAGE",
686
+ file,
687
+ package: name,
688
+ description: `Confirmed malicious ${registry} package (OSV): ${name}`,
689
+ codeExplanation: `\u2620\uFE0F The OpenSSF malicious-packages project lists "${name}" as malicious` + (everyVersion ? "" : ` in the version this repo declares (${spec.trim()})`) + `. Installing it runs the attacker's code. Details: https://osv.dev/list?ecosystem=${registry}&q=${encodeURIComponent(name)}`
690
+ });
691
+ }
692
+ return findings;
693
+ }
694
+ function npmDependencies(pkg, file) {
695
+ const out = [];
696
+ for (const section of ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"]) {
697
+ const deps = pkg[section];
698
+ if (!deps || typeof deps !== "object")
699
+ continue;
700
+ for (const [name, spec] of Object.entries(deps)) {
701
+ out.push({ ecosystem: "npm", name, spec: typeof spec === "string" ? spec : "", file });
702
+ }
703
+ }
704
+ return out;
705
+ }
706
+ var REQUIREMENT = /^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*(?:\[[^\]]*\])?\s*([^;#]*)/;
707
+ function pythonDependencies(content, file) {
708
+ let lines;
709
+ if (file.endsWith("setup.py")) {
710
+ const block = content.match(/install_requires\s*=\s*\[([^\]]{0,20000})\]/);
711
+ lines = block ? [...block[1].matchAll(/["']([^"'\n]+)["']/g)].map((m) => m[1]) : [];
712
+ } else {
713
+ lines = content.split("\n");
714
+ }
715
+ const out = [];
716
+ for (const line of lines) {
717
+ const trimmed = line.trim();
718
+ if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith("-") || trimmed.includes("://"))
719
+ continue;
720
+ const match = trimmed.match(REQUIREMENT);
721
+ if (match)
722
+ out.push({ ecosystem: "pypi", name: match[1], spec: match[2].trim(), file });
723
+ }
724
+ return out;
725
+ }
726
+
522
727
  // node_modules/@flagrix/scanner-core/dist/utils/risk-calculator.js
523
728
  function getSeverityWeight(severity) {
524
729
  const weights = {
@@ -530,16 +735,28 @@ function getSeverityWeight(severity) {
530
735
  };
531
736
  return weights[severity];
532
737
  }
533
- function calculateRawRiskScore(findings) {
534
- const strongestBySignal = /* @__PURE__ */ new Map();
738
+ function calculateSignalContributions(findings) {
739
+ const bySignal = /* @__PURE__ */ new Map();
535
740
  for (const finding of findings) {
536
741
  const confidenceMultiplier = finding.confidence === "low" ? 0.25 : finding.confidence === "medium" ? 0.6 : 1;
537
- const contribution = getSeverityWeight(finding.severity) * confidenceMultiplier;
742
+ const weight = getSeverityWeight(finding.severity) * confidenceMultiplier;
538
743
  const obfuscationHeuristic = finding.type === "OBFUSCATED_CODE" || finding.type === "CODE_INTEGRITY_ISSUE" && /minified|obfuscat/i.test(finding.description) || /^OBF_(?:BASE64|HEX|EVAL|NEW_FUNCTION|SETTIMEOUT|FROMCHARCODE|CHARCODE)/.test(finding.pattern ?? "");
539
744
  const signal = obfuscationHeuristic ? "OBFUSCATION_HEURISTIC" : finding.pattern ?? finding.type;
540
- strongestBySignal.set(signal, Math.max(strongestBySignal.get(signal) ?? 0, contribution));
745
+ const existing = bySignal.get(signal);
746
+ if (!existing) {
747
+ bySignal.set(signal, { signal, weight, finding, findingCount: 1 });
748
+ } else {
749
+ existing.findingCount++;
750
+ if (weight > existing.weight) {
751
+ existing.weight = weight;
752
+ existing.finding = finding;
753
+ }
754
+ }
541
755
  }
542
- return [...strongestBySignal.values()].reduce((sum, contribution) => sum + contribution, 0);
756
+ return [...bySignal.values()];
757
+ }
758
+ function calculateRawRiskScore(findings) {
759
+ return calculateSignalContributions(findings).reduce((sum, contribution) => sum + contribution.weight, 0);
543
760
  }
544
761
  function calculateRiskScore(findings) {
545
762
  return Math.min(1, calculateRawRiskScore(findings));
@@ -708,6 +925,79 @@ var SCANNABLE_EXTENSIONS = [
708
925
  ".html",
709
926
  ".htm"
710
927
  ];
928
+ var CONFIG_FILE_PATTERNS = [
929
+ /(?:^|\/)\.vscode\/(?:tasks|settings|launch|mcp)\.json$/,
930
+ /(?:^|\/)\.claude\/settings(?:\.local)?\.json$/,
931
+ /(?:^|\/)\.(?:cursor|amazonq|gemini|windsurf|roo|kiro)\/(?:mcp|settings)\.json$/,
932
+ /(?:^|\/)\.mcp\.json$/,
933
+ /^\.github\/workflows\/[^/]+\.ya?ml$/,
934
+ /^README(?:\.md|\.txt|\.rst)?$/i,
935
+ /(?:^|\/)(?:AGENTS|CLAUDE|GEMINI)\.md$/,
936
+ /(?:^|\/)\.(?:cursorrules|windsurfrules|clinerules)$/,
937
+ /(?:^|\/)\.cursor\/rules\/[^/]+\.mdc?$/,
938
+ /^\.github\/copilot-instructions\.md$/
939
+ ];
940
+ var UTF8 = new TextDecoder("utf-8");
941
+ function decodeUtf8(binary) {
942
+ const bytes = new Uint8Array(binary.length);
943
+ for (let index = 0; index < binary.length; index++)
944
+ bytes[index] = binary.charCodeAt(index);
945
+ return UTF8.decode(bytes);
946
+ }
947
+ function isConfigFile(path) {
948
+ return CONFIG_FILE_PATTERNS.some((pattern) => pattern.test(path));
949
+ }
950
+ var FONT_EXTENSIONS = [".woff", ".woff2", ".ttf", ".otf", ".eot"];
951
+ var MAX_FONT_FILES = 10;
952
+ var MAX_FONT_BYTES = 512 * 1024;
953
+ function isFontFile(path) {
954
+ return FONT_EXTENSIONS.some((ext) => path.toLowerCase().endsWith(ext));
955
+ }
956
+ var MAX_CONFIG_FILES = 25;
957
+ var MAX_COMMAND_TARGETS = 10;
958
+ var CI_WORKFLOW = /^\.github\/workflows\//;
959
+ var RUNTIME_INVOCATION = /\b(?:node|nodejs|bun|deno\s+run|tsx|ts-node)((?:\s+-{1,2}[\w-]+(?:=[^\s"'`]*)?)*)\s+\\?["']?([^\s"'`&|;<>()]+)/g;
960
+ var INLINE_CODE_FLAG = /(?:^|\s)(?:-[ep]|--eval|--print)(?:\s|=|$)/;
961
+ var RUNTIME_ARGS_FORM = /"command"\s*:\s*"(?:node|nodejs|bun|deno|tsx|ts-node)"\s*,\s*"args"\s*:\s*\[\s*"([^"]+)"/g;
962
+ var PROJECT_DIR_VARIABLE = /\\?["']?\$\{?(?:CLAUDE_PROJECT_DIR|workspaceFolder|workspaceRoot)\}?\\?["']?(?=\/)/g;
963
+ var DOT_DIR_CONFIG = /^(?:(.*)\/)?\.(?:vscode|claude|cursor|amazonq|gemini|windsurf|roo|kiro|github)\//;
964
+ function extractCommandTargets(content, configPath) {
965
+ const dotDir = configPath.match(DOT_DIR_CONFIG);
966
+ const base = dotDir ? dotDir[1] ?? "" : configPath.includes("/") ? configPath.slice(0, configPath.lastIndexOf("/")) : "";
967
+ const text = content.replace(PROJECT_DIR_VARIABLE, ".");
968
+ const targets = /* @__PURE__ */ new Set();
969
+ for (const match of text.matchAll(RUNTIME_INVOCATION)) {
970
+ if (INLINE_CODE_FLAG.test(match[1]))
971
+ continue;
972
+ const path = resolveRepoPath(base, match[2]);
973
+ if (path)
974
+ targets.add(path);
975
+ }
976
+ for (const match of text.matchAll(RUNTIME_ARGS_FORM)) {
977
+ const path = resolveRepoPath(base, match[1]);
978
+ if (path)
979
+ targets.add(path);
980
+ }
981
+ return [...targets];
982
+ }
983
+ function resolveRepoPath(base, raw) {
984
+ const target = raw.replace(/\\+$/, "");
985
+ if (!target || /^[-/~$]/.test(target) || target.includes("://"))
986
+ return null;
987
+ const segments = base ? base.split("/") : [];
988
+ for (const segment of target.split("/")) {
989
+ if (segment === "" || segment === ".")
990
+ continue;
991
+ if (segment === "..") {
992
+ if (segments.length === 0)
993
+ return null;
994
+ segments.pop();
995
+ } else {
996
+ segments.push(segment);
997
+ }
998
+ }
999
+ return segments.length > 0 ? segments.join("/") : null;
1000
+ }
711
1001
  var MAX_FILES_TO_SCAN = 200;
712
1002
  var MAX_FILE_SIZE_BYTES = 1024 * 1024;
713
1003
  var MAX_SKIPPED_FILES_LISTED = 500;
@@ -776,21 +1066,51 @@ async function scanGitHubRepo(repo, options) {
776
1066
  skippedFiles.push({ path, reason });
777
1067
  }
778
1068
  };
779
- const filesToScan = [];
1069
+ const extraFiles = [];
1070
+ let configSelected = 0;
1071
+ let fontsSelected = 0;
1072
+ const sourceBlobs = [];
780
1073
  for (const item of tree.tree) {
781
1074
  if (item.type !== "blob")
782
1075
  continue;
1076
+ const config = isConfigFile(item.path);
1077
+ const font = !config && isFontFile(item.path);
1078
+ if (config || font) {
1079
+ const sizeLimit = font ? MAX_FONT_BYTES : MAX_FILE_SIZE_BYTES;
1080
+ if (item.size !== void 0 && item.size > sizeLimit) {
1081
+ skip(item.path, "too-large");
1082
+ } else if (config ? configSelected >= MAX_CONFIG_FILES : fontsSelected >= MAX_FONT_FILES) {
1083
+ skip(item.path, "over-file-limit");
1084
+ } else {
1085
+ if (config)
1086
+ configSelected++;
1087
+ else
1088
+ fontsSelected++;
1089
+ extraFiles.push(item);
1090
+ }
1091
+ continue;
1092
+ }
783
1093
  const eligible = PRIORITY_FILES.some((pf) => item.path.endsWith(pf)) || SCANNABLE_EXTENSIONS.some((ext) => item.path.endsWith(ext));
784
1094
  if (!eligible) {
785
1095
  skip(item.path, "unsupported-type");
786
1096
  } else if (item.size !== void 0 && item.size > MAX_FILE_SIZE_BYTES) {
787
1097
  skip(item.path, "too-large");
788
- } else if (filesToScan.length >= MAX_FILES_TO_SCAN) {
1098
+ } else if (sourceBlobs.length >= MAX_FILES_TO_SCAN) {
789
1099
  skip(item.path, "over-file-limit");
790
1100
  } else {
791
- filesToScan.push(item);
1101
+ sourceBlobs.push(item);
792
1102
  }
793
1103
  }
1104
+ const filesToScan = [...extraFiles, ...sourceBlobs];
1105
+ const blobsByPath = new Map(tree.tree.filter((item) => item.type === "blob").map((item) => [item.path, item]));
1106
+ const queued = new Set(filesToScan.map((item) => item.path));
1107
+ const commandTargets = /* @__PURE__ */ new Set();
1108
+ const unskip = (path) => {
1109
+ const index = skippedFiles.findIndex((skipped) => skipped.path === path);
1110
+ if (index !== -1)
1111
+ skippedFiles.splice(index, 1);
1112
+ skippedCount--;
1113
+ };
794
1114
  const hiddenFiles = tree.tree.filter((item) => item.path.startsWith(".") && !item.path.startsWith(".github") && !item.path.startsWith(".git") && item.type === "blob");
795
1115
  for (const hidden of hiddenFiles) {
796
1116
  const basename = hidden.path.split("/").pop() ?? hidden.path;
@@ -809,6 +1129,8 @@ async function scanGitHubRepo(repo, options) {
809
1129
  const loadedRuleIds = new Set(yaraRules.map((r) => r.id));
810
1130
  const fileContents = [];
811
1131
  const scannedFiles = [];
1132
+ const declaredDependencies = [];
1133
+ const localPackageNames = /* @__PURE__ */ new Set();
812
1134
  for (const file of filesToScan) {
813
1135
  const contentUrl = `https://api.github.com/repos/${repo.owner}/${repo.repo}/contents/${file.path}?ref=${commitSha}`;
814
1136
  const contentResponse = await fetch(contentUrl, { headers });
@@ -819,18 +1141,43 @@ async function scanGitHubRepo(repo, options) {
819
1141
  filesScanned++;
820
1142
  scannedFiles.push(file.path);
821
1143
  const fileData = await contentResponse.json();
822
- const content = atob(fileData.content || "");
1144
+ const binary = atob(fileData.content || "");
1145
+ const content = isFontFile(file.path) ? binary : decodeUtf8(binary);
1146
+ if (isConfigFile(file.path) && !CI_WORKFLOW.test(file.path) || file.path.endsWith("package.json")) {
1147
+ for (const target of extractCommandTargets(content, file.path)) {
1148
+ if (commandTargets.size >= MAX_COMMAND_TARGETS)
1149
+ break;
1150
+ const item = blobsByPath.get(target);
1151
+ if (!item || queued.has(target))
1152
+ continue;
1153
+ if (item.size !== void 0 && item.size > MAX_FILE_SIZE_BYTES)
1154
+ continue;
1155
+ unskip(target);
1156
+ queued.add(target);
1157
+ commandTargets.add(target);
1158
+ filesToScan.push(item);
1159
+ }
1160
+ }
1161
+ const isSource = SCANNABLE_EXTENSIONS.some((ext) => file.path.endsWith(ext));
1162
+ const runsAsJs = commandTargets.has(file.path) && !isSource && !isFontFile(file.path);
823
1163
  const isHtml = /\.html?$/i.test(file.path);
824
- const isJsTs = /\.(?:[cm]?js|ts|[jt]sx)$/i.test(file.path);
1164
+ const isJsTs = runsAsJs || /\.(?:[cm]?js|ts|[jt]sx)$/i.test(file.path);
825
1165
  const scriptContent = isHtml ? maskExceptInlineScripts(content) : content;
826
1166
  let matchable = isJsTs || isHtml ? maskRegexLiterals(scriptContent) : scriptContent;
827
1167
  if (isJsTs && isTestFile(file.path)) {
828
1168
  matchable = maskStringLiterals(matchable);
829
1169
  }
830
- if (SCANNABLE_EXTENSIONS.some((ext) => file.path.endsWith(ext))) {
1170
+ if (isSource || runsAsJs) {
831
1171
  fileContents.push({ path: file.path, content: matchable });
832
1172
  }
833
1173
  if (file.path.endsWith("package.json")) {
1174
+ try {
1175
+ const pkg = JSON.parse(content);
1176
+ if (typeof pkg.name === "string")
1177
+ localPackageNames.add(pkg.name);
1178
+ declaredDependencies.push(...npmDependencies(pkg, file.path));
1179
+ } catch {
1180
+ }
834
1181
  const pkgFindings = await scanPackageJson(content, maliciousPackages, file.path);
835
1182
  findings.push(...pkgFindings);
836
1183
  patternsMatched += pkgFindings.length;
@@ -839,13 +1186,30 @@ async function scanGitHubRepo(repo, options) {
839
1186
  findings.push(...postinstallFindings);
840
1187
  patternsMatched += postinstallFindings.length;
841
1188
  }
1189
+ if (isFontFile(file.path)) {
1190
+ const masquerade = detectMasqueradedScript(content, file.path);
1191
+ findings.push(...masquerade);
1192
+ patternsMatched += masquerade.length;
1193
+ continue;
1194
+ }
1195
+ if (!isSource && (isConfigFile(file.path) || file.path.endsWith("package.json"))) {
1196
+ const configFindings = applyYaraRules(content, yaraRules, file.path);
1197
+ findings.push(...configFindings);
1198
+ patternsMatched += configFindings.length;
1199
+ if (/^README/i.test(file.path)) {
1200
+ const socialFindings = detectSocialEngineering(content, file.path);
1201
+ findings.push(...socialFindings);
1202
+ patternsMatched += socialFindings.length;
1203
+ }
1204
+ }
842
1205
  if (file.path.endsWith("requirements.txt") || file.path.endsWith("setup.py")) {
1206
+ declaredDependencies.push(...pythonDependencies(content, file.path));
843
1207
  const pyFindings = scanPythonDeps(content, maliciousPackages, file.path);
844
1208
  findings.push(...pyFindings);
845
1209
  patternsMatched += pyFindings.length;
846
1210
  }
847
- if (SCANNABLE_EXTENSIONS.some((ext) => file.path.endsWith(ext))) {
848
- const ruleScopePath = isHtml ? `${file.path}.js` : file.path;
1211
+ if (isSource || runsAsJs) {
1212
+ const ruleScopePath = isHtml || runsAsJs ? `${file.path}.js` : file.path;
849
1213
  const yaraFindings = applyYaraRules(matchable, yaraRules, file.path, ruleScopePath);
850
1214
  findings.push(...yaraFindings);
851
1215
  patternsMatched += yaraFindings.length;
@@ -854,6 +1218,12 @@ async function scanGitHubRepo(repo, options) {
854
1218
  patternsMatched += obfuscationFindings.length;
855
1219
  }
856
1220
  }
1221
+ if (options.osvFeed) {
1222
+ const alreadyFlagged = new Set(findings.filter((finding) => finding.type === "SUSPICIOUS_DEPENDENCY" && finding.package).map((finding) => finding.package));
1223
+ const osvFindings = matchOsvFeed(options.osvFeed, declaredDependencies, localPackageNames, alreadyFlagged);
1224
+ findings.push(...osvFindings);
1225
+ patternsMatched += osvFindings.length;
1226
+ }
857
1227
  const sourceFiles = fileContents.filter((f) => !isTestFile(f.path));
858
1228
  const languageFindings = await detectNonEnglishComments(sourceFiles, repo);
859
1229
  findings.push(...languageFindings);
@@ -895,6 +1265,9 @@ async function scanGitHubRepo(repo, options) {
895
1265
  const integrityFindings = await detectCodeIntegrityIssues(fileContents, allPaths, repo);
896
1266
  findings.push(...integrityFindings);
897
1267
  patternsMatched += integrityFindings.length;
1268
+ const historyFindings = await fetchHistorySignals(`${repo.owner}/${repo.repo}`, branch, commitSha, headers);
1269
+ findings.push(...historyFindings);
1270
+ patternsMatched += historyFindings.length;
898
1271
  const rawRiskScore = calculateRawRiskScore(findings);
899
1272
  const riskScore = calculateRiskScore(findings);
900
1273
  const riskLevel = getRiskLevel(riskScore, findings);
@@ -905,10 +1278,13 @@ async function scanGitHubRepo(repo, options) {
905
1278
  riskScore,
906
1279
  rawRiskScore,
907
1280
  riskLevel,
908
- factors: findings.map((f) => ({
909
- factor: f.type,
910
- weight: getSeverityWeight(f.severity),
911
- description: f.description
1281
+ // One factor per deduped signal, carrying the confidence-adjusted
1282
+ // weight actually counted — factors always sum to rawRiskScore, so
1283
+ // consumers can render a deduction breakdown that reconciles.
1284
+ factors: calculateSignalContributions(findings).map((c) => ({
1285
+ factor: c.finding.type,
1286
+ weight: c.weight,
1287
+ description: c.findingCount > 1 ? `${c.finding.description} (strongest of ${c.findingCount} matches counted once)` : c.finding.description
912
1288
  })),
913
1289
  disclaimer: DEFAULT_DISCLAIMER,
914
1290
  repo,
@@ -1038,6 +1414,37 @@ function scanPythonDeps(content, maliciousPackages, filePath) {
1038
1414
  }
1039
1415
  return findings;
1040
1416
  }
1417
+ var FONT_MAGIC = ["wOFF", "wOF2", "\0\0\0", "OTTO", "true", "ttcf"];
1418
+ var SCRIPT_TOKENS = /\b(?:require\s*\(|import\s+[\w{*]|function\s*\w*\s*\(|const\s+\w+\s*=|let\s+\w+\s*=|var\s+\w+\s*=|eval\s*\(|new\s+Function|module\.exports|process\.env|child_process)/;
1419
+ function detectMasqueradedScript(content, filePath) {
1420
+ if (FONT_MAGIC.some((magic) => content.startsWith(magic)))
1421
+ return [];
1422
+ if (filePath.toLowerCase().endsWith(".eot") && content.slice(34, 36) === "LP")
1423
+ return [];
1424
+ if (content.startsWith("version https://git-lfs.github.com/spec/"))
1425
+ return [];
1426
+ const head = content.slice(0, 8192);
1427
+ if (head.length === 0)
1428
+ return [];
1429
+ let printable = 0;
1430
+ for (let index = 0; index < head.length; index++) {
1431
+ const code = head.charCodeAt(index);
1432
+ if (code === 9 || code === 10 || code === 13 || code >= 32 && code < 127)
1433
+ printable++;
1434
+ }
1435
+ if (printable / head.length < 0.95 || !SCRIPT_TOKENS.test(content))
1436
+ return [];
1437
+ return [{
1438
+ severity: "critical",
1439
+ confidence: "high",
1440
+ type: "OBFUSCATED_CODE",
1441
+ pattern: "MASQUERADED_SCRIPT_FONT",
1442
+ file: filePath,
1443
+ description: `JavaScript disguised as a font file: ${filePath}`,
1444
+ codeSnippet: content.trimStart().slice(0, 200),
1445
+ codeExplanation: "\u{1F3AD} This file has a font extension but contains readable JavaScript, not font data. Fake font files are run with `node <file>` from VS Code tasks or hooks to hide a loader from code review."
1446
+ }];
1447
+ }
1041
1448
  var BUILTIN_OBFUSCATION_RULE_IDS = {
1042
1449
  base64: "OBF_BASE64_HEAVY",
1043
1450
  hex: "OBF_HEX_STRINGS",
@@ -1191,12 +1598,30 @@ function extractCodeSnippet(line, maxLength = 500) {
1191
1598
  }
1192
1599
  return line;
1193
1600
  }
1194
- var ENGLISH_CODES = ["eng", "sco"];
1195
- var MIN_WORD_LENGTH = 5;
1196
- var MIN_WORDS_TO_ANALYZE = 30;
1601
+ var LETTER = new RegExp("\\p{L}", "gu");
1602
+ var NON_LATIN_LETTER = new RegExp("(?!\\p{Script=Latin})\\p{L}", "gu");
1603
+ var NON_LATIN_SHARE_FOR_COMMENT = 0.5;
1604
+ var MIN_LETTERS_TO_ANALYZE = 150;
1197
1605
  var NON_ENGLISH_THRESHOLD = 0.25;
1198
- var MIN_NON_ENGLISH_WORDS = 8;
1199
- var MIN_COMMENT_LEN_FOR_DETECTION = 40;
1606
+ var MIN_NON_ENGLISH_LETTERS = 40;
1607
+ var COMMENT_DIRECTIVE = /^(?:eslint|@ts-|prettier-ignore|istanbul|c8\s|v8\s|jshint|tslint|biome-ignore|global\s|webpackChunkName|[#@]__PURE__|noinspection|type:\s*ignore|noqa|pylint:|fmt:|shellcheck)/i;
1608
+ var COMMENT_NON_PROSE = [
1609
+ /https?:\/\/\S+/g,
1610
+ /`[^`\n]*`/g,
1611
+ /@\w+(?:\s*\{[^}\n]{0,80}\})?/g,
1612
+ /(?<![\w$-])[\w$-]+(?:[.:/\\][\w$-]+)+/g,
1613
+ /\b[a-z]+[A-Z][\w$]*/g,
1614
+ /\b[\w$]*_[\w$]*/g,
1615
+ /[{}()[\]<>=;*#|"'+&!?^~\\-]+/g
1616
+ ];
1617
+ function commentProse(comment) {
1618
+ if (COMMENT_DIRECTIVE.test(comment))
1619
+ return null;
1620
+ let prose = comment;
1621
+ for (const pattern of COMMENT_NON_PROSE)
1622
+ prose = prose.replace(pattern, " ");
1623
+ return prose.replace(/\s+/g, " ").trim();
1624
+ }
1200
1625
  var COMMENT_PATTERNS = {
1201
1626
  javascript: [/\/\/(.*)$/gm, /\/\*[\s\S]*?\*\//g],
1202
1627
  typescript: [/\/\/(.*)$/gm, /\/\*[\s\S]*?\*\//g],
@@ -1230,40 +1655,38 @@ function extractComments(content, fileExtension) {
1230
1655
  return comments.map((comment) => comment.replace(/^\/\/\s*/, "").replace(/^\/\*\s*|\s*\*\/$/g, "").replace(/^#\s*/, "").replace(/^<#\s*|\s*#>$/g, "").replace(/^"""\s*|\s*"""$/g, "").replace(/^'''\s*|\s*'''$/g, "").replace(/^REM\s+/i, "").replace(/^::\s*/, "").trim()).filter((comment) => comment.length > 0);
1231
1656
  }
1232
1657
  function analyzeCommentLanguages(comments, filePath, fileAnalysis) {
1233
- let fileNonEnglishWords = 0;
1234
- for (const comment of comments) {
1235
- const words = comment.split(/\s+/).filter((word) => word.length >= MIN_WORD_LENGTH);
1236
- fileAnalysis.totalWords += words.length;
1658
+ let fileNonEnglishLetters = 0;
1659
+ for (const raw of comments) {
1660
+ const comment = commentProse(raw);
1661
+ if (!comment)
1662
+ continue;
1663
+ const letters = comment.match(LETTER)?.length ?? 0;
1664
+ if (letters === 0)
1665
+ continue;
1666
+ fileAnalysis.totalLetters += letters;
1237
1667
  fileAnalysis.totalComments += 1;
1238
- if (comment.length >= MIN_COMMENT_LEN_FOR_DETECTION) {
1239
- const detectedLang = franc(comment);
1240
- if (detectedLang && detectedLang !== "und") {
1241
- if (ENGLISH_CODES.includes(detectedLang)) {
1242
- fileAnalysis.englishWords += words.length;
1243
- } else {
1244
- fileAnalysis.nonEnglishWords += words.length;
1245
- fileNonEnglishWords += words.length;
1246
- const currentCount = fileAnalysis.detectedLanguages.get(detectedLang) || 0;
1247
- fileAnalysis.detectedLanguages.set(detectedLang, currentCount + words.length);
1248
- }
1249
- } else {
1250
- fileAnalysis.englishWords += words.length;
1251
- }
1252
- } else {
1253
- fileAnalysis.englishWords += words.length;
1668
+ const nonLatin = comment.match(NON_LATIN_LETTER)?.length ?? 0;
1669
+ if (nonLatin / letters < NON_LATIN_SHARE_FOR_COMMENT) {
1670
+ fileAnalysis.englishLetters += letters;
1671
+ continue;
1254
1672
  }
1673
+ const language = franc(comment, { minLength: 3 });
1674
+ const code = language === "und" ? "other" : language;
1675
+ fileAnalysis.nonEnglishLetters += letters;
1676
+ fileNonEnglishLetters += letters;
1677
+ fileAnalysis.detectedLanguages.set(code, (fileAnalysis.detectedLanguages.get(code) ?? 0) + letters);
1255
1678
  }
1256
- if (fileNonEnglishWords > 0) {
1257
- fileAnalysis.filesWithNonEnglish.set(filePath, fileNonEnglishWords);
1679
+ if (fileNonEnglishLetters > 0) {
1680
+ fileAnalysis.filesWithNonEnglish.set(filePath, fileNonEnglishLetters);
1258
1681
  }
1259
1682
  }
1260
1683
  async function detectNonEnglishComments(files, _repo) {
1261
1684
  const findings = [];
1262
1685
  const analysis = {
1263
1686
  totalComments: 0,
1264
- totalWords: 0,
1265
- englishWords: 0,
1266
- nonEnglishWords: 0,
1687
+ totalLetters: 0,
1688
+ englishLetters: 0,
1689
+ nonEnglishLetters: 0,
1267
1690
  nonEnglishPercentage: 0,
1268
1691
  detectedLanguages: /* @__PURE__ */ new Map(),
1269
1692
  filesWithNonEnglish: /* @__PURE__ */ new Map()
@@ -1275,21 +1698,20 @@ async function detectNonEnglishComments(files, _repo) {
1275
1698
  analyzeCommentLanguages(comments, file.path, analysis);
1276
1699
  }
1277
1700
  }
1278
- if (analysis.totalWords >= MIN_WORDS_TO_ANALYZE) {
1279
- analysis.nonEnglishPercentage = analysis.nonEnglishWords / analysis.totalWords;
1280
- if (analysis.nonEnglishPercentage >= NON_ENGLISH_THRESHOLD && analysis.nonEnglishWords >= MIN_NON_ENGLISH_WORDS) {
1701
+ if (analysis.totalLetters >= MIN_LETTERS_TO_ANALYZE) {
1702
+ analysis.nonEnglishPercentage = analysis.nonEnglishLetters / analysis.totalLetters;
1703
+ if (analysis.nonEnglishPercentage >= NON_ENGLISH_THRESHOLD && analysis.nonEnglishLetters >= MIN_NON_ENGLISH_LETTERS) {
1281
1704
  const topFiles = Array.from(analysis.filesWithNonEnglish.entries()).sort((a, b) => b[1] - a[1]).slice(0, 3).map(([path]) => path);
1282
- const langBreakdown = Array.from(analysis.detectedLanguages.entries()).map(([code, count]) => `${getLanguageName(code)}: ${count} words`).join(", ");
1283
- let severity = "medium";
1284
- let description = "";
1285
- if (analysis.englishWords === 0) {
1286
- severity = "high";
1287
- description = `100% of code comments are in non-English languages (${langBreakdown}). Repository contains NO English comments, which is highly suspicious for projects claiming international scope.`;
1288
- } else {
1289
- const percentage = Math.round(analysis.nonEnglishPercentage * 100);
1290
- description = `${percentage}% of code comments are in non-English languages (${langBreakdown}). This may indicate deceptive code where malicious intent is hidden in foreign language comments.`;
1291
- }
1292
- findings.push({ severity, type: "NON_ENGLISH_COMMENTS", description, files: topFiles });
1705
+ const langBreakdown = Array.from(analysis.detectedLanguages.entries()).sort((a, b) => b[1] - a[1]).map(([code, count]) => `${getLanguageName(code)}: ${count} characters`).join(", ");
1706
+ const percentage = Math.round(analysis.nonEnglishPercentage * 100);
1707
+ findings.push({
1708
+ severity: "low",
1709
+ confidence: "low",
1710
+ cloneBlocking: false,
1711
+ type: "NON_ENGLISH_COMMENTS",
1712
+ description: `${percentage}% of code comment text is in languages other than English (${langBreakdown}). Normal for many projects; shown for context only.`,
1713
+ files: topFiles
1714
+ });
1293
1715
  }
1294
1716
  }
1295
1717
  return findings;
@@ -1299,6 +1721,7 @@ function getLanguageName(code) {
1299
1721
  tur: "Turkish",
1300
1722
  rus: "Russian",
1301
1723
  zho: "Chinese",
1724
+ cmn: "Chinese",
1302
1725
  jpn: "Japanese",
1303
1726
  kor: "Korean",
1304
1727
  ara: "Arabic",
@@ -1314,7 +1737,12 @@ function getLanguageName(code) {
1314
1737
  tha: "Thai",
1315
1738
  ind: "Indonesian",
1316
1739
  heb: "Hebrew",
1317
- hin: "Hindi"
1740
+ hin: "Hindi",
1741
+ zlm: "Malay",
1742
+ ron: "Romanian",
1743
+ arb: "Arabic",
1744
+ bel: "Belarusian",
1745
+ other: "Other"
1318
1746
  };
1319
1747
  return names[code] || code;
1320
1748
  }
@@ -2209,6 +2637,68 @@ function describeSource(loaded) {
2209
2637
  }
2210
2638
  }
2211
2639
 
2640
+ // src/lib/osv-feed.ts
2641
+ import { mkdir as mkdir2, readFile as readFile2, writeFile as writeFile2 } from "fs/promises";
2642
+ import { homedir as homedir2 } from "os";
2643
+ import { join as join2 } from "path";
2644
+ var DEFAULT_OSV_FEED_URL = "https://raw.githubusercontent.com/flagrix-io/flagrix-detection-rules/main/osv-malicious.json";
2645
+ var CACHE_TTL_MS2 = 6 * 60 * 60 * 1e3;
2646
+ function feedUrl() {
2647
+ return process.env.FLAGRIX_OSV_FEED_URL || DEFAULT_OSV_FEED_URL;
2648
+ }
2649
+ function cacheFilePath2() {
2650
+ const base = process.env.FLAGRIX_CACHE_DIR || process.env.XDG_CACHE_HOME || join2(homedir2(), ".cache");
2651
+ return join2(base, "flagrix", "osv-malicious.json");
2652
+ }
2653
+ function isFeed(data2) {
2654
+ return data2?.schemaVersion === 1 && Array.isArray(data2.npm?.all) && typeof data2.npm?.versions === "object" && Array.isArray(data2.pypi?.all) && typeof data2.pypi?.versions === "object";
2655
+ }
2656
+ async function readCache2() {
2657
+ try {
2658
+ return JSON.parse(await readFile2(cacheFilePath2(), "utf8"));
2659
+ } catch {
2660
+ return null;
2661
+ }
2662
+ }
2663
+ async function writeCache2(entry) {
2664
+ try {
2665
+ const path = cacheFilePath2();
2666
+ await mkdir2(join2(path, ".."), { recursive: true });
2667
+ await writeFile2(path, JSON.stringify(entry));
2668
+ } catch {
2669
+ }
2670
+ }
2671
+ async function loadOsvFeed() {
2672
+ if (process.env.FLAGRIX_OSV_FEED === "0") return void 0;
2673
+ const cached = await readCache2();
2674
+ const cachedFeed = cached && isFeed(cached.data) ? cached.data : void 0;
2675
+ if (cachedFeed && Date.now() - new Date(cached.fetchedAt).getTime() < CACHE_TTL_MS2) {
2676
+ return cachedFeed;
2677
+ }
2678
+ try {
2679
+ const response = await fetch(feedUrl(), {
2680
+ headers: cachedFeed && cached?.etag ? { "If-None-Match": cached.etag } : {}
2681
+ });
2682
+ if (response.status === 304 && cachedFeed) {
2683
+ await writeCache2({ ...cached, fetchedAt: (/* @__PURE__ */ new Date()).toISOString() });
2684
+ return cachedFeed;
2685
+ }
2686
+ if (response.ok) {
2687
+ const data2 = await response.json();
2688
+ if (isFeed(data2)) {
2689
+ await writeCache2({
2690
+ fetchedAt: (/* @__PURE__ */ new Date()).toISOString(),
2691
+ etag: response.headers.get("etag") ?? void 0,
2692
+ data: data2
2693
+ });
2694
+ return data2;
2695
+ }
2696
+ }
2697
+ } catch {
2698
+ }
2699
+ return cachedFeed;
2700
+ }
2701
+
2212
2702
  // src/lib/render.ts
2213
2703
  var useColor = process.stdout.isTTY && !process.env.NO_COLOR;
2214
2704
  var paint = (code) => (s) => useColor ? `\x1B[${code}m${s}\x1B[0m` : s;
@@ -2303,6 +2793,7 @@ export {
2303
2793
  parseRepoTarget,
2304
2794
  loadSignatures,
2305
2795
  describeSource,
2796
+ loadOsvFeed,
2306
2797
  securityScore,
2307
2798
  renderRepoResult,
2308
2799
  renderUserResult,