security(spawn): strip dynamic-linker env vars from subprocess env#800
Merged
security(spawn): strip dynamic-linker env vars from subprocess env#800
Conversation
Craft 2.25.5 sanitised process.env at startup to prevent LD_PRELOAD injection (PR #794). That closes the primary vector — .craft.env on disk can no longer hydrate process.env — but it does not help if some later code path re-adds LD_PRELOAD to process.env (hostile or accidental mutation) or if a caller explicitly constructs an `options.env` object that contains one of these keys (e.g. via a future `{ ...process.env, ...custom }` spread). In either case the child process would still inherit the injection. Defence-in-depth: sanitise the env that reaches child_process.spawn itself. Every call to `spawnProcess` in src/utils/system.ts now routes `options.env ?? process.env` through sanitizeSpawnEnv() before handing it to spawn(), stripping LD_PRELOAD, LD_LIBRARY_PATH, LD_AUDIT, and the DYLD_* family. The same CRAFT_ALLOW_DYNAMIC_LINKER_ENV=1 escape hatch honoured at startup also applies here; both paths log a one-time info message when the opt-out is in effect. Implementation: - Moved the dynamic-linker key list + opt-out name + both sanitisers into a new leaf module src/utils/dynamicLinkerEnv.ts. src/utils/env.ts re-exports the same names for backward compat. The move avoids a circular import: src/utils/system.ts needs the sanitiser, but src/utils/env.ts transitively pulls in src/config.ts and the artifact providers, which import back into src/utils/system.ts. Tests: - 7 new unit tests on sanitizeSpawnEnv (no-op for undefined, shallow copy, no mutation, stripping all DYLD_* variants, opt-out behaviour, values never logged). - 3 new integration tests on spawnProcess that actually spawn a subprocess and verify LD_PRELOAD does not reach it, including the "set on process.env after startup" regression scenario.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends the dynamic-linker env-var sanitisation landed in #794 so it also applies to every subprocess Craft spawns — not just to Craft's own
process.envat startup. Defence-in-depth against two residual attack surfaces:process.env— hostile or accidental code that setsLD_PRELOADback intoprocess.envafter Craft's startup sanitiser has run. The subprocess would still inherit it.options.env— any future refactor that does{ ...process.env, ...custom }(or takes env from an external source) could smuggle a dynamic-linker key into a child without going throughprocess.env.Neither is exploitable today — #794 closed the primary
.craft.envvector and Craft doesn't currently mutateprocess.envor take env from external files — but the pattern is cheap to make robust before it becomes a regression footgun.Change
src/utils/system.ts:spawnProcessnow routesoptions.env ?? process.envthrough a newsanitizeSpawnEnv()helper before handing it tochild_process.spawn. The helper:LD_PRELOAD,LD_LIBRARY_PATH,LD_AUDIT, and theDYLD_*family (same set as the startup sanitiser).CRAFT_ALLOW_DYNAMIC_LINKER_ENV=1opt-out assanitizeDynamicLinkerEnv.Where the code lives
A new leaf module
src/utils/dynamicLinkerEnv.tsholds the constants and both sanitisers.src/utils/env.tsre-exports the same names so existing imports (notablysrc/index.ts) keep working.Why the move:
src/utils/system.tsneeds the sanitiser, butsrc/utils/env.tstransitively importssrc/config.ts→ artifact providers → which import back intosrc/utils/system.ts. A directsystem.ts → env.tsedge creates a cycle that breaks test loading (observed it locally —Class extends value undefinedwhenBaseArtifactProviderwasn't ready yet). Leaf module with onlyloggeras a dep sidesteps this cleanly.Tests
src/utils/__tests__/env.test.tsonsanitizeSpawnEnv(undefined input passthrough, shallow copy behaviour, input-not-mutated invariant, stripping allDYLD_*variants, opt-out behaviour, value-not-in-log property, strict opt-out equality check).src/utils/__tests__/system.test.tsthat actually spawnnodeas a subprocess and read backprocess.env.LD_PRELOADfrom inside the child:LD_PRELOADinoptions.env→ child seesundefined;LD_PRELOADset onprocess.envpost-startup → child seesundefined;CRAFT_ALLOW_DYNAMIC_LINKER_ENV=1→ child sees the value (opt-out works).Full suite: 969 passed (same 7 pre-existing e2e failures unrelated to this work).
pnpm build+pnpm lint src/clean.Notes for reviewers
CRAFT_ALLOW_DYNAMIC_LINKER_ENV=1and must be set inprocess.env, notoptions.env. If a caller passesLD_PRELOADinoptions.envand the opt-out is not set onprocess.env, it gets stripped — even if the caller "intended" it. This is consistent with the startup sanitiser.buildReleaseCommandEnv()(PR fix(security): disable .craft.env reading and harden release subprocess env #794) already produces an env that cannot contain dynamic-linker keys because its allowlist doesn't include them. This PR is a backstop behind that.