@ship.zone/runner 0.2.4 → 1.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/changelog.md +74 -0
- package/deno.json +18 -4
- package/deno.lock +143 -224
- package/dist_npm/mod.d.ts +297 -39
- package/dist_npm/mod.js +941 -628
- package/dist_rust/runnerworkspace_linux_amd64_musl +0 -0
- package/dist_rust/runnerworkspace_linux_amd64_musl.tsrust-build.json +14 -0
- package/dist_rust/runnerworkspace_linux_arm64_musl +0 -0
- package/dist_rust/runnerworkspace_linux_arm64_musl.tsrust-build.json +14 -0
- package/package.json +3 -3
- package/readme.md +65 -18
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/api-client.ts +85 -304
- package/ts/artifact-manager.ts +9 -3
- package/ts/cache-manager.ts +3 -2
- package/ts/cli.ts +16 -6
- package/ts/config.ts +50 -16
- package/ts/errors.ts +11 -0
- package/ts/index.ts +2 -0
- package/ts/interfaces.ts +196 -32
- package/ts/log-sender.ts +18 -9
- package/ts/oci-executor.ts +48 -22
- package/ts/plugins.ts +5 -1
- package/ts/protocol-validator.ts +261 -224
- package/ts/redactor.ts +229 -20
- package/ts/registration-state.ts +12 -45
- package/ts/runner.ts +116 -22
- package/ts/spec-version.ts +66 -0
- package/ts/transfer-archive.ts +25 -3
- package/ts/validation.ts +20 -0
- package/ts/wire-schema.ts +137 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ProtocolError } from './errors.ts';
|
|
2
|
+
import * as plugins from './plugins.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Outcome of the specification version acceptance rule of the runner protocol: a declared
|
|
6
|
+
* version is accepted when it is canonical, has the implemented major version, and is not newer.
|
|
7
|
+
*/
|
|
8
|
+
export type TSpecAcceptance = 'accepted' | 'malformed' | 'major-mismatch' | 'newer';
|
|
9
|
+
|
|
10
|
+
interface ISpecVersionParts {
|
|
11
|
+
major: bigint;
|
|
12
|
+
minor: bigint;
|
|
13
|
+
patch: bigint;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const canonicalSpecVersionPattern =
|
|
17
|
+
/^(0|[1-9][0-9]{0,14})\.(0|[1-9][0-9]{0,14})\.(0|[1-9][0-9]{0,14})$/;
|
|
18
|
+
|
|
19
|
+
const parseSpecVersion = (value: unknown): ISpecVersionParts | undefined => {
|
|
20
|
+
if (typeof value !== 'string') return undefined;
|
|
21
|
+
const match = canonicalSpecVersionPattern.exec(value);
|
|
22
|
+
if (!match) return undefined;
|
|
23
|
+
return { major: BigInt(match[1]), minor: BigInt(match[2]), patch: BigInt(match[3]) };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const requireCanonical = (value: string, field: string): ISpecVersionParts => {
|
|
27
|
+
const parts = parseSpecVersion(value);
|
|
28
|
+
if (!parts) throw new ProtocolError(`${field} is not a canonical specification version`);
|
|
29
|
+
return parts;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const compareParts = (left: ISpecVersionParts, right: ISpecVersionParts): number => {
|
|
33
|
+
for (const key of ['major', 'minor', 'patch'] as const) {
|
|
34
|
+
if (left[key] !== right[key]) return left[key] < right[key] ? -1 : 1;
|
|
35
|
+
}
|
|
36
|
+
return 0;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** The specification version this runner implements: the ci-spec package it is built against. */
|
|
40
|
+
export const runnerSpecVersion: string = plugins.ciSpecVersion;
|
|
41
|
+
requireCanonical(runnerSpecVersion, 'The implemented specification version');
|
|
42
|
+
|
|
43
|
+
/** Applies the acceptance rule of an implementation of version `implemented` to `declared`. */
|
|
44
|
+
export const acceptSpecVersion = (implemented: string, declared: unknown): TSpecAcceptance => {
|
|
45
|
+
const implementedParts = requireCanonical(implemented, 'The implemented specification version');
|
|
46
|
+
const declaredParts = parseSpecVersion(declared);
|
|
47
|
+
if (!declaredParts) return 'malformed';
|
|
48
|
+
if (declaredParts.major !== implementedParts.major) return 'major-mismatch';
|
|
49
|
+
return compareParts(declaredParts, implementedParts) > 0 ? 'newer' : 'accepted';
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Returns whether two canonical versions share a major version, the coordinator session rule. */
|
|
53
|
+
export const specMajorVersionsMatch = (left: string, right: string): boolean =>
|
|
54
|
+
requireCanonical(left, 'Specification version').major ===
|
|
55
|
+
requireCanonical(right, 'Specification version').major;
|
|
56
|
+
|
|
57
|
+
/** Compares two canonical specification versions numerically. */
|
|
58
|
+
export const compareSpecVersions = (left: string, right: string): number =>
|
|
59
|
+
compareParts(
|
|
60
|
+
requireCanonical(left, 'Specification version'),
|
|
61
|
+
requireCanonical(right, 'Specification version'),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
/** The lower of two canonical versions: the version whose constructs a message may use. */
|
|
65
|
+
export const lowerSpecVersion = (left: string, right: string): string =>
|
|
66
|
+
compareSpecVersions(left, right) <= 0 ? left : right;
|
package/ts/transfer-archive.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { dirname, join, resolve } from '@std/path';
|
|
3
3
|
import { maximumArchivePathMetadataBytes } from './plugins.ts';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ProtocolError,
|
|
6
|
+
ResourceCleanupError,
|
|
7
|
+
RunnerError,
|
|
8
|
+
SecretDisclosureError,
|
|
9
|
+
} from './errors.ts';
|
|
5
10
|
import type {
|
|
11
|
+
ISecretRedactionSet,
|
|
6
12
|
ITransferDownload,
|
|
7
13
|
ITransferMetadata,
|
|
8
14
|
ITransferRecord,
|
|
@@ -65,6 +71,8 @@ export interface ICreateTarGzipOptions {
|
|
|
65
71
|
maximumEntries: number;
|
|
66
72
|
maximumPathMetadataBytes?: number;
|
|
67
73
|
allowSymlinks: boolean;
|
|
74
|
+
/** Every entry path, link target, and file content is checked against this set. */
|
|
75
|
+
redactionSet: ISecretRedactionSet;
|
|
68
76
|
requireAllSelections?: boolean;
|
|
69
77
|
signal?: AbortSignal;
|
|
70
78
|
fileOperations?: ITransferArchiveFileOperations;
|
|
@@ -1789,6 +1797,7 @@ async function* tarChunks(
|
|
|
1789
1797
|
root: string,
|
|
1790
1798
|
rootDevice: number,
|
|
1791
1799
|
openRead: NonNullable<ITransferArchiveFileOperations['openRead']>,
|
|
1800
|
+
redactionSet: ISecretRedactionSet,
|
|
1792
1801
|
signal?: AbortSignal,
|
|
1793
1802
|
): AsyncGenerator<Uint8Array> {
|
|
1794
1803
|
let index = 0;
|
|
@@ -1796,6 +1805,12 @@ async function* tarChunks(
|
|
|
1796
1805
|
if (signal?.aborted) throw signal.reason;
|
|
1797
1806
|
await assertSafeAncestors(root, entry.path, rootDevice);
|
|
1798
1807
|
await assertSnapshot(entry.absolutePath, entry.snapshot);
|
|
1808
|
+
if (
|
|
1809
|
+
redactionSet.occursIn(entry.path) ||
|
|
1810
|
+
(entry.linkTarget !== undefined && redactionSet.occursIn(entry.linkTarget))
|
|
1811
|
+
) {
|
|
1812
|
+
throw new SecretDisclosureError();
|
|
1813
|
+
}
|
|
1799
1814
|
const paxValues: Array<[string, string]> = [];
|
|
1800
1815
|
let pathField = ustarPath(entry.path);
|
|
1801
1816
|
if (!pathField) {
|
|
@@ -1832,6 +1847,9 @@ async function* tarChunks(
|
|
|
1832
1847
|
};
|
|
1833
1848
|
try {
|
|
1834
1849
|
await assertOpenFileSnapshot(file, entry.snapshot);
|
|
1850
|
+
const contentMatcher = redactionSet.empty
|
|
1851
|
+
? undefined
|
|
1852
|
+
: redactionSet.createContentMatcher();
|
|
1835
1853
|
const buffer = new Uint8Array(streamBufferBytes);
|
|
1836
1854
|
let emitted = 0;
|
|
1837
1855
|
while (true) {
|
|
@@ -1842,7 +1860,9 @@ async function* tarChunks(
|
|
|
1842
1860
|
if (emitted > entry.sizeBytes) {
|
|
1843
1861
|
throw new RunnerError('Workspace export file grew during archive creation');
|
|
1844
1862
|
}
|
|
1845
|
-
|
|
1863
|
+
const content = buffer.slice(0, read);
|
|
1864
|
+
if (contentMatcher?.push(content)) throw new SecretDisclosureError();
|
|
1865
|
+
yield content;
|
|
1846
1866
|
}
|
|
1847
1867
|
if (emitted !== entry.sizeBytes) {
|
|
1848
1868
|
throw new RunnerError('Workspace export file shrank during archive creation');
|
|
@@ -1934,6 +1954,7 @@ export const createTarGzipArchive = async (
|
|
|
1934
1954
|
collected.root,
|
|
1935
1955
|
collected.rootSnapshot.dev,
|
|
1936
1956
|
fileOperations.openRead ?? defaultFileOperations.openRead!,
|
|
1957
|
+
options.redactionSet,
|
|
1937
1958
|
options.signal,
|
|
1938
1959
|
),
|
|
1939
1960
|
).pipeThrough(compressor);
|
|
@@ -1964,7 +1985,8 @@ export const createTarGzipArchive = async (
|
|
|
1964
1985
|
try {
|
|
1965
1986
|
await reader?.cancel(error);
|
|
1966
1987
|
} catch (cleanupError) {
|
|
1967
|
-
|
|
1988
|
+
// Canceling a stream that already errored rejects with that same stored error.
|
|
1989
|
+
if (cleanupError !== error) errors.push(cleanupError);
|
|
1968
1990
|
}
|
|
1969
1991
|
try {
|
|
1970
1992
|
reader?.releaseLock();
|
package/ts/validation.ts
CHANGED
|
@@ -75,3 +75,23 @@ export const validateHttpUrl = (
|
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
export const utf8Length = (value: string): number => new TextEncoder().encode(value).byteLength;
|
|
78
|
+
|
|
79
|
+
const protocolTimestampPattern =
|
|
80
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/;
|
|
81
|
+
|
|
82
|
+
/** RFC 3339 `date-time` with calendar and clock range checks, as used by protocol timestamps. */
|
|
83
|
+
export const isProtocolTimestamp = (value: string): boolean => {
|
|
84
|
+
const match = protocolTimestampPattern.exec(value);
|
|
85
|
+
if (!match || !Number.isFinite(Date.parse(value))) return false;
|
|
86
|
+
const year = Number(match[1]);
|
|
87
|
+
const month = Number(match[2]);
|
|
88
|
+
const day = Number(match[3]);
|
|
89
|
+
const hour = Number(match[4]);
|
|
90
|
+
const minute = Number(match[5]);
|
|
91
|
+
const second = Number(match[6]);
|
|
92
|
+
const offsetHour = match[7] === undefined ? 0 : Number(match[7]);
|
|
93
|
+
const offsetMinute = match[8] === undefined ? 0 : Number(match[8]);
|
|
94
|
+
return month >= 1 && month <= 12 && day >= 1 &&
|
|
95
|
+
day <= new Date(Date.UTC(year, month, 0)).getUTCDate() && hour <= 23 && minute <= 59 &&
|
|
96
|
+
second <= 59 && offsetHour <= 23 && offsetMinute <= 59;
|
|
97
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { ProtocolError } from './errors.ts';
|
|
2
|
+
import * as plugins from './plugins.ts';
|
|
3
|
+
import { isProtocolTimestamp, isRecord } from './validation.ts';
|
|
4
|
+
|
|
5
|
+
/** OpenAPI component schemas of the published runner protocol that this runner validates. */
|
|
6
|
+
export const wireSchemaNames = [
|
|
7
|
+
'AbandonJobRequest',
|
|
8
|
+
'AcceptJobRequest',
|
|
9
|
+
'ClaimJobRequest',
|
|
10
|
+
'ClaimJobResponse',
|
|
11
|
+
'CompleteJobRequest',
|
|
12
|
+
'CompleteJobResponse',
|
|
13
|
+
'DiscoveryResponse',
|
|
14
|
+
'ErrorResponse',
|
|
15
|
+
'ImageBinding',
|
|
16
|
+
'ImageGrant',
|
|
17
|
+
'ImageRecord',
|
|
18
|
+
'JobHeartbeatRequest',
|
|
19
|
+
'JobHeartbeatResponse',
|
|
20
|
+
'JobLease',
|
|
21
|
+
'LogAcknowledgement',
|
|
22
|
+
'LogChunk',
|
|
23
|
+
'NpmGrant',
|
|
24
|
+
'RecordImageRequest',
|
|
25
|
+
'RecoverAttemptRequest',
|
|
26
|
+
'RegisterRunnerRequest',
|
|
27
|
+
'RegisterRunnerResponse',
|
|
28
|
+
'RunnerCapabilities',
|
|
29
|
+
'RunnerHeartbeatRequest',
|
|
30
|
+
'RunnerHeartbeatResponse',
|
|
31
|
+
'StartSessionRequest',
|
|
32
|
+
'StartSessionResponse',
|
|
33
|
+
'TransferRecord',
|
|
34
|
+
] as const;
|
|
35
|
+
|
|
36
|
+
export type TWireSchemaName = (typeof wireSchemaNames)[number];
|
|
37
|
+
|
|
38
|
+
export const isWireSchemaName = (value: unknown): value is TWireSchemaName =>
|
|
39
|
+
typeof value === 'string' && (wireSchemaNames as readonly string[]).includes(value);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The OpenAPI document references the job schema relative to its own location
|
|
43
|
+
* (`../schemas/runner-job.schema.json`); registering both under these identifiers resolves those
|
|
44
|
+
* references exactly as the published package lays them out.
|
|
45
|
+
*/
|
|
46
|
+
const openApiSchemaId = 'https://foss.global/spec/runner.openapi.json';
|
|
47
|
+
const runnerJobSchemaKey = 'https://foss.global/schemas/runner-job.schema.json';
|
|
48
|
+
|
|
49
|
+
const rewriteComponentReferences = (value: unknown): unknown => {
|
|
50
|
+
if (Array.isArray(value)) return value.map(rewriteComponentReferences);
|
|
51
|
+
if (!isRecord(value)) return value;
|
|
52
|
+
return Object.fromEntries(
|
|
53
|
+
Object.entries(value).map(([key, entry]) => [
|
|
54
|
+
key,
|
|
55
|
+
key === '$ref' && typeof entry === 'string'
|
|
56
|
+
? entry.replace('#/components/schemas/', '#/$defs/')
|
|
57
|
+
: rewriteComponentReferences(entry),
|
|
58
|
+
]),
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const componentSchemas = (): unknown => {
|
|
63
|
+
const document: unknown = plugins.runnerOpenApi;
|
|
64
|
+
if (!isRecord(document) || !isRecord(document.components)) {
|
|
65
|
+
throw new ProtocolError('The published runner OpenAPI document has no components');
|
|
66
|
+
}
|
|
67
|
+
const schemas = document.components.schemas;
|
|
68
|
+
if (!isRecord(schemas)) {
|
|
69
|
+
throw new ProtocolError('The published runner OpenAPI document has no component schemas');
|
|
70
|
+
}
|
|
71
|
+
return rewriteComponentReferences(schemas);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const ajv = new plugins.Ajv2020({ allErrors: true, strict: true });
|
|
75
|
+
ajv.addFormat('date-time', { type: 'string', validate: isProtocolTimestamp });
|
|
76
|
+
ajv.addSchema(plugins.runnerJobSchema, runnerJobSchemaKey);
|
|
77
|
+
ajv.addSchema({
|
|
78
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
79
|
+
$id: openApiSchemaId,
|
|
80
|
+
$defs: componentSchemas(),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
type TValidateFunction = NonNullable<ReturnType<typeof ajv.getSchema>>;
|
|
84
|
+
|
|
85
|
+
const validatorCache = new Map<string, TValidateFunction>();
|
|
86
|
+
|
|
87
|
+
const validatorFor = (reference: string): TValidateFunction => {
|
|
88
|
+
const cached = validatorCache.get(reference);
|
|
89
|
+
if (cached) return cached;
|
|
90
|
+
const validate = ajv.getSchema(reference);
|
|
91
|
+
if (!validate) throw new ProtocolError(`The published schema ${reference} is unavailable`);
|
|
92
|
+
validatorCache.set(reference, validate);
|
|
93
|
+
return validate;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const componentValidator = (name: TWireSchemaName): TValidateFunction =>
|
|
97
|
+
validatorFor(`${openApiSchemaId}#/$defs/${name}`);
|
|
98
|
+
|
|
99
|
+
const sanitizeErrorText = (value: string): string =>
|
|
100
|
+
[...value].map((character) => {
|
|
101
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
102
|
+
return codePoint < 0x20 || codePoint === 0x7f ? '?' : character;
|
|
103
|
+
}).join('');
|
|
104
|
+
|
|
105
|
+
/** Bounded, control-character-free schema error details that never echo instance values. */
|
|
106
|
+
const safeErrorDetails = (validate: TValidateFunction): string => {
|
|
107
|
+
const validationErrors: unknown = validate.errors;
|
|
108
|
+
const errors = Array.isArray(validationErrors) ? validationErrors.slice(0, 8) : [];
|
|
109
|
+
return errors.map((errorUnknown: unknown) => {
|
|
110
|
+
const error = isRecord(errorUnknown) ? errorUnknown : {};
|
|
111
|
+
const instancePath = typeof error.instancePath === 'string' ? error.instancePath : '/';
|
|
112
|
+
const message = typeof error.message === 'string' ? error.message : 'is invalid';
|
|
113
|
+
return `${sanitizeErrorText(instancePath).slice(0, 160)} ${
|
|
114
|
+
sanitizeErrorText(message).slice(0, 160)
|
|
115
|
+
}`;
|
|
116
|
+
}).join('; ').slice(0, 1024);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const assertValid = <T>(validate: TValidateFunction, value: unknown, label: string): T => {
|
|
120
|
+
if (!validate(value)) {
|
|
121
|
+
const details = safeErrorDetails(validate);
|
|
122
|
+
throw new ProtocolError(`${label} failed schema validation${details ? `: ${details}` : ''}`);
|
|
123
|
+
}
|
|
124
|
+
return value as T;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/** Returns whether a value is valid against one published runner protocol component. */
|
|
128
|
+
export const isValidWireMessage = (name: TWireSchemaName, value: unknown): boolean =>
|
|
129
|
+
componentValidator(name)(value) === true;
|
|
130
|
+
|
|
131
|
+
/** Validates a value against one published runner protocol component and returns it typed. */
|
|
132
|
+
export const validateWireMessage = <T>(name: TWireSchemaName, value: unknown): T =>
|
|
133
|
+
assertValid<T>(componentValidator(name), value, name);
|
|
134
|
+
|
|
135
|
+
/** Validates a compiled runner job against the published runner job schema. */
|
|
136
|
+
export const validateCompiledJobSchema = <T>(value: unknown): T =>
|
|
137
|
+
assertValid<T>(validatorFor(runnerJobSchemaKey), value, 'Compiled runner job');
|