repometry 0.3.1 → 0.4.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.
Files changed (46) hide show
  1. package/package.json +1 -1
  2. package/target/main/resources/report/json/report-v2.schema.json +12 -1
  3. package/target/main/resources/viewer/files.css +6 -2
  4. package/target/main/ts/api/git/git.d.ts +1 -1
  5. package/target/main/ts/api/git/git.js +21 -5
  6. package/target/main/ts/api/git/inspection-source.d.ts +1 -1
  7. package/target/main/ts/api/git/inspection-source.js +10 -4
  8. package/target/main/ts/api/git/local.d.ts +1 -1
  9. package/target/main/ts/api/git/local.js +29 -10
  10. package/target/main/ts/api/worker/worker-protocol.js +34 -9
  11. package/target/main/ts/compose/analyze-snapshot.d.ts +1 -0
  12. package/target/main/ts/compose/analyze-snapshot.js +50 -17
  13. package/target/main/ts/compose/inspect.js +2 -2
  14. package/target/main/ts/compose/plugin-list.js +2 -1
  15. package/target/main/ts/compose/security-plugins.d.ts +1 -0
  16. package/target/main/ts/compose/security-plugins.js +7 -0
  17. package/target/main/ts/core/engine/manifest.js +3 -2
  18. package/target/main/ts/core/engine/plugin.d.ts +2 -0
  19. package/target/main/ts/core/model/line-coverage.d.ts +2 -0
  20. package/target/main/ts/core/model/line-coverage.js +19 -1
  21. package/target/main/ts/core/model/source.d.ts +3 -0
  22. package/target/main/ts/core/model/source.js +4 -1
  23. package/target/main/ts/core/usecases/inspect.js +2 -2
  24. package/target/main/ts/plugins/coverage/coverage.d.ts +4 -3
  25. package/target/main/ts/plugins/coverage/coverage.js +46 -17
  26. package/target/main/ts/plugins/language-js/js-security.d.ts +4 -0
  27. package/target/main/ts/plugins/language-js/js-security.js +508 -0
  28. package/target/main/ts/plugins/security/actions-security.d.ts +2 -0
  29. package/target/main/ts/plugins/security/actions-security.js +581 -0
  30. package/target/main/ts/plugins/security/dockerfile.d.ts +2 -0
  31. package/target/main/ts/plugins/security/dockerfile.js +488 -0
  32. package/target/main/ts/plugins/security/npm-supply-chain.d.ts +4 -0
  33. package/target/main/ts/plugins/security/npm-supply-chain.js +368 -0
  34. package/target/main/ts/plugins/security/secrets.d.ts +12 -0
  35. package/target/main/ts/plugins/security/secrets.js +254 -0
  36. package/target/main/ts/report/html/app/viewer-files.d.ts +7 -0
  37. package/target/main/ts/report/html/app/viewer-files.js +17 -3
  38. package/target/main/ts/report/html/app/viewer-filter.js +8 -0
  39. package/target/main/ts/report/html/app/viewer-source.d.ts +6 -0
  40. package/target/main/ts/report/html/app/viewer-source.js +34 -7
  41. package/target/main/ts/report/html/site/badges.d.ts +1 -4
  42. package/target/main/ts/report/html/site/badges.js +28 -19
  43. package/target/main/ts/report/json/projections/commit-summary.js +2 -11
  44. package/target/main/ts/report/json/projections/inspection-context.js +6 -12
  45. package/target/main/ts/report/json/projections/security-summary.d.ts +11 -0
  46. package/target/main/ts/report/json/projections/security-summary.js +22 -0
@@ -1 +1,4 @@
1
- export {};
1
+ export const maxClaimedFileBytes = 1024 * 1024;
2
+ export function isClaimedText(bytes) {
3
+ return bytes.byteLength <= maxClaimedFileBytes && !bytes.subarray(0, 8192).includes(0);
4
+ }
@@ -2,7 +2,7 @@ import { isDeepStrictEqual } from 'node:util';
2
2
  import { isRecord } from '../engine/plugin.js';
3
3
  import { normalizeFindingIgnore, partitionFindings } from '../model/finding-ignore.js';
4
4
  import { reportSchema } from '../model/report.js';
5
- import { digest } from '../model/snapshot.js';
5
+ import { digest, isSourcePath } from '../model/snapshot.js';
6
6
  import { normalizeExclusions, sourceExclusionMatcher } from '../profile/exclusions.js';
7
7
  export async function executeInspection(options, ports) {
8
8
  const excludedPaths = normalizeExclusions(options.exclude ?? []);
@@ -28,7 +28,7 @@ export async function executeInspection(options, ports) {
28
28
  appliedConfig: { ...findings.appliedConfig, findingIgnore: ignore },
29
29
  ...(findings.findings === undefined ? {} : partitionFindings(findings.findings, ignore)) };
30
30
  const result = snapshot.subject.kind === 'commit' && ports.recordChurn
31
- ? await ports.recordChurn(filtered, root, snapshot.files.map(file => file.path)) : filtered;
31
+ ? await ports.recordChurn(filtered, root, snapshot.files.filter(file => isSourcePath(file.path)).map(file => file.path)) : filtered;
32
32
  await source.save(result);
33
33
  return result;
34
34
  }
@@ -5,6 +5,7 @@ export interface CoverageInput {
5
5
  readonly text: string;
6
6
  readonly root: string;
7
7
  readonly sourceLines: ReadonlyMap<string, number>;
8
+ readonly codeLines?: ReadonlyMap<string, ReadonlySet<number>>;
8
9
  readonly excludes?: (path: string) => boolean;
9
10
  }
10
11
  interface CoverageResult {
@@ -18,9 +19,9 @@ interface CoverageResult {
18
19
  export declare const lcovInput: DataContract<CoverageInput>;
19
20
  export declare const coverageResult: DataContract<CoverageResult>;
20
21
  export declare const coverage: Plugin;
21
- export declare function mapCoverage({ text, root, sourceLines, excludes }: CoverageInput): {
22
- files: Map<string, Map<number, boolean>>;
23
- branches: Map<string, Map<string, boolean>>;
22
+ export declare function mapCoverage({ text, root, sourceLines, codeLines, excludes }: CoverageInput): {
23
+ files: Map<string, Map<number, number>>;
24
+ branches: Map<string, Map<string, number>>;
24
25
  functions: Map<string, Map<string, boolean>>;
25
26
  gaps: Map<string, string>;
26
27
  };
@@ -8,6 +8,7 @@ export const lcovInput = {
8
8
  id: 'repometry.lcov-input/v1',
9
9
  accepts: (value) => isRecord(value) && typeof value.text === 'string'
10
10
  && typeof value.root === 'string' && value.sourceLines instanceof Map
11
+ && (value.codeLines === undefined || value.codeLines instanceof Map)
11
12
  && (value.excludes === undefined || typeof value.excludes === 'function'),
12
13
  };
13
14
  export const coverageResult = {
@@ -35,7 +36,8 @@ export const coverage = {
35
36
  { id: definitions.coveredFunctions, label: 'Covered functions', unit: 'count', scope: 'file', aggregate: 'sum', better: 'higher', group: 'Coverage' },
36
37
  { id: definitions.coverableFunctions, label: 'Measured functions', unit: 'count', scope: 'file', aggregate: 'sum', better: 'none', group: 'Coverage' },
37
38
  ] }, { contract: lcovLines.id, persistence: 'report',
38
- schema: { ...reportSchema.$defs.lcovLines, $defs: { lcovLineRanges: reportSchema.$defs.lcovLineRanges } } }],
39
+ schema: { ...reportSchema.$defs.lcovLines,
40
+ $defs: { lcovLineRanges: reportSchema.$defs.lcovLineRanges, lcovHitRuns: reportSchema.$defs.lcovHitRuns } } }],
39
41
  operations: [{
40
42
  id: 'import',
41
43
  role: 'inspection',
@@ -50,7 +52,7 @@ export const coverage = {
50
52
  execute: inputs => inputs.read('coverage', coverageResult).lineCoverage,
51
53
  }],
52
54
  };
53
- export function mapCoverage({ text, root, sourceLines, excludes }) {
55
+ export function mapCoverage({ text, root, sourceLines, codeLines, excludes }) {
54
56
  const files = new Map();
55
57
  // Branches by line, block and branch. Functions by name and its occurrence in the record: tools such as c8 repeat a
56
58
  // name for different functions and list FNDA in FN order. Several reports merge by any hit.
@@ -81,6 +83,8 @@ export function mapCoverage({ text, root, sourceLines, excludes }) {
81
83
  continue;
82
84
  if (!sourceLines.has(active))
83
85
  gaps.set(active, 'LCOV source is outside the inspected file set');
86
+ else if (codeLines && !codeLines.has(active))
87
+ gaps.set(active, 'LCOV source has no classified code lines');
84
88
  else if (!files.has(active))
85
89
  files.set(active, new Map());
86
90
  }
@@ -98,8 +102,8 @@ export function mapCoverage({ text, root, sourceLines, excludes }) {
98
102
  const limit = sourceLines.get(active);
99
103
  if (limit !== undefined && number > limit)
100
104
  gaps.set(active, 'LCOV refers to a line outside the inspected source');
101
- else if (file)
102
- file.set(number, (file.get(number) ?? false) || hits > 0);
105
+ else if (file && (!codeLines || codeLines.get(active)?.has(number)))
106
+ addHits(file, number, hits, index + 1);
103
107
  }
104
108
  else if (line.startsWith('BRDA:') || line.startsWith('FN:') || line.startsWith('FNDA:')) {
105
109
  if (active === undefined)
@@ -110,10 +114,10 @@ export function mapCoverage({ text, root, sourceLines, excludes }) {
110
114
  const declaration = /^FN:[1-9]\d*(?:,[1-9]\d*)?,(.+)$/.exec(line);
111
115
  const call = /^FNDA:(\d+),(.+)$/.exec(line);
112
116
  if (branch) {
113
- const taken = branch[4] !== '-' && Number(branch[4]) > 0;
117
+ const taken = branch[4] === '-' ? 0 : Number(branch[4]);
114
118
  const key = `${branch[1]},${branch[2]},${branch[3]}`;
115
119
  const file = branches.get(active) ?? new Map();
116
- file.set(key, (file.get(key) ?? false) || taken);
120
+ addHits(file, key, taken, index + 1);
117
121
  branches.set(active, file);
118
122
  }
119
123
  else if (declaration) {
@@ -142,6 +146,12 @@ export function mapCoverage({ text, root, sourceLines, excludes }) {
142
146
  const functions = new Map([...declared].map(([path, names]) => [path, new Map([...names].map(name => [name, called.get(path)?.get(name) ?? false]))]));
143
147
  return { files, branches, functions, gaps };
144
148
  }
149
+ function addHits(hits, key, count, record) {
150
+ const total = (hits.get(key) ?? 0) + count;
151
+ if (!Number.isSafeInteger(total))
152
+ throw new Error(`LCOV hit count exceeds the supported integer range at line ${record}`);
153
+ hits.set(key, total);
154
+ }
145
155
  function importCoverage(input) {
146
156
  const { files, branches, functions, gaps } = mapCoverage(input);
147
157
  const rows = [];
@@ -154,11 +164,14 @@ function importCoverage(input) {
154
164
  const hitLines = [], missedLines = [];
155
165
  for (const [line, hit] of hits)
156
166
  (hit ? hitLines : missedLines).push(line);
157
- const partial = mergedRanges(partiallyCoveredLines(branches.get(path), input.sourceLines.get(path)));
158
- if (hits.size || partial.length)
159
- lineCoverage.push([path, {
160
- covered: mergedRanges(hitLines), uncovered: mergedRanges(missedLines), ...(partial.length ? { partial } : {}),
161
- }]);
167
+ const partialHits = partiallyCoveredLines(branches.get(path), input.sourceLines.get(path), input.codeLines?.get(path));
168
+ const partial = mergedRanges([...partialHits.keys()]);
169
+ if (hits.size || partial.length) {
170
+ // A BRDA-only partial line has at least as many hits as its most frequently taken branch.
171
+ const measured = new Map([...partialHits, ...hits]);
172
+ lineCoverage.push([path, { covered: mergedRanges(hitLines), uncovered: mergedRanges(missedLines),
173
+ ...(partial.length ? { partial } : {}), hits: mergedHits(measured) }]);
174
+ }
162
175
  const covered = hitLines.length;
163
176
  totalCovered += covered;
164
177
  totalLines += hits.size;
@@ -176,14 +189,19 @@ function importCoverage(input) {
176
189
  return { measurements: [...rows, ...counts, ...extra], lineCoverage: Object.fromEntries(lineCoverage),
177
190
  gaps: [...gaps].sort(([left], [right]) => comparePaths(left, right)).map(([path, reason]) => ({ path, reason })) };
178
191
  }
179
- function partiallyCoveredLines(branches, lastLine) {
180
- const taken = new Set(), missed = new Set();
181
- for (const [branch, hit] of branches ?? []) {
192
+ function partiallyCoveredLines(branches, lastLine, codeLines) {
193
+ const taken = new Map();
194
+ const missed = new Set();
195
+ for (const [branch, count] of branches ?? []) {
182
196
  const line = Number(branch.slice(0, branch.indexOf(',')));
183
- if (line <= lastLine)
184
- (hit ? taken : missed).add(line);
197
+ if (line > lastLine || (codeLines && !codeLines.has(line)))
198
+ continue;
199
+ if (count === 0)
200
+ missed.add(line);
201
+ else
202
+ taken.set(line, Math.max(taken.get(line) ?? 0, count));
185
203
  }
186
- return [...taken].filter(line => missed.has(line));
204
+ return new Map([...taken].filter(([line]) => missed.has(line)));
187
205
  }
188
206
  function mergedRanges(lines) {
189
207
  const ranges = [];
@@ -196,6 +214,17 @@ function mergedRanges(lines) {
196
214
  }
197
215
  return ranges;
198
216
  }
217
+ function mergedHits(hits) {
218
+ const runs = [];
219
+ for (const [line, count] of [...hits].sort(([left], [right]) => left - right)) {
220
+ const previous = runs.at(-1);
221
+ if (previous && previous[1] + 1 === line && previous[2] === count)
222
+ previous[1] = line;
223
+ else
224
+ runs.push([line, line, count]);
225
+ }
226
+ return runs;
227
+ }
199
228
  /** Per-file and repository ratios with their counts, for a kind the LCOV reports; nothing when it reports none. */
200
229
  function hitRows(files, gaps, ratioId, coveredId, totalId) {
201
230
  const rows = [];
@@ -0,0 +1,4 @@
1
+ import type { Plugin } from '../../core/engine/plugin.js';
2
+ export declare const jsSecurity: Plugin;
3
+ /** Splits camelCase, PascalCase and snake_case names into words and looks for secret-bearing ones. */
4
+ export declare function isSecretName(name: string): boolean;