@ship.zone/runner 0.1.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/.smartconfig.json +48 -0
- package/changelog.md +12 -0
- package/deno.json +41 -0
- package/deno.lock +1843 -0
- package/dist_npm/mod.d.ts +689 -0
- package/dist_npm/mod.js +8980 -0
- package/dist_rust/runnerworkspace_linux_amd64_musl +0 -0
- package/dist_rust/runnerworkspace_linux_arm64_musl +0 -0
- package/license.md +18 -0
- package/mod.ts +8 -0
- package/musl-copyright.md +81 -0
- package/package.json +58 -0
- package/readme.md +274 -0
- package/rust/.cargo/config.toml +5 -0
- package/rust/Cargo.lock +144 -0
- package/rust/Cargo.toml +17 -0
- package/rust/src/lib.rs +1784 -0
- package/rust/src/main.rs +6 -0
- package/third-party-licenses.md +95 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/api-client.ts +1059 -0
- package/ts/artifact-manager.ts +218 -0
- package/ts/attempt-state.ts +190 -0
- package/ts/cache-manager.ts +431 -0
- package/ts/cli.ts +239 -0
- package/ts/config.ts +724 -0
- package/ts/errors.ts +68 -0
- package/ts/index.ts +13 -0
- package/ts/interfaces.ts +547 -0
- package/ts/log-sender.ts +134 -0
- package/ts/oci-executor.ts +2472 -0
- package/ts/plugins.ts +11 -0
- package/ts/protocol-validator.ts +535 -0
- package/ts/redactor.ts +41 -0
- package/ts/registration-state.ts +253 -0
- package/ts/runner.ts +1036 -0
- package/ts/source-workspace.ts +161 -0
- package/ts/staging-scavenger.ts +62 -0
- package/ts/transfer-archive.ts +1605 -0
- package/ts/validation.ts +77 -0
package/ts/plugins.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export {
|
|
2
|
+
ciSpecStatus,
|
|
3
|
+
runnerProtocolBasePath,
|
|
4
|
+
runnerProtocolIdentifier,
|
|
5
|
+
} from '@foss.global/ci-spec';
|
|
6
|
+
|
|
7
|
+
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
8
|
+
export { Ajv2020 };
|
|
9
|
+
|
|
10
|
+
import runnerJobSchema from '@foss.global/ci-spec/runner-job.schema.json' with { type: 'json' };
|
|
11
|
+
export { runnerJobSchema };
|
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import { ProtocolError } from './errors.ts';
|
|
2
|
+
import type {
|
|
3
|
+
ICompiledRunnerJob,
|
|
4
|
+
IJobLease,
|
|
5
|
+
IRunnerCapabilities,
|
|
6
|
+
ITransferLimits,
|
|
7
|
+
TExecutionProfile,
|
|
8
|
+
TJobNetwork,
|
|
9
|
+
TJsonObject,
|
|
10
|
+
TJsonValue,
|
|
11
|
+
TRunnerFeature,
|
|
12
|
+
TSourceFormat,
|
|
13
|
+
} from './interfaces.ts';
|
|
14
|
+
import * as plugins from './plugins.ts';
|
|
15
|
+
|
|
16
|
+
const identifierPattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
17
|
+
const sha256Pattern = /^[a-f0-9]{64}$/;
|
|
18
|
+
const reservedEnvironmentNames = ['FOSS_CI_INPUTS_JSON', 'FOSS_CI_MATRIX_JSON'] as const;
|
|
19
|
+
const maximumCommandArgumentBytes = 16 * 1024;
|
|
20
|
+
const maximumCommandBytes = 128 * 1024;
|
|
21
|
+
const maximumCombinedEnvironmentEntries = 128;
|
|
22
|
+
|
|
23
|
+
const transferLimitNames = [
|
|
24
|
+
'maximumSourceArchiveBytes',
|
|
25
|
+
'maximumSourceExtractedBytes',
|
|
26
|
+
'maximumSourceEntries',
|
|
27
|
+
'maximumArtifactArchiveBytes',
|
|
28
|
+
'maximumArtifactExtractedBytes',
|
|
29
|
+
'maximumArtifactEntries',
|
|
30
|
+
'maximumArtifacts',
|
|
31
|
+
'maximumCacheArchiveBytes',
|
|
32
|
+
'maximumCacheExtractedBytes',
|
|
33
|
+
'maximumCacheEntries',
|
|
34
|
+
'maximumCaches',
|
|
35
|
+
'maximumLogChunkBytes',
|
|
36
|
+
'maximumTotalLogBytes',
|
|
37
|
+
'maximumWorkspaceBytes',
|
|
38
|
+
'maximumWorkspaceEntries',
|
|
39
|
+
] as const satisfies readonly (keyof ITransferLimits)[];
|
|
40
|
+
|
|
41
|
+
const transferLimitBounds: Record<keyof ITransferLimits, readonly [number, number]> = {
|
|
42
|
+
maximumSourceArchiveBytes: [0, 8_589_934_592],
|
|
43
|
+
maximumSourceExtractedBytes: [0, 68_719_476_736],
|
|
44
|
+
maximumSourceEntries: [0, 1_000_000],
|
|
45
|
+
maximumArtifactArchiveBytes: [0, 17_179_869_184],
|
|
46
|
+
maximumArtifactExtractedBytes: [0, 137_438_953_472],
|
|
47
|
+
maximumArtifactEntries: [0, 1_000_000],
|
|
48
|
+
maximumArtifacts: [0, 64],
|
|
49
|
+
maximumCacheArchiveBytes: [0, 17_179_869_184],
|
|
50
|
+
maximumCacheExtractedBytes: [0, 137_438_953_472],
|
|
51
|
+
maximumCacheEntries: [0, 1_000_000],
|
|
52
|
+
maximumCaches: [0, 32],
|
|
53
|
+
maximumLogChunkBytes: [1, 65_536],
|
|
54
|
+
maximumTotalLogBytes: [0, 1_073_741_824],
|
|
55
|
+
maximumWorkspaceBytes: [16_777_216, 1_099_511_627_776],
|
|
56
|
+
maximumWorkspaceEntries: [1, 2_000_000],
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
60
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
61
|
+
|
|
62
|
+
const assertExactKeys = (
|
|
63
|
+
value: Record<string, unknown>,
|
|
64
|
+
required: readonly string[],
|
|
65
|
+
optional: readonly string[] = [],
|
|
66
|
+
field = 'value',
|
|
67
|
+
): void => {
|
|
68
|
+
const allowed = new Set([...required, ...optional]);
|
|
69
|
+
if (
|
|
70
|
+
required.some((key) => !Object.hasOwn(value, key)) ||
|
|
71
|
+
Object.keys(value).some((key) => !allowed.has(key))
|
|
72
|
+
) {
|
|
73
|
+
throw new ProtocolError(`Invalid ${field} shape`);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const sanitizeErrorText = (value: string): string =>
|
|
78
|
+
[...value].map((character) => {
|
|
79
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
80
|
+
return codePoint < 0x20 || codePoint === 0x7f ? '?' : character;
|
|
81
|
+
}).join('');
|
|
82
|
+
|
|
83
|
+
const safeAjvErrors = (): string => {
|
|
84
|
+
const validationErrors: unknown = validateRunnerJobSchema.errors;
|
|
85
|
+
const errors = Array.isArray(validationErrors) ? validationErrors.slice(0, 8) : [];
|
|
86
|
+
const details = errors.map((errorUnknown: unknown) => {
|
|
87
|
+
const error = isRecord(errorUnknown) ? errorUnknown : {};
|
|
88
|
+
const instancePath = typeof error.instancePath === 'string' ? error.instancePath : '/';
|
|
89
|
+
const errorMessage = typeof error.message === 'string' ? error.message : 'is invalid';
|
|
90
|
+
const path = sanitizeErrorText(instancePath).slice(0, 160);
|
|
91
|
+
const message = sanitizeErrorText(errorMessage).slice(0, 160);
|
|
92
|
+
return `${path} ${message}`;
|
|
93
|
+
}).join('; ');
|
|
94
|
+
return details.slice(0, 1024);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const ajv = new plugins.Ajv2020({ allErrors: true, strict: true });
|
|
98
|
+
const validateRunnerJobSchema = ajv.compile<ICompiledRunnerJob>(plugins.runnerJobSchema);
|
|
99
|
+
|
|
100
|
+
const assertWellFormedUnicode = (value: string): void => {
|
|
101
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
102
|
+
const codeUnit = value.charCodeAt(index);
|
|
103
|
+
if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
|
|
104
|
+
const nextCodeUnit = value.charCodeAt(index + 1);
|
|
105
|
+
if (!(nextCodeUnit >= 0xdc00 && nextCodeUnit <= 0xdfff)) {
|
|
106
|
+
throw new ProtocolError('RFC 8785 rejects lone Unicode surrogates');
|
|
107
|
+
}
|
|
108
|
+
index += 1;
|
|
109
|
+
} else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
|
|
110
|
+
throw new ProtocolError('RFC 8785 rejects lone Unicode surrogates');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const canonicalizeJsonInner = (value: unknown, ancestors: Set<object>): string => {
|
|
116
|
+
if (value === null || typeof value !== 'object') {
|
|
117
|
+
if (typeof value === 'string') assertWellFormedUnicode(value);
|
|
118
|
+
if (typeof value === 'number' && !Number.isFinite(value)) {
|
|
119
|
+
throw new ProtocolError('RFC 8785 rejects non-finite numbers');
|
|
120
|
+
}
|
|
121
|
+
const serialized = JSON.stringify(value);
|
|
122
|
+
if (serialized === undefined) {
|
|
123
|
+
throw new ProtocolError('Value is not valid canonical JSON');
|
|
124
|
+
}
|
|
125
|
+
return serialized;
|
|
126
|
+
}
|
|
127
|
+
if (ancestors.has(value)) {
|
|
128
|
+
throw new ProtocolError('Canonical JSON cannot contain cycles');
|
|
129
|
+
}
|
|
130
|
+
ancestors.add(value);
|
|
131
|
+
try {
|
|
132
|
+
if (Array.isArray(value)) {
|
|
133
|
+
return `[${value.map((entry) => canonicalizeJsonInner(entry, ancestors)).join(',')}]`;
|
|
134
|
+
}
|
|
135
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
136
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
137
|
+
throw new ProtocolError('Value is not valid canonical JSON');
|
|
138
|
+
}
|
|
139
|
+
const record = value as Record<string, unknown>;
|
|
140
|
+
const keys = Object.keys(record);
|
|
141
|
+
for (const key of keys) assertWellFormedUnicode(key);
|
|
142
|
+
return `{${
|
|
143
|
+
keys.sort().map((key) =>
|
|
144
|
+
`${JSON.stringify(key)}:${canonicalizeJsonInner(record[key], ancestors)}`
|
|
145
|
+
).join(',')
|
|
146
|
+
}}`;
|
|
147
|
+
} finally {
|
|
148
|
+
ancestors.delete(value);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const canonicalizeJson = (value: unknown): string => canonicalizeJsonInner(value, new Set());
|
|
153
|
+
|
|
154
|
+
const toJsonValue = (value: unknown): TJsonValue => {
|
|
155
|
+
canonicalizeJson(value);
|
|
156
|
+
return value as TJsonValue;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const projectCompiledPlan = (job: unknown): TJsonObject => {
|
|
160
|
+
if (!isRecord(job) || !isRecord(job.secrets)) {
|
|
161
|
+
throw new ProtocolError('Compiled runner job secrets must be an object');
|
|
162
|
+
}
|
|
163
|
+
const projection: TJsonObject = {};
|
|
164
|
+
for (const [key, value] of Object.entries(job)) {
|
|
165
|
+
if (key === 'compiledPlanDigest') continue;
|
|
166
|
+
if (key === 'secrets') {
|
|
167
|
+
projection.secrets = Object.fromEntries(
|
|
168
|
+
Object.keys(job.secrets).map((secretName) => [secretName, true]),
|
|
169
|
+
);
|
|
170
|
+
} else {
|
|
171
|
+
projection[key] = toJsonValue(value);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return projection;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const sha256 = async (value: string): Promise<string> => {
|
|
178
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
|
|
179
|
+
return [...new Uint8Array(digest)]
|
|
180
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
181
|
+
.join('');
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export const computeCompiledPlanDigest = async (job: unknown): Promise<string> =>
|
|
185
|
+
await sha256(canonicalizeJson(projectCompiledPlan(job)));
|
|
186
|
+
|
|
187
|
+
const validateIdentifier = (value: unknown, field: string): string => {
|
|
188
|
+
if (typeof value !== 'string' || !identifierPattern.test(value)) {
|
|
189
|
+
throw new ProtocolError(`Invalid ${field}`);
|
|
190
|
+
}
|
|
191
|
+
return value;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const timestampPattern =
|
|
195
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/;
|
|
196
|
+
|
|
197
|
+
const validateTimestamp = (value: unknown, field: string): string => {
|
|
198
|
+
if (typeof value !== 'string') throw new ProtocolError(`Invalid ${field}`);
|
|
199
|
+
const match = timestampPattern.exec(value);
|
|
200
|
+
if (!match) throw new ProtocolError(`Invalid ${field}`);
|
|
201
|
+
const year = Number(match[1]);
|
|
202
|
+
const month = Number(match[2]);
|
|
203
|
+
const day = Number(match[3]);
|
|
204
|
+
const hour = Number(match[4]);
|
|
205
|
+
const minute = Number(match[5]);
|
|
206
|
+
const second = Number(match[6]);
|
|
207
|
+
const offsetHour = match[7] === undefined ? 0 : Number(match[7]);
|
|
208
|
+
const offsetMinute = match[8] === undefined ? 0 : Number(match[8]);
|
|
209
|
+
const lastDay = new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
210
|
+
if (
|
|
211
|
+
month < 1 || month > 12 || day < 1 || day > lastDay || hour > 23 || minute > 59 ||
|
|
212
|
+
second > 59 || offsetHour > 23 || offsetMinute > 59 || !Number.isFinite(Date.parse(value))
|
|
213
|
+
) {
|
|
214
|
+
throw new ProtocolError(`Invalid ${field}`);
|
|
215
|
+
}
|
|
216
|
+
return value;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const validateTransferLimits = (value: unknown, field: string): ITransferLimits => {
|
|
220
|
+
if (!isRecord(value)) throw new ProtocolError(`Invalid ${field}`);
|
|
221
|
+
assertExactKeys(value, transferLimitNames, [], field);
|
|
222
|
+
const limits = {} as ITransferLimits;
|
|
223
|
+
for (const name of transferLimitNames) {
|
|
224
|
+
const entry = value[name];
|
|
225
|
+
const [minimum, maximum] = transferLimitBounds[name];
|
|
226
|
+
if (
|
|
227
|
+
typeof entry !== 'number' || !Number.isSafeInteger(entry) || entry < minimum ||
|
|
228
|
+
entry > maximum
|
|
229
|
+
) {
|
|
230
|
+
throw new ProtocolError(`Invalid ${field}.${name}`);
|
|
231
|
+
}
|
|
232
|
+
limits[name] = entry;
|
|
233
|
+
}
|
|
234
|
+
return limits;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const validateCapabilityArray = (
|
|
238
|
+
value: unknown,
|
|
239
|
+
field: string,
|
|
240
|
+
minimum: number,
|
|
241
|
+
maximum: number,
|
|
242
|
+
allowed?: ReadonlySet<string>,
|
|
243
|
+
identifiers = false,
|
|
244
|
+
): string[] => {
|
|
245
|
+
if (
|
|
246
|
+
!Array.isArray(value) || value.length < minimum || value.length > maximum ||
|
|
247
|
+
value.some((entry) => typeof entry !== 'string')
|
|
248
|
+
) {
|
|
249
|
+
throw new ProtocolError(`Invalid runner capabilities ${field}`);
|
|
250
|
+
}
|
|
251
|
+
const entries = value as string[];
|
|
252
|
+
if (
|
|
253
|
+
new Set(entries).size !== entries.length ||
|
|
254
|
+
entries.some((entry) =>
|
|
255
|
+
allowed ? !allowed.has(entry) : identifiers && !identifierPattern.test(entry)
|
|
256
|
+
)
|
|
257
|
+
) {
|
|
258
|
+
throw new ProtocolError(`Invalid runner capabilities ${field}`);
|
|
259
|
+
}
|
|
260
|
+
return [...entries];
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
export const createRunnerCapabilitySnapshot = (
|
|
264
|
+
capabilities: IRunnerCapabilities,
|
|
265
|
+
): IRunnerCapabilities => {
|
|
266
|
+
const value: unknown = capabilities;
|
|
267
|
+
if (!isRecord(value)) throw new ProtocolError('Invalid runner capabilities');
|
|
268
|
+
assertExactKeys(
|
|
269
|
+
value,
|
|
270
|
+
['executionProfiles', 'labels', 'features', 'networks', 'sourceFormats', 'limits'],
|
|
271
|
+
[],
|
|
272
|
+
'runner capabilities',
|
|
273
|
+
);
|
|
274
|
+
const executionProfiles = validateCapabilityArray(
|
|
275
|
+
value.executionProfiles,
|
|
276
|
+
'executionProfiles',
|
|
277
|
+
1,
|
|
278
|
+
16,
|
|
279
|
+
new Set(['oci.v1']),
|
|
280
|
+
) as TExecutionProfile[];
|
|
281
|
+
const labels = validateCapabilityArray(value.labels, 'labels', 0, 32, undefined, true);
|
|
282
|
+
const features = validateCapabilityArray(
|
|
283
|
+
value.features,
|
|
284
|
+
'features',
|
|
285
|
+
0,
|
|
286
|
+
16,
|
|
287
|
+
new Set(['source.tar-gz', 'artifacts.v1', 'caches.v1']),
|
|
288
|
+
) as TRunnerFeature[];
|
|
289
|
+
const networks = validateCapabilityArray(
|
|
290
|
+
value.networks,
|
|
291
|
+
'networks',
|
|
292
|
+
1,
|
|
293
|
+
2,
|
|
294
|
+
new Set(['none', 'bridge']),
|
|
295
|
+
) as TJobNetwork[];
|
|
296
|
+
const sourceFormats = validateCapabilityArray(
|
|
297
|
+
value.sourceFormats,
|
|
298
|
+
'sourceFormats',
|
|
299
|
+
1,
|
|
300
|
+
1,
|
|
301
|
+
new Set(['tar.gz']),
|
|
302
|
+
) as TSourceFormat[];
|
|
303
|
+
const limits = validateTransferLimits(value.limits, 'runner capabilities limits');
|
|
304
|
+
Object.freeze(executionProfiles);
|
|
305
|
+
Object.freeze(labels);
|
|
306
|
+
Object.freeze(features);
|
|
307
|
+
Object.freeze(networks);
|
|
308
|
+
Object.freeze(sourceFormats);
|
|
309
|
+
Object.freeze(limits);
|
|
310
|
+
return Object.freeze({ executionProfiles, labels, features, networks, sourceFormats, limits });
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const requireCapabilityFit = (
|
|
314
|
+
job: ICompiledRunnerJob,
|
|
315
|
+
capabilities: IRunnerCapabilities,
|
|
316
|
+
): void => {
|
|
317
|
+
if (!capabilities.executionProfiles.includes(job.requirements.executionProfile)) {
|
|
318
|
+
throw new ProtocolError('Job execution profile is not supported by this runner');
|
|
319
|
+
}
|
|
320
|
+
if (job.execution.profile !== job.requirements.executionProfile) {
|
|
321
|
+
throw new ProtocolError('Job execution profile does not match its requirement');
|
|
322
|
+
}
|
|
323
|
+
for (const label of job.requirements.labels) {
|
|
324
|
+
if (!capabilities.labels.includes(label)) {
|
|
325
|
+
throw new ProtocolError('Job requires an unavailable runner label');
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
for (const feature of job.requirements.features) {
|
|
329
|
+
if (!capabilities.features.includes(feature)) {
|
|
330
|
+
throw new ProtocolError('Job requires an unavailable runner feature');
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (!capabilities.networks.includes(job.execution.network)) {
|
|
334
|
+
throw new ProtocolError('Job network mode is not supported by this runner');
|
|
335
|
+
}
|
|
336
|
+
if (!capabilities.sourceFormats.includes(job.source.format)) {
|
|
337
|
+
throw new ProtocolError('Job source format is not supported by this runner');
|
|
338
|
+
}
|
|
339
|
+
for (const name of transferLimitNames) {
|
|
340
|
+
if (job.requirements.limits[name] > capabilities.limits[name]) {
|
|
341
|
+
throw new ProtocolError(`Job requirement ${name} exceeds runner capability`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const requireTransferCoherence = (job: ICompiledRunnerJob): void => {
|
|
347
|
+
const limits = job.requirements.limits;
|
|
348
|
+
if (!job.requirements.features.includes('source.tar-gz')) {
|
|
349
|
+
throw new ProtocolError('Job does not require its source transfer feature');
|
|
350
|
+
}
|
|
351
|
+
if (
|
|
352
|
+
job.source.sizeBytes > limits.maximumSourceArchiveBytes ||
|
|
353
|
+
job.source.extractedSizeBytes > limits.maximumSourceExtractedBytes ||
|
|
354
|
+
job.source.entryCount > limits.maximumSourceEntries
|
|
355
|
+
) {
|
|
356
|
+
throw new ProtocolError('Job source metadata exceeds its requirements');
|
|
357
|
+
}
|
|
358
|
+
if (job.artifacts.length > limits.maximumArtifacts) {
|
|
359
|
+
throw new ProtocolError('Job artifact count exceeds its requirements');
|
|
360
|
+
}
|
|
361
|
+
if (new Set(job.artifacts.map((artifact) => artifact.name)).size !== job.artifacts.length) {
|
|
362
|
+
throw new ProtocolError('Job artifact names must be unique');
|
|
363
|
+
}
|
|
364
|
+
for (const artifact of job.artifacts) {
|
|
365
|
+
if (
|
|
366
|
+
artifact.maximumBytes > limits.maximumArtifactArchiveBytes ||
|
|
367
|
+
artifact.maximumExtractedBytes > limits.maximumArtifactExtractedBytes ||
|
|
368
|
+
artifact.maximumEntries > limits.maximumArtifactEntries
|
|
369
|
+
) {
|
|
370
|
+
throw new ProtocolError('Job artifact declaration exceeds its requirements');
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (job.caches.length > limits.maximumCaches) {
|
|
374
|
+
throw new ProtocolError('Job cache count exceeds its requirements');
|
|
375
|
+
}
|
|
376
|
+
if (new Set(job.caches.map((cache) => cache.name)).size !== job.caches.length) {
|
|
377
|
+
throw new ProtocolError('Job cache names must be unique');
|
|
378
|
+
}
|
|
379
|
+
const cachePaths: string[] = [];
|
|
380
|
+
for (const cache of job.caches) {
|
|
381
|
+
if (
|
|
382
|
+
cache.maximumBytes > limits.maximumCacheArchiveBytes ||
|
|
383
|
+
cache.maximumExtractedBytes > limits.maximumCacheExtractedBytes ||
|
|
384
|
+
cache.maximumEntries > limits.maximumCacheEntries
|
|
385
|
+
) {
|
|
386
|
+
throw new ProtocolError('Job cache declaration exceeds its requirements');
|
|
387
|
+
}
|
|
388
|
+
if (cache.access === 'read-write' && job.permissions.caches !== 'read-write') {
|
|
389
|
+
throw new ProtocolError('Read-write cache declaration lacks read-write permission');
|
|
390
|
+
}
|
|
391
|
+
for (const path of cache.paths) {
|
|
392
|
+
if (
|
|
393
|
+
cachePaths.some((existing) =>
|
|
394
|
+
existing === path || existing.startsWith(`${path}/`) || path.startsWith(`${existing}/`)
|
|
395
|
+
)
|
|
396
|
+
) {
|
|
397
|
+
throw new ProtocolError('Job cache paths must not overlap');
|
|
398
|
+
}
|
|
399
|
+
cachePaths.push(path);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (job.artifacts.length > 0) {
|
|
403
|
+
if (
|
|
404
|
+
job.permissions.artifacts !== 'write' ||
|
|
405
|
+
!job.requirements.features.includes('artifacts.v1')
|
|
406
|
+
) {
|
|
407
|
+
throw new ProtocolError('Job artifact declarations are not authorized');
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (job.caches.length > 0) {
|
|
411
|
+
if (
|
|
412
|
+
job.permissions.caches === 'none' || !job.requirements.features.includes('caches.v1')
|
|
413
|
+
) {
|
|
414
|
+
throw new ProtocolError('Job cache declarations are not authorized');
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const requireCanonicalReservedJson = (value: string, name: string): void => {
|
|
420
|
+
let parsed: unknown;
|
|
421
|
+
try {
|
|
422
|
+
parsed = JSON.parse(value);
|
|
423
|
+
} catch {
|
|
424
|
+
throw new ProtocolError(`${name} must contain canonical JSON`);
|
|
425
|
+
}
|
|
426
|
+
if (canonicalizeJson(parsed) !== value) {
|
|
427
|
+
throw new ProtocolError(`${name} must contain canonical JSON`);
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const requireExecutionCoherence = (job: ICompiledRunnerJob): void => {
|
|
432
|
+
for (const secretName of Object.keys(job.secrets)) {
|
|
433
|
+
if (secretName.startsWith('FOSS_CI_')) {
|
|
434
|
+
throw new ProtocolError('Secret targets may not use reserved FOSS_CI names');
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
for (const step of job.steps) {
|
|
438
|
+
const environmentNames = Object.keys(step.environment);
|
|
439
|
+
for (const name of environmentNames) {
|
|
440
|
+
if (
|
|
441
|
+
name.startsWith('FOSS_CI_') && !reservedEnvironmentNames.includes(
|
|
442
|
+
name as (typeof reservedEnvironmentNames)[number],
|
|
443
|
+
)
|
|
444
|
+
) {
|
|
445
|
+
throw new ProtocolError('Step environment uses an unknown reserved FOSS_CI name');
|
|
446
|
+
}
|
|
447
|
+
if (Object.hasOwn(job.secrets, name)) {
|
|
448
|
+
throw new ProtocolError('Step environment and secret targets must be unique');
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
for (const name of reservedEnvironmentNames) {
|
|
452
|
+
if (!Object.hasOwn(step.environment, name)) {
|
|
453
|
+
throw new ProtocolError(`Step environment is missing reserved ${name}`);
|
|
454
|
+
}
|
|
455
|
+
requireCanonicalReservedJson(step.environment[name], name);
|
|
456
|
+
}
|
|
457
|
+
if (
|
|
458
|
+
environmentNames.length + Object.keys(job.secrets).length > maximumCombinedEnvironmentEntries
|
|
459
|
+
) {
|
|
460
|
+
throw new ProtocolError('Step environment and secret targets exceed the protocol limit');
|
|
461
|
+
}
|
|
462
|
+
const commandBytes = step.command.reduce((total, argument) => {
|
|
463
|
+
const argumentBytes = new TextEncoder().encode(argument).byteLength;
|
|
464
|
+
if (argumentBytes > maximumCommandArgumentBytes) {
|
|
465
|
+
throw new ProtocolError('Step command argument exceeds the UTF-8 byte limit');
|
|
466
|
+
}
|
|
467
|
+
return total + argumentBytes;
|
|
468
|
+
}, 0);
|
|
469
|
+
if (commandBytes > maximumCommandBytes) {
|
|
470
|
+
throw new ProtocolError('Step command exceeds the total UTF-8 byte limit');
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
export const validateCapabilityFit = (
|
|
476
|
+
job: ICompiledRunnerJob,
|
|
477
|
+
capabilities: IRunnerCapabilities,
|
|
478
|
+
): void => {
|
|
479
|
+
const snapshot = createRunnerCapabilitySnapshot(capabilities);
|
|
480
|
+
requireCapabilityFit(job, snapshot);
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
export class RunnerProtocolValidator {
|
|
484
|
+
private readonly capabilities: IRunnerCapabilities;
|
|
485
|
+
|
|
486
|
+
constructor(capabilities: IRunnerCapabilities) {
|
|
487
|
+
this.capabilities = createRunnerCapabilitySnapshot(capabilities);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
public async validateJob(jobUnknown: unknown): Promise<ICompiledRunnerJob> {
|
|
491
|
+
if (!validateRunnerJobSchema(jobUnknown)) {
|
|
492
|
+
const details = safeAjvErrors();
|
|
493
|
+
throw new ProtocolError(
|
|
494
|
+
`Compiled runner job failed schema validation${details ? `: ${details}` : ''}`,
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
const job = jobUnknown as ICompiledRunnerJob;
|
|
498
|
+
const digest = await computeCompiledPlanDigest(job);
|
|
499
|
+
if (!sha256Pattern.test(job.compiledPlanDigest) || digest !== job.compiledPlanDigest) {
|
|
500
|
+
throw new ProtocolError('Compiled runner job digest does not match');
|
|
501
|
+
}
|
|
502
|
+
requireTransferCoherence(job);
|
|
503
|
+
requireExecutionCoherence(job);
|
|
504
|
+
requireCapabilityFit(job, this.capabilities);
|
|
505
|
+
return job;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
public async validateJobLease(leaseUnknown: unknown): Promise<IJobLease> {
|
|
509
|
+
if (!isRecord(leaseUnknown)) throw new ProtocolError('Invalid job lease');
|
|
510
|
+
assertExactKeys(
|
|
511
|
+
leaseUnknown,
|
|
512
|
+
['jobId', 'attemptId', 'leaseId', 'fencingToken', 'leaseExpiresAt', 'job'],
|
|
513
|
+
[],
|
|
514
|
+
'job lease',
|
|
515
|
+
);
|
|
516
|
+
const fencingToken = leaseUnknown.fencingToken;
|
|
517
|
+
if (typeof fencingToken !== 'string' || fencingToken.length < 16 || fencingToken.length > 512) {
|
|
518
|
+
throw new ProtocolError('Invalid job lease fencing token');
|
|
519
|
+
}
|
|
520
|
+
return {
|
|
521
|
+
jobId: validateIdentifier(leaseUnknown.jobId, 'job lease jobId'),
|
|
522
|
+
attemptId: validateIdentifier(leaseUnknown.attemptId, 'job lease attemptId'),
|
|
523
|
+
leaseId: validateIdentifier(leaseUnknown.leaseId, 'job lease leaseId'),
|
|
524
|
+
fencingToken,
|
|
525
|
+
leaseExpiresAt: validateTimestamp(leaseUnknown.leaseExpiresAt, 'job lease leaseExpiresAt'),
|
|
526
|
+
job: await this.validateJob(leaseUnknown.job),
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export const validateJobLease = async (
|
|
532
|
+
leaseUnknown: unknown,
|
|
533
|
+
capabilities: IRunnerCapabilities,
|
|
534
|
+
): Promise<IJobLease> =>
|
|
535
|
+
await new RunnerProtocolValidator(capabilities).validateJobLease(leaseUnknown);
|
package/ts/redactor.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class StreamingSecretRedactor {
|
|
2
|
+
private readonly secrets: string[];
|
|
3
|
+
private readonly maximumSecretLength: number;
|
|
4
|
+
private buffer = '';
|
|
5
|
+
|
|
6
|
+
constructor(secrets: Iterable<string>) {
|
|
7
|
+
this.secrets = [...new Set([...secrets].filter((secret) => secret.length > 0))]
|
|
8
|
+
.sort((left, right) => right.length - left.length);
|
|
9
|
+
this.maximumSecretLength = this.secrets.reduce(
|
|
10
|
+
(maximum, secret) => Math.max(maximum, secret.length),
|
|
11
|
+
0,
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
push(chunk: string): string {
|
|
16
|
+
if (this.secrets.length === 0) return chunk;
|
|
17
|
+
this.buffer += chunk;
|
|
18
|
+
return this.consume(false);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
flush(): string {
|
|
22
|
+
if (this.secrets.length === 0) return '';
|
|
23
|
+
return this.consume(true);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private consume(flush: boolean): string {
|
|
27
|
+
let output = '';
|
|
28
|
+
while (this.buffer.length > 0 && (flush || this.buffer.length >= this.maximumSecretLength)) {
|
|
29
|
+
const matchingSecret = this.secrets.find((secret) => this.buffer.startsWith(secret));
|
|
30
|
+
if (matchingSecret) {
|
|
31
|
+
output += '***';
|
|
32
|
+
this.buffer = this.buffer.slice(matchingSecret.length);
|
|
33
|
+
} else {
|
|
34
|
+
const [firstCodePoint] = this.buffer;
|
|
35
|
+
output += firstCodePoint;
|
|
36
|
+
this.buffer = this.buffer.slice(firstCodePoint.length);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return output;
|
|
40
|
+
}
|
|
41
|
+
}
|