flagrix 0.2.0 → 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.
package/README.md CHANGED
@@ -65,6 +65,13 @@ calls go to the GitHub/npm APIs and the public
65
65
  [detection-rules](https://github.com/flagrix-io/flagrix-detection-rules)
66
66
  repository (signature refresh, cached 6 h, with a bundled offline snapshot).
67
67
 
68
+ Dependencies are also checked against the OSV malicious-package feed
69
+ (`osv-malicious.json` from the same repository: every npm/PyPI package the
70
+ OpenSSF malicious-packages project has confirmed malicious). The feed is
71
+ downloaded and matched locally, so dependency names never leave your machine.
72
+ It is cached 6 h and revalidated with its ETag. Set `FLAGRIX_OSV_FEED=0` to
73
+ skip it (the curated list in the signatures still applies).
74
+
68
75
  ## How it works
69
76
 
70
77
  Scanning logic lives in [@flagrix/scanner-core](https://github.com/flagrix-io/flagrix-scanner-core)
@@ -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 = {
@@ -749,6 +954,50 @@ function isFontFile(path) {
749
954
  return FONT_EXTENSIONS.some((ext) => path.toLowerCase().endsWith(ext));
750
955
  }
751
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
+ }
752
1001
  var MAX_FILES_TO_SCAN = 200;
753
1002
  var MAX_FILE_SIZE_BYTES = 1024 * 1024;
754
1003
  var MAX_SKIPPED_FILES_LISTED = 500;
@@ -853,6 +1102,15 @@ async function scanGitHubRepo(repo, options) {
853
1102
  }
854
1103
  }
855
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
+ };
856
1114
  const hiddenFiles = tree.tree.filter((item) => item.path.startsWith(".") && !item.path.startsWith(".github") && !item.path.startsWith(".git") && item.type === "blob");
857
1115
  for (const hidden of hiddenFiles) {
858
1116
  const basename = hidden.path.split("/").pop() ?? hidden.path;
@@ -871,6 +1129,8 @@ async function scanGitHubRepo(repo, options) {
871
1129
  const loadedRuleIds = new Set(yaraRules.map((r) => r.id));
872
1130
  const fileContents = [];
873
1131
  const scannedFiles = [];
1132
+ const declaredDependencies = [];
1133
+ const localPackageNames = /* @__PURE__ */ new Set();
874
1134
  for (const file of filesToScan) {
875
1135
  const contentUrl = `https://api.github.com/repos/${repo.owner}/${repo.repo}/contents/${file.path}?ref=${commitSha}`;
876
1136
  const contentResponse = await fetch(contentUrl, { headers });
@@ -883,17 +1143,41 @@ async function scanGitHubRepo(repo, options) {
883
1143
  const fileData = await contentResponse.json();
884
1144
  const binary = atob(fileData.content || "");
885
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);
886
1163
  const isHtml = /\.html?$/i.test(file.path);
887
- const isJsTs = /\.(?:[cm]?js|ts|[jt]sx)$/i.test(file.path);
1164
+ const isJsTs = runsAsJs || /\.(?:[cm]?js|ts|[jt]sx)$/i.test(file.path);
888
1165
  const scriptContent = isHtml ? maskExceptInlineScripts(content) : content;
889
1166
  let matchable = isJsTs || isHtml ? maskRegexLiterals(scriptContent) : scriptContent;
890
1167
  if (isJsTs && isTestFile(file.path)) {
891
1168
  matchable = maskStringLiterals(matchable);
892
1169
  }
893
- if (SCANNABLE_EXTENSIONS.some((ext) => file.path.endsWith(ext))) {
1170
+ if (isSource || runsAsJs) {
894
1171
  fileContents.push({ path: file.path, content: matchable });
895
1172
  }
896
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
+ }
897
1181
  const pkgFindings = await scanPackageJson(content, maliciousPackages, file.path);
898
1182
  findings.push(...pkgFindings);
899
1183
  patternsMatched += pkgFindings.length;
@@ -908,7 +1192,7 @@ async function scanGitHubRepo(repo, options) {
908
1192
  patternsMatched += masquerade.length;
909
1193
  continue;
910
1194
  }
911
- if (!SCANNABLE_EXTENSIONS.some((ext) => file.path.endsWith(ext)) && (isConfigFile(file.path) || file.path.endsWith("package.json"))) {
1195
+ if (!isSource && (isConfigFile(file.path) || file.path.endsWith("package.json"))) {
912
1196
  const configFindings = applyYaraRules(content, yaraRules, file.path);
913
1197
  findings.push(...configFindings);
914
1198
  patternsMatched += configFindings.length;
@@ -919,12 +1203,13 @@ async function scanGitHubRepo(repo, options) {
919
1203
  }
920
1204
  }
921
1205
  if (file.path.endsWith("requirements.txt") || file.path.endsWith("setup.py")) {
1206
+ declaredDependencies.push(...pythonDependencies(content, file.path));
922
1207
  const pyFindings = scanPythonDeps(content, maliciousPackages, file.path);
923
1208
  findings.push(...pyFindings);
924
1209
  patternsMatched += pyFindings.length;
925
1210
  }
926
- if (SCANNABLE_EXTENSIONS.some((ext) => file.path.endsWith(ext))) {
927
- const ruleScopePath = isHtml ? `${file.path}.js` : file.path;
1211
+ if (isSource || runsAsJs) {
1212
+ const ruleScopePath = isHtml || runsAsJs ? `${file.path}.js` : file.path;
928
1213
  const yaraFindings = applyYaraRules(matchable, yaraRules, file.path, ruleScopePath);
929
1214
  findings.push(...yaraFindings);
930
1215
  patternsMatched += yaraFindings.length;
@@ -933,6 +1218,12 @@ async function scanGitHubRepo(repo, options) {
933
1218
  patternsMatched += obfuscationFindings.length;
934
1219
  }
935
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
+ }
936
1227
  const sourceFiles = fileContents.filter((f) => !isTestFile(f.path));
937
1228
  const languageFindings = await detectNonEnglishComments(sourceFiles, repo);
938
1229
  findings.push(...languageFindings);
@@ -974,6 +1265,9 @@ async function scanGitHubRepo(repo, options) {
974
1265
  const integrityFindings = await detectCodeIntegrityIssues(fileContents, allPaths, repo);
975
1266
  findings.push(...integrityFindings);
976
1267
  patternsMatched += integrityFindings.length;
1268
+ const historyFindings = await fetchHistorySignals(`${repo.owner}/${repo.repo}`, branch, commitSha, headers);
1269
+ findings.push(...historyFindings);
1270
+ patternsMatched += historyFindings.length;
977
1271
  const rawRiskScore = calculateRawRiskScore(findings);
978
1272
  const riskScore = calculateRiskScore(findings);
979
1273
  const riskLevel = getRiskLevel(riskScore, findings);
@@ -1304,12 +1598,30 @@ function extractCodeSnippet(line, maxLength = 500) {
1304
1598
  }
1305
1599
  return line;
1306
1600
  }
1307
- var ENGLISH_CODES = ["eng", "sco"];
1308
- var MIN_WORD_LENGTH = 5;
1309
- 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;
1310
1605
  var NON_ENGLISH_THRESHOLD = 0.25;
1311
- var MIN_NON_ENGLISH_WORDS = 8;
1312
- 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
+ }
1313
1625
  var COMMENT_PATTERNS = {
1314
1626
  javascript: [/\/\/(.*)$/gm, /\/\*[\s\S]*?\*\//g],
1315
1627
  typescript: [/\/\/(.*)$/gm, /\/\*[\s\S]*?\*\//g],
@@ -1343,40 +1655,38 @@ function extractComments(content, fileExtension) {
1343
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);
1344
1656
  }
1345
1657
  function analyzeCommentLanguages(comments, filePath, fileAnalysis) {
1346
- let fileNonEnglishWords = 0;
1347
- for (const comment of comments) {
1348
- const words = comment.split(/\s+/).filter((word) => word.length >= MIN_WORD_LENGTH);
1349
- 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;
1350
1667
  fileAnalysis.totalComments += 1;
1351
- if (comment.length >= MIN_COMMENT_LEN_FOR_DETECTION) {
1352
- const detectedLang = franc(comment);
1353
- if (detectedLang && detectedLang !== "und") {
1354
- if (ENGLISH_CODES.includes(detectedLang)) {
1355
- fileAnalysis.englishWords += words.length;
1356
- } else {
1357
- fileAnalysis.nonEnglishWords += words.length;
1358
- fileNonEnglishWords += words.length;
1359
- const currentCount = fileAnalysis.detectedLanguages.get(detectedLang) || 0;
1360
- fileAnalysis.detectedLanguages.set(detectedLang, currentCount + words.length);
1361
- }
1362
- } else {
1363
- fileAnalysis.englishWords += words.length;
1364
- }
1365
- } else {
1366
- 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;
1367
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);
1368
1678
  }
1369
- if (fileNonEnglishWords > 0) {
1370
- fileAnalysis.filesWithNonEnglish.set(filePath, fileNonEnglishWords);
1679
+ if (fileNonEnglishLetters > 0) {
1680
+ fileAnalysis.filesWithNonEnglish.set(filePath, fileNonEnglishLetters);
1371
1681
  }
1372
1682
  }
1373
1683
  async function detectNonEnglishComments(files, _repo) {
1374
1684
  const findings = [];
1375
1685
  const analysis = {
1376
1686
  totalComments: 0,
1377
- totalWords: 0,
1378
- englishWords: 0,
1379
- nonEnglishWords: 0,
1687
+ totalLetters: 0,
1688
+ englishLetters: 0,
1689
+ nonEnglishLetters: 0,
1380
1690
  nonEnglishPercentage: 0,
1381
1691
  detectedLanguages: /* @__PURE__ */ new Map(),
1382
1692
  filesWithNonEnglish: /* @__PURE__ */ new Map()
@@ -1388,21 +1698,20 @@ async function detectNonEnglishComments(files, _repo) {
1388
1698
  analyzeCommentLanguages(comments, file.path, analysis);
1389
1699
  }
1390
1700
  }
1391
- if (analysis.totalWords >= MIN_WORDS_TO_ANALYZE) {
1392
- analysis.nonEnglishPercentage = analysis.nonEnglishWords / analysis.totalWords;
1393
- 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) {
1394
1704
  const topFiles = Array.from(analysis.filesWithNonEnglish.entries()).sort((a, b) => b[1] - a[1]).slice(0, 3).map(([path]) => path);
1395
- const langBreakdown = Array.from(analysis.detectedLanguages.entries()).map(([code, count]) => `${getLanguageName(code)}: ${count} words`).join(", ");
1396
- let severity = "medium";
1397
- let description = "";
1398
- if (analysis.englishWords === 0) {
1399
- severity = "high";
1400
- 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.`;
1401
- } else {
1402
- const percentage = Math.round(analysis.nonEnglishPercentage * 100);
1403
- 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.`;
1404
- }
1405
- 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
+ });
1406
1715
  }
1407
1716
  }
1408
1717
  return findings;
@@ -1412,6 +1721,7 @@ function getLanguageName(code) {
1412
1721
  tur: "Turkish",
1413
1722
  rus: "Russian",
1414
1723
  zho: "Chinese",
1724
+ cmn: "Chinese",
1415
1725
  jpn: "Japanese",
1416
1726
  kor: "Korean",
1417
1727
  ara: "Arabic",
@@ -1427,7 +1737,12 @@ function getLanguageName(code) {
1427
1737
  tha: "Thai",
1428
1738
  ind: "Indonesian",
1429
1739
  heb: "Hebrew",
1430
- hin: "Hindi"
1740
+ hin: "Hindi",
1741
+ zlm: "Malay",
1742
+ ron: "Romanian",
1743
+ arb: "Arabic",
1744
+ bel: "Belarusian",
1745
+ other: "Other"
1431
1746
  };
1432
1747
  return names[code] || code;
1433
1748
  }
@@ -2322,6 +2637,68 @@ function describeSource(loaded) {
2322
2637
  }
2323
2638
  }
2324
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
+
2325
2702
  // src/lib/render.ts
2326
2703
  var useColor = process.stdout.isTTY && !process.env.NO_COLOR;
2327
2704
  var paint = (code) => (s) => useColor ? `\x1B[${code}m${s}\x1B[0m` : s;
@@ -2416,6 +2793,7 @@ export {
2416
2793
  parseRepoTarget,
2417
2794
  loadSignatures,
2418
2795
  describeSource,
2796
+ loadOsvFeed,
2419
2797
  securityScore,
2420
2798
  renderRepoResult,
2421
2799
  renderUserResult,
package/dist/cli.js CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  EXIT,
4
4
  describeSource,
5
5
  exitCodeFor,
6
+ loadOsvFeed,
6
7
  loadSignatures,
7
8
  parseRepoTarget,
8
9
  renderRepoResult,
@@ -11,7 +12,7 @@ import {
11
12
  scanGitHubRepo,
12
13
  scanGitHubUser,
13
14
  wantJson
14
- } from "./chunk-EBYRT2M7.js";
15
+ } from "./chunk-34LISSSS.js";
15
16
 
16
17
  // src/cli.ts
17
18
  import { parseArgs } from "util";
@@ -29,10 +30,12 @@ async function runScan(target, options) {
29
30
  const loaded = await loadSignatures();
30
31
  const note = describeSource(loaded);
31
32
  if (note) console.error(`flagrix: ${note}`);
32
- const result = await scanGitHubRepo(repo, {
33
+ const scanOptions = {
33
34
  signatures: loaded.signatures,
34
- githubToken: resolveToken(options.token)
35
- });
35
+ githubToken: resolveToken(options.token),
36
+ osvFeed: await loadOsvFeed()
37
+ };
38
+ const result = await scanGitHubRepo(repo, scanOptions);
36
39
  if (wantJson(options.json)) {
37
40
  console.log(JSON.stringify(result, null, 2));
38
41
  } else {
@@ -132,7 +135,7 @@ async function main() {
132
135
  }
133
136
  return runScanUser(target, { json: values.json, token: values.token });
134
137
  case "mcp": {
135
- const { runMcp } = await import("./mcp-7DWCHBWC.js");
138
+ const { runMcp } = await import("./mcp-7CQFVW5O.js");
136
139
  return runMcp();
137
140
  }
138
141
  default:
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  describeSource,
4
+ loadOsvFeed,
4
5
  loadSignatures,
5
6
  parseRepoTarget,
6
7
  resolveToken,
7
8
  scanGitHubRepo,
8
9
  scanGitHubUser,
9
10
  securityScore
10
- } from "./chunk-EBYRT2M7.js";
11
+ } from "./chunk-34LISSSS.js";
11
12
 
12
13
  // src/commands/mcp.ts
13
14
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -33,10 +34,12 @@ async function runMcp() {
33
34
  async ({ target, ref }) => {
34
35
  const repo = parseRepoTarget(target, ref);
35
36
  const loaded = await loadSignatures();
36
- const result = await scanGitHubRepo(repo, {
37
+ const scanOptions = {
37
38
  signatures: loaded.signatures,
38
- githubToken: resolveToken()
39
- });
39
+ githubToken: resolveToken(),
40
+ osvFeed: await loadOsvFeed()
41
+ };
42
+ const result = await scanGitHubRepo(repo, scanOptions);
40
43
  const note = describeSource(loaded);
41
44
  const summary = [
42
45
  `${result.riskLevel.toUpperCase()} risk \u2014 security score ${securityScore(result.riskScore)}/100 (verdict pinned to commit ${result.commitSha ?? "unknown"}).`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flagrix",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Scan GitHub repos and profiles for malware before you clone — CLI and MCP server for the Flagrix scanner",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -53,7 +53,7 @@
53
53
  "zod": "^3.24.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@flagrix/scanner-core": "^0.4.0",
56
+ "@flagrix/scanner-core": "^0.5.0",
57
57
  "@types/node": "^20.0.0",
58
58
  "tsup": "^8.0.0",
59
59
  "typescript": "^5.0.0",