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.
- package/package.json +1 -1
- package/target/main/resources/report/json/report-v2.schema.json +12 -1
- package/target/main/resources/viewer/files.css +6 -2
- package/target/main/ts/api/git/git.d.ts +1 -1
- package/target/main/ts/api/git/git.js +21 -5
- package/target/main/ts/api/git/inspection-source.d.ts +1 -1
- package/target/main/ts/api/git/inspection-source.js +10 -4
- package/target/main/ts/api/git/local.d.ts +1 -1
- package/target/main/ts/api/git/local.js +29 -10
- package/target/main/ts/api/worker/worker-protocol.js +34 -9
- package/target/main/ts/compose/analyze-snapshot.d.ts +1 -0
- package/target/main/ts/compose/analyze-snapshot.js +50 -17
- package/target/main/ts/compose/inspect.js +2 -2
- package/target/main/ts/compose/plugin-list.js +2 -1
- package/target/main/ts/compose/security-plugins.d.ts +1 -0
- package/target/main/ts/compose/security-plugins.js +7 -0
- package/target/main/ts/core/engine/manifest.js +3 -2
- package/target/main/ts/core/engine/plugin.d.ts +2 -0
- package/target/main/ts/core/model/line-coverage.d.ts +2 -0
- package/target/main/ts/core/model/line-coverage.js +19 -1
- package/target/main/ts/core/model/source.d.ts +3 -0
- package/target/main/ts/core/model/source.js +4 -1
- package/target/main/ts/core/usecases/inspect.js +2 -2
- package/target/main/ts/plugins/coverage/coverage.d.ts +4 -3
- package/target/main/ts/plugins/coverage/coverage.js +46 -17
- package/target/main/ts/plugins/language-js/js-security.d.ts +4 -0
- package/target/main/ts/plugins/language-js/js-security.js +508 -0
- package/target/main/ts/plugins/security/actions-security.d.ts +2 -0
- package/target/main/ts/plugins/security/actions-security.js +581 -0
- package/target/main/ts/plugins/security/dockerfile.d.ts +2 -0
- package/target/main/ts/plugins/security/dockerfile.js +488 -0
- package/target/main/ts/plugins/security/npm-supply-chain.d.ts +4 -0
- package/target/main/ts/plugins/security/npm-supply-chain.js +368 -0
- package/target/main/ts/plugins/security/secrets.d.ts +12 -0
- package/target/main/ts/plugins/security/secrets.js +254 -0
- package/target/main/ts/report/html/app/viewer-files.d.ts +7 -0
- package/target/main/ts/report/html/app/viewer-files.js +17 -3
- package/target/main/ts/report/html/app/viewer-filter.js +8 -0
- package/target/main/ts/report/html/app/viewer-source.d.ts +6 -0
- package/target/main/ts/report/html/app/viewer-source.js +34 -7
- package/target/main/ts/report/html/site/badges.d.ts +1 -4
- package/target/main/ts/report/html/site/badges.js +28 -19
- package/target/main/ts/report/json/projections/commit-summary.js +2 -11
- package/target/main/ts/report/json/projections/inspection-context.js +6 -12
- package/target/main/ts/report/json/projections/security-summary.d.ts +11 -0
- package/target/main/ts/report/json/projections/security-summary.js +22 -0
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import { pluginApi } from '../../core/engine/plugin.js';
|
|
2
|
+
import { findingResult } from '../../core/model/finding.js';
|
|
3
|
+
import { sourceFile } from '../../core/model/snapshot.js';
|
|
4
|
+
const tool = 'actions-security';
|
|
5
|
+
const ruleCatalog = {
|
|
6
|
+
'unpinned-action': { level: 'medium',
|
|
7
|
+
description: 'A third-party action or reusable workflow is referenced by a tag or branch instead of a full commit SHA.' },
|
|
8
|
+
'unpinned-first-party-action': { level: 'low',
|
|
9
|
+
description: 'An action owned by actions or github is referenced by a tag or branch instead of a full commit SHA.' },
|
|
10
|
+
'script-injection': { level: 'high',
|
|
11
|
+
description: 'A run script expands attacker-controlled context, such as an issue title or a branch name, directly into the shell.' },
|
|
12
|
+
'pull-request-target-checkout': { level: 'high',
|
|
13
|
+
description: 'A pull_request_target or workflow_run workflow checks out untrusted pull request code in a privileged job.' },
|
|
14
|
+
'excessive-permissions': { level: 'medium',
|
|
15
|
+
description: 'The workflow or one of its jobs grants the GITHUB_TOKEN write-all permissions.' },
|
|
16
|
+
'missing-permissions': { level: 'low',
|
|
17
|
+
description: 'Neither the workflow nor any of its jobs declares permissions, so the GITHUB_TOKEN gets the repository default.' },
|
|
18
|
+
'persisted-credentials': { level: 'low',
|
|
19
|
+
description: 'An actions/checkout step leaves the GITHUB_TOKEN in the git config because persist-credentials is not false.' },
|
|
20
|
+
'secrets-inherit': { level: 'low',
|
|
21
|
+
description: 'A reusable workflow call passes every repository secret with secrets: inherit.' },
|
|
22
|
+
};
|
|
23
|
+
export const actionsSecurity = {
|
|
24
|
+
id: tool, version: '1.0.0', api: pluginApi,
|
|
25
|
+
kinds: ['security'],
|
|
26
|
+
claims: { globs: ['.github/workflows/*.yml', '.github/workflows/*.yaml', 'action.yml', 'action.yaml'] },
|
|
27
|
+
produces: [{ contract: findingResult.id, persistence: 'report' }],
|
|
28
|
+
rules: Object.entries(ruleCatalog).map(([id, { description }]) => ({ id, category: 'infrastructure', description })),
|
|
29
|
+
operations: [{
|
|
30
|
+
id: 'scan', role: 'inspection', inputs: { source: sourceFile }, output: findingResult,
|
|
31
|
+
execute(inputs) {
|
|
32
|
+
const { path, bytes } = inputs.read('source', sourceFile);
|
|
33
|
+
return { measurements: [], findings: audit(path, new TextDecoder().decode(bytes)) };
|
|
34
|
+
},
|
|
35
|
+
}],
|
|
36
|
+
};
|
|
37
|
+
function audit(path, text) {
|
|
38
|
+
const root = new YamlReader(text).read();
|
|
39
|
+
const findings = new Map();
|
|
40
|
+
const report = (rule, line, message) => {
|
|
41
|
+
// Aliased nodes are visited once per alias; report each location once.
|
|
42
|
+
const key = `${rule}\0${line}\0${message}`;
|
|
43
|
+
if (!findings.has(key)) {
|
|
44
|
+
findings.set(key, { tool, rule, category: 'infrastructure', level: ruleCatalog[rule].level, message, path, line });
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const jobs = entryOf(root, 'jobs');
|
|
48
|
+
if (jobs)
|
|
49
|
+
auditWorkflow(root, jobs, report);
|
|
50
|
+
else
|
|
51
|
+
for (const step of itemsOf(valueOf(valueOf(root, 'runs'), 'steps')))
|
|
52
|
+
auditStep(step, undefined, report);
|
|
53
|
+
return [...findings.values()].sort((left, right) => left.line - right.line || (left.rule < right.rule ? -1 : left.rule > right.rule ? 1 : 0));
|
|
54
|
+
}
|
|
55
|
+
function auditWorkflow(root, jobs, report) {
|
|
56
|
+
const events = triggers(valueOf(root, 'on'));
|
|
57
|
+
const privileged = ['pull_request_target', 'workflow_run'].find(event => events.has(event));
|
|
58
|
+
const top = entryOf(root, 'permissions');
|
|
59
|
+
let declared = top !== undefined;
|
|
60
|
+
if (top)
|
|
61
|
+
auditPermissions(top, 'The workflow', report);
|
|
62
|
+
for (const job of jobs.value.kind === 'map' ? jobs.value.entries : []) {
|
|
63
|
+
const permissions = entryOf(job.value, 'permissions');
|
|
64
|
+
if (permissions) {
|
|
65
|
+
declared = true;
|
|
66
|
+
auditPermissions(permissions, `Job ${job.key}`, report);
|
|
67
|
+
}
|
|
68
|
+
const call = entryOf(job.value, 'uses');
|
|
69
|
+
if (call) {
|
|
70
|
+
auditPinning(call, 'Reusable workflow', report);
|
|
71
|
+
const secrets = entryOf(job.value, 'secrets');
|
|
72
|
+
if (secrets && textOf(secrets.value)?.trim() === 'inherit') {
|
|
73
|
+
report('secrets-inherit', secrets.line, `Job ${job.key} passes every repository secret to the called workflow with secrets: inherit instead of only the secrets it needs.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const step of itemsOf(valueOf(job.value, 'steps')))
|
|
77
|
+
auditStep(step, privileged, report);
|
|
78
|
+
}
|
|
79
|
+
if (!declared) {
|
|
80
|
+
report('missing-permissions', jobs.line, 'No permissions are set for the workflow or any of its jobs, so the GITHUB_TOKEN gets the repository default, which may allow writes.');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function triggers(node) {
|
|
84
|
+
if (node === undefined)
|
|
85
|
+
return new Set();
|
|
86
|
+
if (node.kind === 'scalar')
|
|
87
|
+
return new Set([node.value.trim()]);
|
|
88
|
+
if (node.kind === 'list')
|
|
89
|
+
return new Set(node.items.flatMap(item => item.kind === 'scalar' ? [item.value.trim()] : []));
|
|
90
|
+
return new Set(node.entries.map(entry => entry.key));
|
|
91
|
+
}
|
|
92
|
+
function auditPermissions(permissions, subject, report) {
|
|
93
|
+
if (textOf(permissions.value)?.trim().toLowerCase() !== 'write-all')
|
|
94
|
+
return;
|
|
95
|
+
report('excessive-permissions', permissions.line, `${subject} sets permissions: write-all, which gives the GITHUB_TOKEN write access to every scope.`);
|
|
96
|
+
}
|
|
97
|
+
function auditPinning(uses, subject, report) {
|
|
98
|
+
const reference = textOf(uses.value)?.trim();
|
|
99
|
+
if (!reference || reference.startsWith('./') || /^docker:\/\//i.test(reference) || reference.includes('${{'))
|
|
100
|
+
return;
|
|
101
|
+
const at = reference.lastIndexOf('@');
|
|
102
|
+
if (at <= 0)
|
|
103
|
+
return;
|
|
104
|
+
const name = reference.slice(0, at);
|
|
105
|
+
const ref = reference.slice(at + 1);
|
|
106
|
+
if (/^[0-9a-f]{40}$/i.test(ref))
|
|
107
|
+
return;
|
|
108
|
+
const owner = name.split('/')[0].toLowerCase();
|
|
109
|
+
if (owner === 'actions' || owner === 'github') {
|
|
110
|
+
report('unpinned-first-party-action', uses.line, `${subject} ${name}@${ref} is not pinned to a full commit SHA, so the code behind that ref can still change.`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
report('unpinned-action', uses.line, `${subject} ${name}@${ref} is not pinned to a full commit SHA, so whoever controls that ref can change the code this workflow runs.`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// PR head refs, PR merge refs and the workflow_run head all point at code the pull request author controls.
|
|
117
|
+
const untrustedCheckout = /github\.event\.pull_request\.head\.|github\.event\.pull_request\.merge_commit_sha|github\.head_ref|github\.event\.workflow_run\.head_|refs\/pull\//;
|
|
118
|
+
function auditStep(step, privileged, report) {
|
|
119
|
+
const uses = entryOf(step, 'uses');
|
|
120
|
+
const action = textOf(uses?.value)?.trim();
|
|
121
|
+
if (uses && action !== undefined) {
|
|
122
|
+
auditPinning(uses, 'Action', report);
|
|
123
|
+
if (/^actions\/checkout(?:@|$)/i.test(action)) {
|
|
124
|
+
const inputs = valueOf(step, 'with');
|
|
125
|
+
const persist = textOf(valueOf(inputs, 'persist-credentials'))?.trim();
|
|
126
|
+
if (persist === undefined || (persist.toLowerCase() !== 'false' && !persist.includes('${{'))) {
|
|
127
|
+
report('persisted-credentials', uses.line, 'This actions/checkout step keeps the GITHUB_TOKEN in .git/config for every later step because persist-credentials is not set to false.');
|
|
128
|
+
}
|
|
129
|
+
const source = privileged === undefined ? undefined : ['ref', 'repository'].map(key => entryOf(inputs, key))
|
|
130
|
+
.find(entry => entry !== undefined && untrustedCheckout.test(normalize(textOf(entry.value) ?? '')));
|
|
131
|
+
if (source) {
|
|
132
|
+
report('pull-request-target-checkout', source.line, `This ${privileged} workflow checks out untrusted pull request code from ${textOf(source.value).trim()} in a job that can read secrets and write to the repository.`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const run = valueOf(step, 'run');
|
|
137
|
+
if (run?.kind === 'scalar')
|
|
138
|
+
auditScript(run, report);
|
|
139
|
+
}
|
|
140
|
+
function auditScript(script, report) {
|
|
141
|
+
const { fragments } = script;
|
|
142
|
+
const starts = [];
|
|
143
|
+
let offset = 0;
|
|
144
|
+
for (const fragment of fragments) {
|
|
145
|
+
starts.push(offset);
|
|
146
|
+
offset += fragment.text.length + 1;
|
|
147
|
+
}
|
|
148
|
+
const source = fragments.map(fragment => fragment.text).join('\n');
|
|
149
|
+
for (const match of source.matchAll(/\$\{\{([\s\S]*?)\}\}/g)) {
|
|
150
|
+
const contexts = untrustedContexts(match[1]);
|
|
151
|
+
if (contexts.length === 0)
|
|
152
|
+
continue;
|
|
153
|
+
let index = 0;
|
|
154
|
+
while (index + 1 < starts.length && starts[index + 1] <= match.index)
|
|
155
|
+
index++;
|
|
156
|
+
report('script-injection', fragments[index]?.line ?? script.line, `The run script expands ${contexts.join(', ')} directly into the shell, so a crafted value can run arbitrary commands; pass it through an env variable instead.`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Context an outside contributor can set through an issue, a comment, a pull request, a branch name or a commit.
|
|
160
|
+
const untrustedContext = new RegExp(String.raw `(?<![\w.-])(?:github\.head_ref|github\.event\.(?:`
|
|
161
|
+
+ String.raw `issue\.(?:title|body)|pull_request\.(?:title|body|head\.(?:ref|label|repo\.default_branch))|comment\.body|review\.body`
|
|
162
|
+
+ String.raw `|discussion\.(?:title|body)|pages\.\*\.page_name|commits\.\*\.(?:message|author\.(?:name|email)|committer\.(?:name|email))`
|
|
163
|
+
+ String.raw `|head_commit\.(?:message|author\.(?:name|email)|committer\.(?:name|email))`
|
|
164
|
+
+ String.raw `|workflow_run\.(?:head_branch|head_commit\.(?:message|author\.(?:name|email)))))(?![\w-])`, 'g');
|
|
165
|
+
function untrustedContexts(expression) {
|
|
166
|
+
const source = normalize(expression).replace(/'(?:[^']|'')*'/g, literal => `'${'_'.repeat(literal.length - 2)}'`);
|
|
167
|
+
const found = new Set();
|
|
168
|
+
for (const match of source.matchAll(untrustedContext)) {
|
|
169
|
+
if (!isBooleanOperand(source, match.index, match.index + match[0].length))
|
|
170
|
+
found.add(match[0]);
|
|
171
|
+
}
|
|
172
|
+
return [...found];
|
|
173
|
+
}
|
|
174
|
+
/** Expression property access is case-insensitive and may use index syntax: github.event['issue'].title, commits[0]. */
|
|
175
|
+
function normalize(expression) {
|
|
176
|
+
return expression.toLowerCase()
|
|
177
|
+
.replace(/\[\s*(['"])([^'"]*)\1\s*\]/g, '.$2')
|
|
178
|
+
.replace(/\[\s*(?:\*|\d+)\s*\]/g, '.*');
|
|
179
|
+
}
|
|
180
|
+
/** A comparison or contains/startsWith/endsWith call turns the untrusted value into a boolean, which is safe to expand. */
|
|
181
|
+
function isBooleanOperand(source, start, end) {
|
|
182
|
+
if (/(?:[=!<>]=|[<>!])$/.test(source.slice(0, start).trimEnd()) || /^(?:[=!<>]=|[<>])/.test(source.slice(end).trimStart()))
|
|
183
|
+
return true;
|
|
184
|
+
let depth = 0;
|
|
185
|
+
for (let index = start - 1; index >= 0; index--) {
|
|
186
|
+
if (source[index] === ')')
|
|
187
|
+
depth++;
|
|
188
|
+
else if (source[index] === '(') {
|
|
189
|
+
if (depth > 0)
|
|
190
|
+
depth--;
|
|
191
|
+
else if (/(?:^|[^\w.])(?:contains|startswith|endswith)\s*$/.test(source.slice(0, index)))
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
function scalar(value, line, fragments = []) {
|
|
198
|
+
return { kind: 'scalar', value, line, fragments };
|
|
199
|
+
}
|
|
200
|
+
function entryOf(node, key) {
|
|
201
|
+
return node?.kind === 'map' ? node.entries.find(entry => entry.key === key) : undefined;
|
|
202
|
+
}
|
|
203
|
+
function valueOf(node, key) {
|
|
204
|
+
return entryOf(node, key)?.value;
|
|
205
|
+
}
|
|
206
|
+
function textOf(node) {
|
|
207
|
+
return node?.kind === 'scalar' ? node.value : undefined;
|
|
208
|
+
}
|
|
209
|
+
function itemsOf(node) {
|
|
210
|
+
return node?.kind === 'list' ? node.items : [];
|
|
211
|
+
}
|
|
212
|
+
const keyPattern = /^(?:"((?:[^"\\]|\\.)*)"|'((?:[^']|'')*)'|([^\s#"'&*!|>%@`,[\]{}?:-][^#]*?|[?:-][^\s#][^#]*?))[ \t]*:(?=\s|$)/;
|
|
213
|
+
const propertyPattern = /^([&!])(\S*)(?:\s+|$)/;
|
|
214
|
+
function isItem(text) {
|
|
215
|
+
return /^-(?:\s|$)/.test(text);
|
|
216
|
+
}
|
|
217
|
+
function indentOf(raw) {
|
|
218
|
+
return raw.length - raw.trimStart().length;
|
|
219
|
+
}
|
|
220
|
+
class YamlReader {
|
|
221
|
+
position = 0;
|
|
222
|
+
anchors = new Map();
|
|
223
|
+
lines;
|
|
224
|
+
constructor(text) {
|
|
225
|
+
this.lines = text.split(/\r\n|\r|\n/);
|
|
226
|
+
}
|
|
227
|
+
read() {
|
|
228
|
+
return this.node(-1) ?? scalar('', 1);
|
|
229
|
+
}
|
|
230
|
+
/** The next line with content; blank lines, comment lines and document markers are skipped. */
|
|
231
|
+
peek() {
|
|
232
|
+
for (; this.position < this.lines.length; this.position++) {
|
|
233
|
+
const raw = this.lines[this.position];
|
|
234
|
+
const text = raw.trim();
|
|
235
|
+
if (text === '' || text.startsWith('#') || /^(?:(?:---|\.\.\.)(?:\s|$)|%)/.test(raw))
|
|
236
|
+
continue;
|
|
237
|
+
return { indent: indentOf(raw), text, line: this.position + 1 };
|
|
238
|
+
}
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
node(parent) {
|
|
242
|
+
const next = this.peek();
|
|
243
|
+
if (next === undefined || next.indent <= parent)
|
|
244
|
+
return undefined;
|
|
245
|
+
if (isItem(next.text))
|
|
246
|
+
return this.sequence(next.indent, next.line);
|
|
247
|
+
if (keyPattern.test(next.text))
|
|
248
|
+
return this.mapping(next.indent, next.line);
|
|
249
|
+
this.position++;
|
|
250
|
+
return this.inline(next.text, parent, next.line, false);
|
|
251
|
+
}
|
|
252
|
+
sequence(indent, line) {
|
|
253
|
+
const items = [];
|
|
254
|
+
for (let next = this.peek(); next !== undefined && next.indent >= indent; next = this.peek()) {
|
|
255
|
+
if (next.indent > indent) {
|
|
256
|
+
this.position++; // A line no node owns: skip it rather than end the document.
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (!isItem(next.text))
|
|
260
|
+
break;
|
|
261
|
+
// Blank out the dash so the item content reads as a node indented past the sequence.
|
|
262
|
+
const raw = this.lines[this.position];
|
|
263
|
+
this.lines[this.position] = `${raw.slice(0, indent)} ${raw.slice(indent + 1)}`;
|
|
264
|
+
items.push(this.node(indent) ?? scalar('', next.line));
|
|
265
|
+
}
|
|
266
|
+
return { kind: 'list', line, items };
|
|
267
|
+
}
|
|
268
|
+
mapping(indent, line) {
|
|
269
|
+
const entries = [];
|
|
270
|
+
for (let next = this.peek(); next !== undefined && next.indent >= indent; next = this.peek()) {
|
|
271
|
+
if (next.indent > indent) {
|
|
272
|
+
this.position++;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
const key = keyPattern.exec(next.text);
|
|
276
|
+
if (key === null)
|
|
277
|
+
break;
|
|
278
|
+
this.position++;
|
|
279
|
+
const name = key[1] !== undefined ? unescape(key[1]) : key[2] !== undefined ? key[2].replaceAll("''", "'") : key[3].trimEnd();
|
|
280
|
+
entries.push({ key: name, line: next.line, value: this.inline(next.text.slice(key[0].length), indent, next.line, true) });
|
|
281
|
+
}
|
|
282
|
+
return { kind: 'map', line, entries };
|
|
283
|
+
}
|
|
284
|
+
/** The value written after `key:` or `- `; `owner` is the indent its continuation lines must exceed. */
|
|
285
|
+
inline(text, owner, line, mapValue) {
|
|
286
|
+
let rest = text.trim();
|
|
287
|
+
let anchor;
|
|
288
|
+
for (let property = propertyPattern.exec(rest); property !== null && rest !== ''; property = propertyPattern.exec(rest)) {
|
|
289
|
+
if (property[1] === '&')
|
|
290
|
+
anchor = property[2];
|
|
291
|
+
rest = rest.slice(property[0].length);
|
|
292
|
+
}
|
|
293
|
+
const node = this.value(rest, owner, line, mapValue);
|
|
294
|
+
if (anchor)
|
|
295
|
+
this.anchors.set(anchor, node);
|
|
296
|
+
return node;
|
|
297
|
+
}
|
|
298
|
+
value(text, owner, line, mapValue) {
|
|
299
|
+
if (text === '' || text.startsWith('#'))
|
|
300
|
+
return this.nested(owner, line, mapValue);
|
|
301
|
+
const alias = /^\*(\S+)(?:\s+#.*)?$/.exec(text);
|
|
302
|
+
if (alias)
|
|
303
|
+
return this.anchors.get(alias[1]) ?? scalar(text, line, [{ text, line }]);
|
|
304
|
+
if (/^[|>](?:[1-9][+-]?|[+-][1-9]?)?(?:\s+#.*)?$/.test(text))
|
|
305
|
+
return this.block(text, owner, line);
|
|
306
|
+
if (text.startsWith('[') || text.startsWith('{'))
|
|
307
|
+
return this.flow(text, owner, line);
|
|
308
|
+
if (text.startsWith('"') || text.startsWith("'"))
|
|
309
|
+
return this.quoted(text, owner, line);
|
|
310
|
+
return this.plain(text, owner, line);
|
|
311
|
+
}
|
|
312
|
+
nested(owner, line, mapValue) {
|
|
313
|
+
const next = this.peek();
|
|
314
|
+
if (next !== undefined && next.indent > owner)
|
|
315
|
+
return this.node(owner) ?? scalar('', line);
|
|
316
|
+
// A sequence may sit at the same indent as the key that owns it.
|
|
317
|
+
if (mapValue && next !== undefined && next.indent === owner && isItem(next.text))
|
|
318
|
+
return this.sequence(owner, next.line);
|
|
319
|
+
return scalar('', line);
|
|
320
|
+
}
|
|
321
|
+
block(header, owner, line) {
|
|
322
|
+
const explicit = Number(/[1-9]/.exec(header)?.[0] ?? 0);
|
|
323
|
+
let indent = explicit > 0 ? Math.max(owner, 0) + explicit : undefined;
|
|
324
|
+
const fragments = [];
|
|
325
|
+
let last = -1;
|
|
326
|
+
for (let index = this.position; index < this.lines.length; index++) {
|
|
327
|
+
const raw = this.lines[index];
|
|
328
|
+
if (raw.trim() === '')
|
|
329
|
+
continue;
|
|
330
|
+
const depth = indentOf(raw);
|
|
331
|
+
if (indent === undefined && depth > owner)
|
|
332
|
+
indent = depth;
|
|
333
|
+
if (indent === undefined || depth < indent)
|
|
334
|
+
break;
|
|
335
|
+
if (last >= 0)
|
|
336
|
+
for (let blank = last + 1; blank < index; blank++)
|
|
337
|
+
fragments.push({ text: '', line: blank + 1 });
|
|
338
|
+
fragments.push({ text: raw.slice(indent), line: index + 1 });
|
|
339
|
+
last = index;
|
|
340
|
+
}
|
|
341
|
+
if (last >= 0)
|
|
342
|
+
this.position = last + 1;
|
|
343
|
+
const joined = fragments.map(fragment => fragment.text).join('\n');
|
|
344
|
+
return scalar(header.startsWith('|') ? joined : joined.replace(/([^\n])\n(?=\S)/g, '$1 '), line, fragments);
|
|
345
|
+
}
|
|
346
|
+
plain(text, owner, line) {
|
|
347
|
+
const first = uncomment(text);
|
|
348
|
+
const fragments = [{ text: first.text, line }];
|
|
349
|
+
for (let ended = first.commented; !ended;) {
|
|
350
|
+
let index = this.position;
|
|
351
|
+
while (index < this.lines.length && this.lines[index].trim() === '')
|
|
352
|
+
index++;
|
|
353
|
+
const raw = this.lines[index];
|
|
354
|
+
if (raw === undefined || indentOf(raw) <= owner || raw.trim().startsWith('#'))
|
|
355
|
+
break;
|
|
356
|
+
const next = uncomment(raw.trim());
|
|
357
|
+
fragments.push({ text: next.text, line: index + 1 });
|
|
358
|
+
this.position = index + 1;
|
|
359
|
+
ended = next.commented;
|
|
360
|
+
}
|
|
361
|
+
return scalar(fragments.map(fragment => fragment.text).join(' '), line, fragments);
|
|
362
|
+
}
|
|
363
|
+
quoted(text, owner, line) {
|
|
364
|
+
const quote = text[0];
|
|
365
|
+
const fragments = [{ text, line }];
|
|
366
|
+
let source = text.slice(1);
|
|
367
|
+
let close = closing(source, quote);
|
|
368
|
+
while (close < 0 && this.position < this.lines.length) {
|
|
369
|
+
const raw = this.lines[this.position];
|
|
370
|
+
if (raw.trim() !== '' && indentOf(raw) <= owner)
|
|
371
|
+
break;
|
|
372
|
+
this.position++;
|
|
373
|
+
fragments.push({ text: raw.trim(), line: this.position });
|
|
374
|
+
source += `\n${raw.trim()}`;
|
|
375
|
+
close = closing(source, quote);
|
|
376
|
+
}
|
|
377
|
+
return scalar(cook(close < 0 ? source : source.slice(0, close), quote), line, fragments);
|
|
378
|
+
}
|
|
379
|
+
flow(text, owner, line) {
|
|
380
|
+
let source = text;
|
|
381
|
+
while (!balanced(source) && this.position < this.lines.length) {
|
|
382
|
+
const raw = this.lines[this.position];
|
|
383
|
+
if (raw.trim() !== '' && indentOf(raw) <= owner)
|
|
384
|
+
break;
|
|
385
|
+
source += `\n${raw}`;
|
|
386
|
+
this.position++;
|
|
387
|
+
}
|
|
388
|
+
return new FlowReader(source, line).read();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/** Reads one flow collection such as `[push, pull_request]` or `{ persist-credentials: false }`. */
|
|
392
|
+
class FlowReader {
|
|
393
|
+
source;
|
|
394
|
+
line;
|
|
395
|
+
index = 0;
|
|
396
|
+
constructor(source, line) {
|
|
397
|
+
this.source = source;
|
|
398
|
+
this.line = line;
|
|
399
|
+
}
|
|
400
|
+
read() {
|
|
401
|
+
return this.node();
|
|
402
|
+
}
|
|
403
|
+
lineAt(index) {
|
|
404
|
+
let line = this.line;
|
|
405
|
+
for (let newline = this.source.indexOf('\n'); newline >= 0 && newline < index; newline = this.source.indexOf('\n', newline + 1))
|
|
406
|
+
line++;
|
|
407
|
+
return line;
|
|
408
|
+
}
|
|
409
|
+
space() {
|
|
410
|
+
while (this.index < this.source.length) {
|
|
411
|
+
const char = this.source[this.index];
|
|
412
|
+
if (char === '#' && (this.index === 0 || /\s/.test(this.source[this.index - 1]))) {
|
|
413
|
+
const newline = this.source.indexOf('\n', this.index);
|
|
414
|
+
this.index = newline < 0 ? this.source.length : newline;
|
|
415
|
+
}
|
|
416
|
+
else if (/\s/.test(char))
|
|
417
|
+
this.index++;
|
|
418
|
+
else
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
node() {
|
|
423
|
+
this.space();
|
|
424
|
+
const line = this.lineAt(this.index);
|
|
425
|
+
const char = this.source[this.index];
|
|
426
|
+
if (char === '[')
|
|
427
|
+
return this.sequence(line);
|
|
428
|
+
if (char === '{')
|
|
429
|
+
return this.mapping(line);
|
|
430
|
+
if (char === '"' || char === "'")
|
|
431
|
+
return this.quoted(line);
|
|
432
|
+
return this.plain(line);
|
|
433
|
+
}
|
|
434
|
+
sequence(line) {
|
|
435
|
+
const items = [];
|
|
436
|
+
this.index++;
|
|
437
|
+
for (;;) {
|
|
438
|
+
this.space();
|
|
439
|
+
const char = this.source[this.index];
|
|
440
|
+
if (char === undefined)
|
|
441
|
+
break;
|
|
442
|
+
if (char === ']') {
|
|
443
|
+
this.index++;
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
if (char === ',') {
|
|
447
|
+
this.index++;
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const start = this.index;
|
|
451
|
+
items.push(this.node());
|
|
452
|
+
if (this.index === start)
|
|
453
|
+
this.index++;
|
|
454
|
+
}
|
|
455
|
+
return { kind: 'list', line, items };
|
|
456
|
+
}
|
|
457
|
+
mapping(line) {
|
|
458
|
+
const entries = [];
|
|
459
|
+
this.index++;
|
|
460
|
+
for (;;) {
|
|
461
|
+
this.space();
|
|
462
|
+
const char = this.source[this.index];
|
|
463
|
+
if (char === undefined)
|
|
464
|
+
break;
|
|
465
|
+
if (char === '}') {
|
|
466
|
+
this.index++;
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
if (char === ',') {
|
|
470
|
+
this.index++;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const start = this.index;
|
|
474
|
+
const key = this.node();
|
|
475
|
+
this.space();
|
|
476
|
+
let value = scalar('', key.line);
|
|
477
|
+
if (this.source[this.index] === ':') {
|
|
478
|
+
this.index++;
|
|
479
|
+
this.space();
|
|
480
|
+
const next = this.source[this.index];
|
|
481
|
+
if (next !== undefined && next !== ',' && next !== '}')
|
|
482
|
+
value = this.node();
|
|
483
|
+
}
|
|
484
|
+
entries.push({ key: textOf(key) ?? '', line: key.line, value });
|
|
485
|
+
if (this.index === start)
|
|
486
|
+
this.index++;
|
|
487
|
+
}
|
|
488
|
+
return { kind: 'map', line, entries };
|
|
489
|
+
}
|
|
490
|
+
quoted(line) {
|
|
491
|
+
const quote = this.source[this.index];
|
|
492
|
+
const close = closing(this.source.slice(this.index + 1), quote);
|
|
493
|
+
const end = close < 0 ? this.source.length : this.index + 1 + close;
|
|
494
|
+
const raw = this.source.slice(this.index, end + 1);
|
|
495
|
+
const body = this.source.slice(this.index + 1, end);
|
|
496
|
+
this.index = Math.min(end + 1, this.source.length);
|
|
497
|
+
return scalar(cook(body, quote), line, [{ text: raw, line }]);
|
|
498
|
+
}
|
|
499
|
+
plain(line) {
|
|
500
|
+
const start = this.index;
|
|
501
|
+
let end = start;
|
|
502
|
+
while (this.index < this.source.length) {
|
|
503
|
+
// Expressions are kept whole even when they are not quoted, although their braces end a plain scalar in YAML.
|
|
504
|
+
if (this.source.startsWith('${{', this.index)) {
|
|
505
|
+
const close = this.source.indexOf('}}', this.index + 3);
|
|
506
|
+
this.index = close < 0 ? this.source.length : close + 2;
|
|
507
|
+
end = this.index;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const char = this.source[this.index];
|
|
511
|
+
const next = this.source[this.index + 1];
|
|
512
|
+
if (',[]{}'.includes(char) || (char === ':' && (next === undefined || /[\s,[\]{}]/.test(next)))
|
|
513
|
+
|| (char === '#' && /\s/.test(this.source[this.index - 1] ?? ' ')))
|
|
514
|
+
break;
|
|
515
|
+
this.index++;
|
|
516
|
+
if (!/\s/.test(char))
|
|
517
|
+
end = this.index;
|
|
518
|
+
}
|
|
519
|
+
const raw = this.source.slice(start, end);
|
|
520
|
+
return scalar(raw.replace(/\s*\n\s*/g, ' '), line, [{ text: raw, line }]);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
function uncomment(text) {
|
|
524
|
+
const comment = /(?:^|[ \t])#/.exec(text);
|
|
525
|
+
return comment ? { text: text.slice(0, comment.index).trimEnd(), commented: true } : { text: text.trimEnd(), commented: false };
|
|
526
|
+
}
|
|
527
|
+
/** The index of the quote that closes a scalar whose opening quote precedes `source`, or -1. */
|
|
528
|
+
function closing(source, quote) {
|
|
529
|
+
for (let index = 0; index < source.length; index++) {
|
|
530
|
+
const char = source[index];
|
|
531
|
+
if (quote === '"' && char === '\\')
|
|
532
|
+
index++;
|
|
533
|
+
else if (char === quote) {
|
|
534
|
+
if (quote === "'" && source[index + 1] === "'")
|
|
535
|
+
index++;
|
|
536
|
+
else
|
|
537
|
+
return index;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return -1;
|
|
541
|
+
}
|
|
542
|
+
function balanced(source) {
|
|
543
|
+
let depth = 0;
|
|
544
|
+
for (let index = 0; index < source.length; index++) {
|
|
545
|
+
const char = source[index];
|
|
546
|
+
if ((char === '"' || char === "'") && /(?:^|[[{,:])$/.test(source.slice(0, index).trimEnd())) {
|
|
547
|
+
const close = closing(source.slice(index + 1), char);
|
|
548
|
+
if (close < 0)
|
|
549
|
+
return false;
|
|
550
|
+
index += close + 1;
|
|
551
|
+
}
|
|
552
|
+
else if (char === '#' && (index === 0 || /\s/.test(source[index - 1]))) {
|
|
553
|
+
const newline = source.indexOf('\n', index);
|
|
554
|
+
if (newline < 0)
|
|
555
|
+
break;
|
|
556
|
+
index = newline;
|
|
557
|
+
}
|
|
558
|
+
else if (char === '[' || char === '{')
|
|
559
|
+
depth++;
|
|
560
|
+
else if (char === ']' || char === '}')
|
|
561
|
+
depth--;
|
|
562
|
+
}
|
|
563
|
+
return depth <= 0;
|
|
564
|
+
}
|
|
565
|
+
const escapes = {
|
|
566
|
+
'0': '\0', a: '\x07', b: '\b', t: '\t', n: '\n', v: '\v', f: '\f', r: '\r', e: '\x1b', N: '\x85', _: '\xa0', L: '
', P: '
',
|
|
567
|
+
};
|
|
568
|
+
/** The value of a quoted scalar body: line breaks fold to spaces, double-quoted escapes are decoded. */
|
|
569
|
+
function cook(body, quote) {
|
|
570
|
+
const folded = body.replace(/[ \t]*\n[ \t]*/g, ' ');
|
|
571
|
+
return quote === '"' ? unescape(folded) : folded.replaceAll("''", "'");
|
|
572
|
+
}
|
|
573
|
+
function unescape(text) {
|
|
574
|
+
return text.replace(/\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|[\s\S])/g, (_, code) => {
|
|
575
|
+
if (code.length === 1)
|
|
576
|
+
return escapes[code] ?? code;
|
|
577
|
+
const point = parseInt(code.slice(1), 16);
|
|
578
|
+
return point <= 0x10ffff ? String.fromCodePoint(point) : code;
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
//# sourceMappingURL=actions-security.js.map
|