@playcraft/build 0.2.17 → 0.2.19-test-a
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/dist/audio-source-files.d.ts +5 -0
- package/dist/audio-source-files.js +135 -0
- package/dist/base-builder.js +42 -2
- package/dist/delivery/agent-build-runner.d.ts +44 -0
- package/dist/delivery/agent-build-runner.js +365 -0
- package/dist/delivery/agent-build-source.d.ts +8 -0
- package/dist/delivery/agent-build-source.js +25 -0
- package/dist/delivery/artifact-descriptor.d.ts +31 -0
- package/dist/delivery/artifact-descriptor.js +237 -0
- package/dist/delivery/artifact-package.d.ts +43 -0
- package/dist/delivery/artifact-package.js +283 -0
- package/dist/delivery/build-plan.d.ts +4 -0
- package/dist/delivery/build-plan.js +189 -0
- package/dist/delivery/build-plan.types.d.ts +51 -0
- package/dist/delivery/build-plan.types.js +1 -0
- package/dist/delivery/command-isolation.d.ts +21 -0
- package/dist/delivery/command-isolation.js +327 -0
- package/dist/delivery/commands.d.ts +5 -0
- package/dist/delivery/commands.js +146 -0
- package/dist/delivery/files.d.ts +9 -0
- package/dist/delivery/files.js +144 -0
- package/dist/delivery/git-source-fingerprint.d.ts +2 -0
- package/dist/delivery/git-source-fingerprint.js +58 -0
- package/dist/delivery/isolated-source.d.ts +5 -0
- package/dist/delivery/isolated-source.js +148 -0
- package/dist/delivery/output-publication.d.ts +21 -0
- package/dist/delivery/output-publication.js +444 -0
- package/dist/delivery/project-delivery.d.ts +6 -0
- package/dist/delivery/project-delivery.js +287 -0
- package/dist/delivery/recipe-build-plan.d.ts +4 -0
- package/dist/delivery/recipe-build-plan.js +53 -0
- package/dist/delivery/recipe.d.ts +20 -0
- package/dist/delivery/recipe.js +211 -0
- package/dist/delivery/safe-path.d.ts +2 -0
- package/dist/delivery/safe-path.js +18 -0
- package/dist/delivery/types.d.ts +81 -0
- package/dist/delivery/types.js +1 -0
- package/dist/engines/engine-detector.js +4 -0
- package/dist/engines/generic-adapter.js +14 -19
- package/dist/engines/playable-scripts-adapter.js +2 -5
- package/dist/index.d.ts +21 -4
- package/dist/index.js +12 -2
- package/dist/platforms/adikteev.js +1 -0
- package/dist/platforms/applovin.js +1 -0
- package/dist/platforms/base.d.ts +4 -0
- package/dist/platforms/base.js +25 -6
- package/dist/platforms/bigo.js +1 -0
- package/dist/platforms/facebook.js +1 -0
- package/dist/platforms/google.js +1 -0
- package/dist/platforms/inmobi.js +1 -0
- package/dist/platforms/ironsource.js +1 -0
- package/dist/platforms/liftoff.js +1 -0
- package/dist/platforms/mintegral.js +1 -0
- package/dist/platforms/moloco.js +1 -0
- package/dist/platforms/playcraft.js +1 -0
- package/dist/platforms/remerge.js +1 -0
- package/dist/platforms/snapchat.js +1 -0
- package/dist/platforms/tiktok.js +1 -0
- package/dist/platforms/unity.js +1 -0
- package/dist/production-animation-presence.d.ts +2 -0
- package/dist/production-animation-presence.js +146 -0
- package/dist/production-inputs.d.ts +30 -0
- package/dist/production-inputs.js +262 -0
- package/dist/production-source-paths.d.ts +5 -0
- package/dist/production-source-paths.js +27 -0
- package/dist/redirect-policy.d.ts +23 -0
- package/dist/redirect-policy.js +25 -0
- package/dist/types.d.ts +12 -2
- package/dist/validation/artifact-qa.d.ts +36 -0
- package/dist/validation/artifact-qa.js +263 -0
- package/dist/validation/artifact-rules.d.ts +39 -0
- package/dist/validation/artifact-rules.js +451 -0
- package/dist/validation/artifact.d.ts +14 -0
- package/dist/validation/artifact.js +188 -0
- package/dist/validation/html-validator.d.ts +5 -0
- package/dist/validation/html-validator.js +19 -17
- package/dist/validation/index.d.ts +5 -0
- package/dist/validation/index.js +5 -0
- package/dist/validation/package-validator.d.ts +2 -0
- package/dist/validation/package-validator.js +25 -64
- package/dist/validation/playwright-driver.d.ts +37 -0
- package/dist/validation/playwright-driver.js +302 -0
- package/dist/validation/profiles.d.ts +13 -1
- package/dist/validation/profiles.js +27 -0
- package/dist/validation/runtime-cover.d.ts +32 -0
- package/dist/validation/runtime-cover.js +120 -0
- package/dist/validation/runtime-load.d.ts +141 -0
- package/dist/validation/runtime-load.js +812 -0
- package/dist/validation/source-scanner.d.ts +11 -0
- package/dist/validation/source-scanner.js +32 -2
- package/dist/validation/types.d.ts +201 -2
- package/dist/validation/types.js +1 -1
- package/dist/validation/validation-runner.d.ts +22 -2
- package/dist/validation/validation-runner.js +64 -3
- package/dist/validation/zip-reader.d.ts +7 -0
- package/dist/validation/zip-reader.js +159 -26
- package/dist/vite/config-builder-generic.d.ts +3 -1
- package/dist/vite/config-builder-generic.js +9 -7
- package/dist/vite/config-builder.js +1 -1
- package/dist/vite/plugin-platform.js +16 -30
- package/dist/vite/plugin-source-builder.d.ts +2 -14
- package/dist/vite/plugin-source-builder.js +61 -997
- package/dist/vite/source-builder-asset-paths.d.ts +25 -0
- package/dist/vite/source-builder-asset-paths.js +154 -0
- package/dist/vite/source-builder-esm.d.ts +7 -0
- package/dist/vite/source-builder-esm.js +877 -0
- package/dist/vite/source-builder-types.d.ts +14 -0
- package/dist/vite/source-builder-types.js +1 -0
- package/dist/vite/workspace.d.ts +6 -0
- package/dist/vite/workspace.js +41 -0
- package/dist/vite-builder.d.ts +4 -5
- package/dist/vite-builder.js +33 -34
- package/dist/workspace-css-imports.d.ts +2 -0
- package/dist/workspace-css-imports.js +139 -0
- package/dist/workspace-model-source.d.ts +14 -0
- package/dist/workspace-model-source.js +184 -0
- package/dist/workspace-public-imports.d.ts +11 -0
- package/dist/workspace-public-imports.js +232 -0
- package/dist/workspace-public-source.d.ts +15 -0
- package/dist/workspace-public-source.js +564 -0
- package/package.json +30 -4
- package/templates/patches/playcraft-redirect-policy.js +115 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { agentBuildSourceRulesVersionSchema } from '@playcraft/common/cloud-workspace/runtime';
|
|
3
|
+
import { CHANNEL_RULE_PROFILES } from '../validation/profiles.js';
|
|
4
|
+
import { ProjectDeliveryError, relativePath } from './recipe.js';
|
|
5
|
+
const HASH = /^[a-f0-9]{64}$/;
|
|
6
|
+
const COMMIT = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
|
|
7
|
+
const FORMATS = ['html', 'zip'];
|
|
8
|
+
const PACKAGE_MANAGERS = ['pnpm', 'npm', 'yarn', 'bun'];
|
|
9
|
+
const COMPILE_STRATEGIES = ['project-command', 'platform-vite', 'project-owned'];
|
|
10
|
+
const PACKAGE_STRATEGIES = ['single-html', 'normalized-zip', 'project-owned'];
|
|
11
|
+
function invalid(message, file) {
|
|
12
|
+
throw new ProjectDeliveryError('delivery_build_plan_invalid', message, file);
|
|
13
|
+
}
|
|
14
|
+
function record(value, field) {
|
|
15
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
16
|
+
invalid(`${field} must be an object`);
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function keys(value, allowed, field) {
|
|
21
|
+
const actual = Object.keys(value).sort();
|
|
22
|
+
const expected = [...allowed].sort();
|
|
23
|
+
if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
|
|
24
|
+
invalid(`${field} has missing or unsupported fields`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function allowedKeys(value, allowed, field) {
|
|
28
|
+
if (Object.keys(value).some((key) => !allowed.includes(key))) {
|
|
29
|
+
invalid(`${field} has unsupported fields`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function text(value, field, maximum = 4096) {
|
|
33
|
+
if (typeof value !== 'string' || !value.trim() || value.length > maximum || value.includes('\0')) {
|
|
34
|
+
invalid(`Invalid ${field}`);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function hash(value, field) {
|
|
39
|
+
const result = text(value, field, 64);
|
|
40
|
+
if (!HASH.test(result))
|
|
41
|
+
invalid(`${field} must be a lowercase SHA-256 digest`);
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
function choice(value, values, field) {
|
|
45
|
+
if (typeof value !== 'string' || !values.includes(value))
|
|
46
|
+
invalid(`Invalid ${field}`);
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function command(value, field) {
|
|
50
|
+
const input = record(value, field);
|
|
51
|
+
allowedKeys(input, ['argv', 'cwd', 'timeoutMs'], field);
|
|
52
|
+
if (!Array.isArray(input.argv) || input.argv.length === 0 || input.argv.length > 128) {
|
|
53
|
+
invalid(`${field}.argv must be a nonempty bounded array`);
|
|
54
|
+
}
|
|
55
|
+
const result = {
|
|
56
|
+
argv: input.argv.map((argument, index) => text(argument, `${field}.argv[${index}]`)),
|
|
57
|
+
};
|
|
58
|
+
if (input.cwd !== undefined) {
|
|
59
|
+
result.cwd = relativePath(text(input.cwd, `${field}.cwd`), `${field}.cwd`, true);
|
|
60
|
+
}
|
|
61
|
+
if (input.timeoutMs !== undefined) {
|
|
62
|
+
if (!Number.isInteger(input.timeoutMs) || Number(input.timeoutMs) < 1000 || Number(input.timeoutMs) > 1800000) {
|
|
63
|
+
invalid(`${field}.timeoutMs must be between 1000 and 1800000`);
|
|
64
|
+
}
|
|
65
|
+
result.timeoutMs = Number(input.timeoutMs);
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
function optionalCommands(value, field) {
|
|
70
|
+
if (!Array.isArray(value) || value.length > 16)
|
|
71
|
+
invalid(`Invalid ${field}`);
|
|
72
|
+
return value.map((item, index) => command(item, `${field}[${index}]`));
|
|
73
|
+
}
|
|
74
|
+
function relativePaths(value, field) {
|
|
75
|
+
if (!Array.isArray(value) || value.length > 32)
|
|
76
|
+
invalid(`Invalid ${field}`);
|
|
77
|
+
const paths = value.map((item, index) => relativePath(text(item, `${field}[${index}]`), `${field}[${index}]`));
|
|
78
|
+
if (new Set(paths).size !== paths.length)
|
|
79
|
+
invalid(`${field} contains duplicates`);
|
|
80
|
+
return paths;
|
|
81
|
+
}
|
|
82
|
+
/** Strictly parses the portable plan; absolute Runtime workspace paths are deliberately absent. */
|
|
83
|
+
export function parseBuildPlan(value) {
|
|
84
|
+
const input = record(value, 'buildPlan');
|
|
85
|
+
keys(input, ['planVersion', 'source', 'target', 'environment', 'setup', 'compile', 'delivery'], 'buildPlan');
|
|
86
|
+
if (input.planVersion !== 1)
|
|
87
|
+
invalid('buildPlan.planVersion must be 1');
|
|
88
|
+
const source = record(input.source, 'source');
|
|
89
|
+
keys(source, ['commitHash', 'sourceFingerprint', 'sourceRulesVersion', 'recipeDigest'], 'source');
|
|
90
|
+
const commitHash = text(source.commitHash, 'source.commitHash', 64);
|
|
91
|
+
if (!COMMIT.test(commitHash))
|
|
92
|
+
invalid('source.commitHash must be a lowercase Git object id');
|
|
93
|
+
const sourceRules = agentBuildSourceRulesVersionSchema.safeParse(source.sourceRulesVersion);
|
|
94
|
+
if (!sourceRules.success)
|
|
95
|
+
invalid('Unsupported source.sourceRulesVersion');
|
|
96
|
+
const target = record(input.target, 'target');
|
|
97
|
+
keys(target, ['channel', 'format'], 'target');
|
|
98
|
+
const channel = text(target.channel, 'target.channel', 64);
|
|
99
|
+
if (!Object.prototype.hasOwnProperty.call(CHANNEL_RULE_PROFILES, channel))
|
|
100
|
+
invalid('Unsupported target.channel');
|
|
101
|
+
const format = choice(target.format, FORMATS, 'target.format');
|
|
102
|
+
const environment = record(input.environment, 'environment');
|
|
103
|
+
keys(environment, ['packageManager', 'executables', 'isolation', 'maxDurationMs'], 'environment');
|
|
104
|
+
if (environment.isolation !== 'managed-delivery-v1')
|
|
105
|
+
invalid('Unsupported environment.isolation');
|
|
106
|
+
if (!Number.isInteger(environment.maxDurationMs) || Number(environment.maxDurationMs) < 1000 || Number(environment.maxDurationMs) > 1800000) {
|
|
107
|
+
invalid('environment.maxDurationMs must be between 1000 and 1800000');
|
|
108
|
+
}
|
|
109
|
+
if (!Array.isArray(environment.executables) || environment.executables.length > 32) {
|
|
110
|
+
invalid('Invalid environment.executables');
|
|
111
|
+
}
|
|
112
|
+
const setup = record(input.setup, 'setup');
|
|
113
|
+
keys(setup, ['install', 'prepareCommands', 'generatedDirectories'], 'setup');
|
|
114
|
+
if (setup.install !== 'frozen-lockfile')
|
|
115
|
+
invalid('setup.install must be frozen-lockfile');
|
|
116
|
+
const compile = record(input.compile, 'compile');
|
|
117
|
+
const output = record(compile.output, 'compile.output');
|
|
118
|
+
keys(output, ['kind', 'directory', 'entry'], 'compile.output');
|
|
119
|
+
const kind = choice(output.kind, ['web-bundle', 'channel-package'], 'compile.output.kind');
|
|
120
|
+
const strategy = choice(compile.strategy, COMPILE_STRATEGIES, 'compile.strategy');
|
|
121
|
+
const scope = choice(compile.scope, ['shared', 'per-channel'], 'compile.scope');
|
|
122
|
+
const hasCommand = compile.command !== undefined;
|
|
123
|
+
keys(compile, hasCommand ? ['strategy', 'command', 'scope', 'output'] : ['strategy', 'scope', 'output'], 'compile');
|
|
124
|
+
if (strategy !== 'project-owned' && !hasCommand) {
|
|
125
|
+
invalid('Project-command and platform-vite strategies require a compile command');
|
|
126
|
+
}
|
|
127
|
+
if (scope === 'shared' && (kind !== 'web-bundle' || strategy === 'project-owned')) {
|
|
128
|
+
invalid('Only a project-compiled web-bundle can be shared across channels');
|
|
129
|
+
}
|
|
130
|
+
const delivery = record(input.delivery, 'delivery');
|
|
131
|
+
keys(delivery, ['channelRulesVersion', 'packageStrategy'], 'delivery');
|
|
132
|
+
const packageStrategy = choice(delivery.packageStrategy, PACKAGE_STRATEGIES, 'delivery.packageStrategy');
|
|
133
|
+
if (kind === 'channel-package' && (strategy === 'platform-vite' || packageStrategy !== 'project-owned')) {
|
|
134
|
+
invalid('A channel-package must stay project-owned and must not enter platform Vite packaging');
|
|
135
|
+
}
|
|
136
|
+
if (kind === 'web-bundle' && packageStrategy === 'project-owned') {
|
|
137
|
+
invalid('A web-bundle requires platform-owned final packaging');
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
planVersion: 1,
|
|
141
|
+
source: {
|
|
142
|
+
commitHash,
|
|
143
|
+
sourceFingerprint: hash(source.sourceFingerprint, 'source.sourceFingerprint'),
|
|
144
|
+
sourceRulesVersion: sourceRules.data,
|
|
145
|
+
recipeDigest: hash(source.recipeDigest, 'source.recipeDigest'),
|
|
146
|
+
},
|
|
147
|
+
target: { channel: channel, format },
|
|
148
|
+
environment: {
|
|
149
|
+
packageManager: choice(environment.packageManager, PACKAGE_MANAGERS, 'environment.packageManager'),
|
|
150
|
+
executables: environment.executables.map((item, index) => text(item, `environment.executables[${index}]`)),
|
|
151
|
+
isolation: 'managed-delivery-v1',
|
|
152
|
+
maxDurationMs: Number(environment.maxDurationMs),
|
|
153
|
+
},
|
|
154
|
+
setup: {
|
|
155
|
+
install: 'frozen-lockfile',
|
|
156
|
+
prepareCommands: optionalCommands(setup.prepareCommands, 'setup.prepareCommands'),
|
|
157
|
+
generatedDirectories: relativePaths(setup.generatedDirectories, 'setup.generatedDirectories'),
|
|
158
|
+
},
|
|
159
|
+
compile: {
|
|
160
|
+
strategy,
|
|
161
|
+
...(hasCommand ? { command: command(compile.command, 'compile.command') } : {}),
|
|
162
|
+
scope,
|
|
163
|
+
output: {
|
|
164
|
+
kind,
|
|
165
|
+
directory: relativePath(text(output.directory, 'compile.output.directory'), 'compile.output.directory'),
|
|
166
|
+
entry: relativePath(text(output.entry, 'compile.output.entry'), 'compile.output.entry'),
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
delivery: {
|
|
170
|
+
channelRulesVersion: text(delivery.channelRulesVersion, 'delivery.channelRulesVersion', 64),
|
|
171
|
+
packageStrategy,
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function canonical(value) {
|
|
176
|
+
if (Array.isArray(value))
|
|
177
|
+
return `[${value.map(canonical).join(',')}]`;
|
|
178
|
+
if (value && typeof value === 'object') {
|
|
179
|
+
const entries = Object.entries(value)
|
|
180
|
+
.filter(([, item]) => item !== undefined)
|
|
181
|
+
.sort(([left], [right]) => left.localeCompare(right));
|
|
182
|
+
return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(',')}}`;
|
|
183
|
+
}
|
|
184
|
+
return JSON.stringify(value) ?? 'null';
|
|
185
|
+
}
|
|
186
|
+
export function buildPlanDigest(value) {
|
|
187
|
+
const plan = parseBuildPlan(value);
|
|
188
|
+
return createHash('sha256').update(`playcraft-build-plan-v1\0${canonical(plan)}`).digest('hex');
|
|
189
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { AgentBuildSourceRulesVersion } from '@playcraft/common/cloud-workspace/runtime';
|
|
2
|
+
import type { OutputFormat, Platform } from '../types.js';
|
|
3
|
+
import type { DeliveryCommand } from './types.js';
|
|
4
|
+
export type BuildPackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun';
|
|
5
|
+
export type BuildPlan = {
|
|
6
|
+
planVersion: 1;
|
|
7
|
+
source: {
|
|
8
|
+
commitHash: string;
|
|
9
|
+
sourceFingerprint: string;
|
|
10
|
+
sourceRulesVersion: AgentBuildSourceRulesVersion;
|
|
11
|
+
recipeDigest: string;
|
|
12
|
+
};
|
|
13
|
+
target: {
|
|
14
|
+
channel: Platform;
|
|
15
|
+
format: OutputFormat;
|
|
16
|
+
};
|
|
17
|
+
environment: {
|
|
18
|
+
packageManager: BuildPackageManager;
|
|
19
|
+
executables: string[];
|
|
20
|
+
isolation: 'managed-delivery-v1';
|
|
21
|
+
maxDurationMs: number;
|
|
22
|
+
};
|
|
23
|
+
setup: {
|
|
24
|
+
install: 'frozen-lockfile';
|
|
25
|
+
prepareCommands: DeliveryCommand[];
|
|
26
|
+
generatedDirectories: string[];
|
|
27
|
+
};
|
|
28
|
+
compile: {
|
|
29
|
+
strategy: 'project-command' | 'platform-vite' | 'project-owned';
|
|
30
|
+
command?: DeliveryCommand;
|
|
31
|
+
scope: 'shared' | 'per-channel';
|
|
32
|
+
output: {
|
|
33
|
+
kind: 'web-bundle' | 'channel-package';
|
|
34
|
+
directory: string;
|
|
35
|
+
entry: string;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
delivery: {
|
|
39
|
+
channelRulesVersion: string;
|
|
40
|
+
packageStrategy: 'single-html' | 'normalized-zip' | 'project-owned';
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export type RecipeBuildPlanOptions = {
|
|
44
|
+
commitHash: string;
|
|
45
|
+
sourceFingerprint: string;
|
|
46
|
+
sourceRulesVersion: AgentBuildSourceRulesVersion;
|
|
47
|
+
channel: Platform;
|
|
48
|
+
format: OutputFormat;
|
|
49
|
+
packageManager: BuildPackageManager;
|
|
50
|
+
maxDurationMs?: number;
|
|
51
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type DeliveryCommandIsolation = {
|
|
2
|
+
projectRoot: string;
|
|
3
|
+
isolatedRoot: string;
|
|
4
|
+
dependencyRoots?: readonly string[];
|
|
5
|
+
temporaryRoot?: string;
|
|
6
|
+
};
|
|
7
|
+
type AllowedPath = {
|
|
8
|
+
path: string;
|
|
9
|
+
directory: boolean;
|
|
10
|
+
dev: string;
|
|
11
|
+
ino: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Mount-namespace view equivalent to the Landlock rules: only allowances exist,
|
|
15
|
+
* read-only except the snapshot; /dev is the minimal device set and /proc only
|
|
16
|
+
* shows the new PID namespace. The network stays shared, as with Landlock.
|
|
17
|
+
*/
|
|
18
|
+
export declare function bubblewrapArguments(read: readonly Pick<AllowedPath, 'path'>[], isolated: string, links: ReadonlyMap<string, string>): string[];
|
|
19
|
+
/** Construct argv for the existing managed launcher; never execute or fall back here. */
|
|
20
|
+
export declare function wrapDeliveryCommand(argv: readonly string[], options: DeliveryCommandIsolation): Promise<string[]>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { access, lstat, realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { ProjectDeliveryError } from './recipe.js';
|
|
5
|
+
const SYSTEM_PATHS = [
|
|
6
|
+
'/bin',
|
|
7
|
+
'/usr/bin',
|
|
8
|
+
'/usr/lib',
|
|
9
|
+
'/usr/lib64',
|
|
10
|
+
'/usr/libexec',
|
|
11
|
+
'/lib',
|
|
12
|
+
'/lib64',
|
|
13
|
+
'/usr/local/bin',
|
|
14
|
+
'/usr/local/lib',
|
|
15
|
+
'/usr/share/nodejs',
|
|
16
|
+
'/etc/ld.so.cache',
|
|
17
|
+
'/etc/localtime',
|
|
18
|
+
'/etc/nsswitch.conf',
|
|
19
|
+
'/etc/passwd',
|
|
20
|
+
'/etc/group',
|
|
21
|
+
'/etc/ssl/openssl.cnf',
|
|
22
|
+
'/etc/crypto-policies/back-ends/opensslcnf.config',
|
|
23
|
+
'/dev/null',
|
|
24
|
+
'/dev/zero',
|
|
25
|
+
'/dev/random',
|
|
26
|
+
'/dev/urandom',
|
|
27
|
+
];
|
|
28
|
+
const MAC_SYSTEM_PATHS = [
|
|
29
|
+
'/System/Library',
|
|
30
|
+
'/System/Volumes/Preboot/Cryptexes/OS/System/Library',
|
|
31
|
+
'/System/Volumes/Preboot/Cryptexes/OS/usr/lib',
|
|
32
|
+
'/Library/Apple/System/Library',
|
|
33
|
+
'/Library/Developer/CommandLineTools',
|
|
34
|
+
'/Applications/Xcode.app/Contents/Developer',
|
|
35
|
+
'/private/var/db/dyld',
|
|
36
|
+
'/opt/homebrew/bin',
|
|
37
|
+
'/opt/homebrew/lib',
|
|
38
|
+
'/opt/homebrew/opt',
|
|
39
|
+
'/opt/homebrew/Cellar',
|
|
40
|
+
'/opt/homebrew/share',
|
|
41
|
+
'/opt/homebrew/etc/openssl@3/openssl.cnf',
|
|
42
|
+
'/usr/local/Cellar',
|
|
43
|
+
'/usr/local/opt',
|
|
44
|
+
'/usr/local/share',
|
|
45
|
+
'/usr/local/etc/openssl@3/openssl.cnf',
|
|
46
|
+
];
|
|
47
|
+
function inside(root, candidate) {
|
|
48
|
+
return candidate === root || candidate.startsWith(`${root}${path.sep}`);
|
|
49
|
+
}
|
|
50
|
+
function failure(message) {
|
|
51
|
+
throw new ProjectDeliveryError('delivery_isolation_unavailable', message);
|
|
52
|
+
}
|
|
53
|
+
async function allowedPath(value, optional = false) {
|
|
54
|
+
try {
|
|
55
|
+
const canonical = await realpath(value);
|
|
56
|
+
const info = await stat(canonical, { bigint: true });
|
|
57
|
+
if (!info.isDirectory() && !info.isFile() && !info.isCharacterDevice())
|
|
58
|
+
failure('An isolation allowance has an unsupported file type');
|
|
59
|
+
return {
|
|
60
|
+
path: canonical,
|
|
61
|
+
directory: info.isDirectory(),
|
|
62
|
+
dev: info.dev.toString(),
|
|
63
|
+
ino: info.ino.toString(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (optional && error.code === 'ENOENT')
|
|
68
|
+
return undefined;
|
|
69
|
+
if (error instanceof ProjectDeliveryError)
|
|
70
|
+
throw error;
|
|
71
|
+
failure('An isolation allowance could not be verified');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function executable(value) {
|
|
75
|
+
try {
|
|
76
|
+
await access(value, constants.X_OK);
|
|
77
|
+
if (!(await stat(value)).isFile())
|
|
78
|
+
failure('The isolation launcher is not a file');
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
failure('The required operating-system isolation launcher is unavailable');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Kernel user API: https://docs.kernel.org/userspace-api/landlock.html .
|
|
85
|
+
// ABI (Application Binary Interface, 应用二进制接口) 3 is required: both
|
|
86
|
+
// cross-directory REFER and TRUNCATE must be handled. A kernel without that ABI
|
|
87
|
+
// (for example a guest kernel built without Landlock) uses the same allowance
|
|
88
|
+
// list through Bubblewrap mount namespaces; without either, nothing executes.
|
|
89
|
+
// The packed path-beneath structure and syscall numbers come from Linux's
|
|
90
|
+
// include/uapi/linux/landlock.h and x86/asm-generic syscall tables.
|
|
91
|
+
const LANDLOCK_LAUNCHER = String.raw `
|
|
92
|
+
import ctypes, json, os, platform, resource, stat, sys
|
|
93
|
+
|
|
94
|
+
BUBBLEWRAP = "/usr/bin/bwrap"
|
|
95
|
+
|
|
96
|
+
def fail(reason):
|
|
97
|
+
sys.stderr.write("delivery_isolation_unavailable:" + reason + "\n")
|
|
98
|
+
sys.exit(125)
|
|
99
|
+
|
|
100
|
+
def verified(item):
|
|
101
|
+
if os.path.realpath(item["path"]) != item["path"]:
|
|
102
|
+
fail("allowance_changed")
|
|
103
|
+
fd = os.open(item["path"], os.O_PATH | os.O_CLOEXEC | os.O_NOFOLLOW)
|
|
104
|
+
info = os.fstat(fd)
|
|
105
|
+
if str(info.st_dev) != item["dev"] or str(info.st_ino) != item["ino"]:
|
|
106
|
+
fail("allowance_changed")
|
|
107
|
+
return fd, info
|
|
108
|
+
|
|
109
|
+
def launch_bubblewrap(policy, command):
|
|
110
|
+
arguments = policy.get("bwrap")
|
|
111
|
+
if not arguments:
|
|
112
|
+
fail("landlock_abi_3_required")
|
|
113
|
+
try:
|
|
114
|
+
info = os.stat(BUBBLEWRAP)
|
|
115
|
+
except OSError:
|
|
116
|
+
fail("landlock_abi_3_required_and_bubblewrap_missing")
|
|
117
|
+
# Never a project-controlled or user-writable launcher.
|
|
118
|
+
if not stat.S_ISREG(info.st_mode) or info.st_uid != 0 or info.st_mode & 0o022 or not os.access(BUBBLEWRAP, os.X_OK):
|
|
119
|
+
fail("bubblewrap_untrusted")
|
|
120
|
+
for item in policy["read"]:
|
|
121
|
+
os.close(verified(item)[0])
|
|
122
|
+
os.environ["TMPDIR"] = policy["temporary"]
|
|
123
|
+
os.environ["TMP"] = policy["temporary"]
|
|
124
|
+
os.environ["TEMP"] = policy["temporary"]
|
|
125
|
+
try:
|
|
126
|
+
os.execv(BUBBLEWRAP, [BUBBLEWRAP] + arguments + ["--"] + command)
|
|
127
|
+
except OSError:
|
|
128
|
+
fail("bubblewrap_exec_failed")
|
|
129
|
+
|
|
130
|
+
try:
|
|
131
|
+
if sys.platform != "linux" or platform.machine() not in ("x86_64", "aarch64", "riscv64"):
|
|
132
|
+
fail("unsupported_landlock_architecture")
|
|
133
|
+
policy = json.loads(sys.argv[1])
|
|
134
|
+
command = sys.argv[2:]
|
|
135
|
+
if not command:
|
|
136
|
+
fail("missing_command")
|
|
137
|
+
# Landlock does not revoke already-open descriptors. Preserve only managed
|
|
138
|
+
# stdio, never an inherited directory/file handle into the original tree.
|
|
139
|
+
ceiling = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
|
|
140
|
+
if ceiling == resource.RLIM_INFINITY:
|
|
141
|
+
ceiling = os.sysconf("SC_OPEN_MAX")
|
|
142
|
+
os.closerange(3, ceiling)
|
|
143
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
144
|
+
libc.syscall.restype = ctypes.c_long
|
|
145
|
+
abi = libc.syscall(444, ctypes.c_void_p(), ctypes.c_size_t(0), ctypes.c_uint(1))
|
|
146
|
+
if abi < 3:
|
|
147
|
+
launch_bubblewrap(policy, command)
|
|
148
|
+
handled = (1 << 15) - 1
|
|
149
|
+
if abi >= 5:
|
|
150
|
+
handled |= 1 << 15
|
|
151
|
+
if abi >= 9:
|
|
152
|
+
handled |= 1 << 16
|
|
153
|
+
class Ruleset(ctypes.Structure):
|
|
154
|
+
_fields_ = [("handled_access_fs", ctypes.c_uint64)]
|
|
155
|
+
class Beneath(ctypes.Structure):
|
|
156
|
+
_pack_ = 1
|
|
157
|
+
_fields_ = [("allowed_access", ctypes.c_uint64), ("parent_fd", ctypes.c_int32)]
|
|
158
|
+
attr = Ruleset(handled)
|
|
159
|
+
ruleset = libc.syscall(444, ctypes.byref(attr), ctypes.sizeof(attr), 0)
|
|
160
|
+
if ruleset < 0:
|
|
161
|
+
fail("landlock_ruleset_failed")
|
|
162
|
+
read = (1 << 0) | (1 << 2) | (1 << 3)
|
|
163
|
+
write = (1 << 1) | (1 << 4) | (1 << 5) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 12) | (1 << 13) | (1 << 14)
|
|
164
|
+
file_rights = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 14) | (1 << 15)
|
|
165
|
+
for item in policy["read"]:
|
|
166
|
+
fd, info = verified(item)
|
|
167
|
+
rights = read
|
|
168
|
+
if item["path"] in policy["write"]:
|
|
169
|
+
rights |= write
|
|
170
|
+
if not stat.S_ISDIR(info.st_mode):
|
|
171
|
+
rights &= file_rights
|
|
172
|
+
rule = Beneath(rights & handled, fd)
|
|
173
|
+
result = libc.syscall(445, ruleset, 1, ctypes.byref(rule), 0)
|
|
174
|
+
os.close(fd)
|
|
175
|
+
if result != 0:
|
|
176
|
+
fail("landlock_rule_failed")
|
|
177
|
+
# A fresh, single-threaded launcher applies policy before exec; all future
|
|
178
|
+
# threads/children inherit it. Do not spawn an untracked second executor.
|
|
179
|
+
if libc.prctl(38, 1, 0, 0, 0) != 0:
|
|
180
|
+
fail("no_new_privs_failed")
|
|
181
|
+
if libc.syscall(446, ruleset, 0) != 0:
|
|
182
|
+
fail("landlock_restrict_failed")
|
|
183
|
+
os.close(ruleset)
|
|
184
|
+
os.environ["TMPDIR"] = policy["temporary"]
|
|
185
|
+
os.environ["TMP"] = policy["temporary"]
|
|
186
|
+
os.environ["TEMP"] = policy["temporary"]
|
|
187
|
+
os.execvpe(command[0], command, os.environ)
|
|
188
|
+
except Exception:
|
|
189
|
+
fail("landlock_setup_or_exec_failed")
|
|
190
|
+
`;
|
|
191
|
+
/**
|
|
192
|
+
* Mount-namespace view equivalent to the Landlock rules: only allowances exist,
|
|
193
|
+
* read-only except the snapshot; /dev is the minimal device set and /proc only
|
|
194
|
+
* shows the new PID namespace. The network stays shared, as with Landlock.
|
|
195
|
+
*/
|
|
196
|
+
export function bubblewrapArguments(read, isolated, links) {
|
|
197
|
+
const view = [
|
|
198
|
+
'--unshare-user',
|
|
199
|
+
'--unshare-pid',
|
|
200
|
+
'--unshare-ipc',
|
|
201
|
+
'--unshare-uts',
|
|
202
|
+
'--unshare-cgroup-try',
|
|
203
|
+
'--die-with-parent',
|
|
204
|
+
'--new-session',
|
|
205
|
+
'--cap-drop',
|
|
206
|
+
'ALL',
|
|
207
|
+
'--dev',
|
|
208
|
+
'/dev',
|
|
209
|
+
'--proc',
|
|
210
|
+
'/proc',
|
|
211
|
+
];
|
|
212
|
+
for (const item of read) {
|
|
213
|
+
if (item.path === isolated || item.path === '/dev' || item.path.startsWith('/dev/'))
|
|
214
|
+
continue;
|
|
215
|
+
view.push('--ro-bind', item.path, item.path);
|
|
216
|
+
}
|
|
217
|
+
view.push('--bind', isolated, isolated);
|
|
218
|
+
for (const [alias, target] of links)
|
|
219
|
+
view.push('--symlink', target, alias);
|
|
220
|
+
// The bwrap root is a private tmpfs; keep it unwritable so only the snapshot accepts writes.
|
|
221
|
+
view.push('--remount-ro', '/');
|
|
222
|
+
return view;
|
|
223
|
+
}
|
|
224
|
+
/** Construct argv for the existing managed launcher; never execute or fall back here. */
|
|
225
|
+
export async function wrapDeliveryCommand(argv, options) {
|
|
226
|
+
if (!argv[0] || argv.some((value) => value.includes('\0')))
|
|
227
|
+
failure('The isolated command is invalid');
|
|
228
|
+
if (process.platform !== 'darwin' && process.platform !== 'linux')
|
|
229
|
+
failure('Delivery file isolation is unsupported on this operating system');
|
|
230
|
+
// Loader injection would execute before Python/Seatbelt establishes policy.
|
|
231
|
+
if (['LD_PRELOAD', 'LD_AUDIT', 'DYLD_INSERT_LIBRARIES', 'DYLD_LIBRARY_PATH'].some((key) => process.env[key]))
|
|
232
|
+
failure('Inherited loader injection is incompatible with delivery file isolation');
|
|
233
|
+
const project = await allowedPath(options.projectRoot);
|
|
234
|
+
const isolated = await allowedPath(options.isolatedRoot);
|
|
235
|
+
if (!project?.directory || !isolated?.directory || inside(isolated.path, project.path))
|
|
236
|
+
failure('The snapshot must be a separate directory from its source');
|
|
237
|
+
const temporary = options.temporaryRoot === undefined ? isolated : await allowedPath(options.temporaryRoot);
|
|
238
|
+
if (!temporary?.directory || !inside(isolated.path, temporary.path))
|
|
239
|
+
failure('The temporary directory must be inside the snapshot');
|
|
240
|
+
const dependencies = [];
|
|
241
|
+
for (const candidate of options.dependencyRoots ?? []) {
|
|
242
|
+
if (path.basename(path.resolve(candidate)) !== 'node_modules')
|
|
243
|
+
failure('Only a genuine node_modules directory can be a dependency allowance');
|
|
244
|
+
const info = await lstat(candidate).catch(() => undefined);
|
|
245
|
+
if (!info?.isDirectory() || info.isSymbolicLink())
|
|
246
|
+
failure('Dependency allowances must be real directories');
|
|
247
|
+
const dependency = await allowedPath(candidate);
|
|
248
|
+
if (!dependency?.directory ||
|
|
249
|
+
inside(dependency.path, project.path) ||
|
|
250
|
+
inside(dependency.path, isolated.path) ||
|
|
251
|
+
inside(isolated.path, dependency.path))
|
|
252
|
+
failure('Dependency allowances cannot expose the source or snapshot parent');
|
|
253
|
+
dependencies.push(dependency);
|
|
254
|
+
}
|
|
255
|
+
const roots = new Map();
|
|
256
|
+
for (const value of [isolated, ...dependencies])
|
|
257
|
+
roots.set(value.path, value);
|
|
258
|
+
const node = await allowedPath(process.execPath);
|
|
259
|
+
const nodePrefix = node ? path.dirname(path.dirname(node.path)) : undefined;
|
|
260
|
+
const candidates = [
|
|
261
|
+
...SYSTEM_PATHS,
|
|
262
|
+
...(process.platform === 'darwin' ? MAC_SYSTEM_PATHS : []),
|
|
263
|
+
process.execPath,
|
|
264
|
+
// Accommodate a versioned Node installation without admitting its home directory.
|
|
265
|
+
...(nodePrefix ? [path.join(nodePrefix, 'bin'), path.join(nodePrefix, 'lib')] : []),
|
|
266
|
+
];
|
|
267
|
+
// Merged-/usr aliases such as /bin -> /usr/bin; a mount view must recreate them.
|
|
268
|
+
const links = new Map();
|
|
269
|
+
for (const candidate of candidates) {
|
|
270
|
+
const item = await allowedPath(candidate, true);
|
|
271
|
+
if (!item)
|
|
272
|
+
continue;
|
|
273
|
+
if (inside(item.path, project.path) || inside(project.path, item.path))
|
|
274
|
+
failure('A system toolchain allowance overlaps the original project');
|
|
275
|
+
if (inside(item.path, isolated.path))
|
|
276
|
+
failure('A system toolchain allowance contains the snapshot');
|
|
277
|
+
roots.set(item.path, item);
|
|
278
|
+
const alias = path.resolve(candidate);
|
|
279
|
+
if (alias !== item.path)
|
|
280
|
+
links.set(alias, item.path);
|
|
281
|
+
}
|
|
282
|
+
const read = [...roots.values()];
|
|
283
|
+
const write = [isolated.path, (await realpath('/dev/null'))];
|
|
284
|
+
if (process.platform === 'darwin') {
|
|
285
|
+
await executable('/usr/bin/sandbox-exec');
|
|
286
|
+
const filter = (item) => `(${item.directory ? 'subpath' : 'literal'} ${JSON.stringify(item.path)})`;
|
|
287
|
+
const readable = read.map(filter).join(' ');
|
|
288
|
+
const profile = [
|
|
289
|
+
'(version 1)',
|
|
290
|
+
'(allow default)',
|
|
291
|
+
// Metadata remains visible, as with Landlock. New dyld/libignition also
|
|
292
|
+
// opens / as a directory anchor; literal / grants no descendant access.
|
|
293
|
+
// Deny actions take precedence over allow actions in Seatbelt, so encode
|
|
294
|
+
// the exceptions in the deny predicate rather than an overriding allow.
|
|
295
|
+
`(deny file-read-data (require-not (require-any (literal "/") ${readable})))`,
|
|
296
|
+
`(deny file-read-xattr (require-not (require-any ${readable})))`,
|
|
297
|
+
`(deny file-write* (require-not (require-any (subpath ${JSON.stringify(isolated.path)}) (literal "/dev/null"))))`,
|
|
298
|
+
].join('\n');
|
|
299
|
+
// env is itself sandboxed; it only confines tool temporary files to the snapshot.
|
|
300
|
+
return [
|
|
301
|
+
'/usr/bin/sandbox-exec',
|
|
302
|
+
'-p',
|
|
303
|
+
profile,
|
|
304
|
+
'/usr/bin/env',
|
|
305
|
+
`TMPDIR=${temporary.path}`,
|
|
306
|
+
`TMP=${temporary.path}`,
|
|
307
|
+
`TEMP=${temporary.path}`,
|
|
308
|
+
...argv,
|
|
309
|
+
];
|
|
310
|
+
}
|
|
311
|
+
// Use the OS interpreter, never a project-controlled PATH shim or PYTHONPATH.
|
|
312
|
+
await executable('/usr/bin/python3');
|
|
313
|
+
return [
|
|
314
|
+
'/usr/bin/python3',
|
|
315
|
+
'-I',
|
|
316
|
+
'-S',
|
|
317
|
+
'-c',
|
|
318
|
+
LANDLOCK_LAUNCHER,
|
|
319
|
+
JSON.stringify({
|
|
320
|
+
read,
|
|
321
|
+
write,
|
|
322
|
+
temporary: temporary.path,
|
|
323
|
+
bwrap: bubblewrapArguments(read, isolated.path, links),
|
|
324
|
+
}),
|
|
325
|
+
...argv,
|
|
326
|
+
];
|
|
327
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DeliveryCommand, ProjectDeliveryOptions } from './types.js';
|
|
2
|
+
import { wrapDeliveryCommand } from './command-isolation.js';
|
|
3
|
+
export type DeliveryCommandIsolation = Parameters<typeof wrapDeliveryCommand>[1];
|
|
4
|
+
export declare function requireExecutable(root: string, executable: string, cwd: string, options: ProjectDeliveryOptions, isolation?: DeliveryCommandIsolation): Promise<string>;
|
|
5
|
+
export declare function runDeliveryCommand(root: string, command: DeliveryCommand, replace: (value: string) => string, options: ProjectDeliveryOptions, isolation?: DeliveryCommandIsolation): Promise<void>;
|