Repository navigation
Make guarded safe-output assets readable by the uploader #75761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c3f996e
9d48a81
5c3319a
900bf50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,9 +130,10 @@ function resolveIssueArtifactPath(filename) { | |
| * @param {string} directory | ||
| * @param {boolean} create | ||
| * @param {(directoryPath: string) => T} operation | ||
| * @param {number} [directoryMode] | ||
| * @returns {T} | ||
| */ | ||
| function withGuardedDirectory(directory, create, operation) { | ||
| function withGuardedDirectory(directory, create, operation, directoryMode = 0o700) { | ||
| if (process.platform !== "linux") { | ||
| throw assetValidationError("Guarded asset access requires Linux descriptor-relative paths; macOS and Windows are unsupported"); | ||
| } | ||
|
|
@@ -151,12 +152,14 @@ function withGuardedDirectory(directory, create, operation) { | |
| current = `/proc/self/fd/${parent.fd}/${component}`; | ||
| } | ||
| let stat; | ||
| let created = false; | ||
| try { | ||
| stat = lstatGuard(current); | ||
| } catch (error) { | ||
| if (!create || !error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") throw error; | ||
| try { | ||
| fs.mkdirSync(current, { mode: 0o700 }); | ||
| fs.mkdirSync(current, { mode: directoryMode }); | ||
| created = true; | ||
| } catch (mkdirError) { | ||
| if (!mkdirError || typeof mkdirError !== "object" || !("code" in mkdirError) || mkdirError.code !== "EEXIST") throw mkdirError; | ||
| } | ||
|
|
@@ -168,6 +171,7 @@ function withGuardedDirectory(directory, create, operation) { | |
| directories.push({ fd, filename: current, stat }); | ||
| const opened = fs.fstatSync(fd); | ||
| if (opened.dev !== stat.dev || opened.ino !== stat.ino) throw assetValidationError("Asset directory changed while opening"); | ||
| if (created) fs.fchmodSync(fd, directoryMode); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Staging now uses the private
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conversely, an ancestor that already exists with the old default 0700 mode is never chmodded because
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ownership handoff now ensures the backend and uploader use the same runner UID/GID; existing private directories need no permission broadening. The Linux runner-identity test exercises writing and host-side reading without requiring root. Updated in |
||
| } | ||
| const verify = () => { | ||
| for (const directory of directories) { | ||
|
|
@@ -230,18 +234,27 @@ function readGuardedFile(filename, maximumBytes = 10 * 1024 * 1024) { | |
| * Exclusively create a staged/published file without following destination links. | ||
| * @param {string} filename | ||
| * @param {Buffer} content | ||
| * @param {{mode?: number, directoryMode?: number}} [options] | ||
| */ | ||
| function writeGuardedFile(filename, content) { | ||
| return withGuardedDirectory(path.dirname(filename), true, directory => { | ||
| const target = path.join(directory, path.basename(filename)); | ||
| const fd = fs.openSync(target, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600); | ||
| try { | ||
| fs.fchmodSync(fd, 0o600); | ||
| fs.writeFileSync(fd, content); | ||
| } finally { | ||
| fs.closeSync(fd); | ||
| } | ||
| }); | ||
| function writeGuardedFile(filename, content, { mode = 0o600, directoryMode = 0o700 } = {}) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] @copilot please address this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added validation for only the |
||
| if (!((mode === 0o600 && directoryMode === 0o700) || (mode === 0o644 && directoryMode === 0o755))) { | ||
| throw assetValidationError("Invalid guarded asset permissions"); | ||
| } | ||
| return withGuardedDirectory( | ||
| path.dirname(filename), | ||
| true, | ||
| directory => { | ||
| const target = path.join(directory, path.basename(filename)); | ||
| const fd = fs.openSync(target, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600); | ||
| try { | ||
| fs.writeFileSync(fd, content); | ||
| fs.fchmodSync(fd, mode); | ||
| } finally { | ||
| fs.closeSync(fd); | ||
| } | ||
| }, | ||
| directoryMode | ||
| ); | ||
| } | ||
|
|
||
| module.exports = { lstatGuard, normalizeGuardedPath, resolveWorkspaceOrTmpPath, resolveIssueArtifactPath, validateUploadSourcePath, withGuardedDirectory, readGuardedFile, writeGuardedFile }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/codebase-design]
directoryModeapplies to every ancestor this call creates, not just the asset and Claim directories. On a first run, parents such asgh-aw/orsafeoutputs/aboveassetswould be created0755, which is broader than the private0700the spec requires for guard-created subdirectories. Apply the wider mode only to the leaf directories the caller needs, or document why the ancestors must be traversable.@copilot please address this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The staging caller now uses
0600/0700, keeping all newly created staging ancestors private. The separate publisher still uses its constrained publication pair outside the shared staging tree. Updated in900bf5051c.