-
-
Notifications
You must be signed in to change notification settings - Fork 24
Add opt-in owner permission normalization to @tryghost/zip extract #761
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
Merged
+366
−11
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eaef85d
Added opt-in owner permission normalization to @tryghost/zip extract
sagzy ca53c17
Honor owner-less defaults for zero-mode entries in ensureOwnerPermiss…
sagzy 95bac4e
Mark optional extract() options as optional in JSDoc
sagzy e83aaff
Extract owner-permission logic into its own module for direct unit te…
sagzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| const { S_IFMT, S_IFDIR, getEntryMode } = require('./entry-mode'); | ||
|
|
||
| // Owner permission bits we guarantee when `ensureOwnerPermissions` is enabled. | ||
| const OWNER_DIR_BITS = 0o700; // rwx, so the tree can be traversed, written and removed | ||
| const OWNER_FILE_BITS = 0o600; // rw, so files can be read and rewritten/removed | ||
|
|
||
| // Mirrors extract-zip's directory detection: POSIX directory type bit, a | ||
| // trailing slash, or the Windows directory attribute. | ||
| function isDirectoryEntry(entry, mode) { | ||
| if ((mode & S_IFMT) === S_IFDIR) { | ||
| return true; | ||
| } | ||
|
|
||
| if (entry.fileName.endsWith('/')) { | ||
| return true; | ||
| } | ||
|
|
||
| // Windows' way of specifying a directory | ||
| // https://github.com/maxogden/extract-zip/issues/13#issuecomment-154494566 | ||
| const madeBy = entry.versionMadeBy >> 8; | ||
| return madeBy === 0 && entry.externalFileAttributes === 16; | ||
| } | ||
|
|
||
| // Resolves the mode extract-zip would synthesise for an entry that carries no | ||
| // POSIX mode bits, mirroring its getExtractedMode() fallback: the caller's | ||
| // defaultDirMode/defaultFileMode, or 0o755/0o644 when those are not set. | ||
| function resolveDefaultMode(isDir, opts) { | ||
| let mode = 0; | ||
|
|
||
| if (isDir) { | ||
| if (opts.defaultDirMode) { | ||
| mode = parseInt(opts.defaultDirMode, 10); | ||
| } | ||
|
|
||
| if (!mode) { | ||
| mode = 0o755; | ||
| } | ||
| } else { | ||
| if (opts.defaultFileMode) { | ||
| mode = parseInt(opts.defaultFileMode, 10); | ||
| } | ||
|
|
||
| if (!mode) { | ||
| mode = 0o644; | ||
| } | ||
| } | ||
|
|
||
| return mode; | ||
| } | ||
|
|
||
| /** | ||
| * Normalises a zip entry's permissions in place so the extracting user can | ||
| * always read, move and remove the result: directories gain at least owner rwx | ||
| * and files at least owner rw, while existing execute/group/world bits are | ||
| * preserved. Only the in-memory entry handed to extract-zip is changed; the | ||
| * original zip file is never rewritten. | ||
| * | ||
| * @param {Object} entry - the yauzl entry extract-zip is about to extract | ||
| * @param {Object} [opts] - the extract options (read for defaultDirMode/defaultFileMode) | ||
| */ | ||
| function ensureOwnerPermissions(entry, opts = {}) { | ||
| let mode = getEntryMode(entry); | ||
| const isDir = isDirectoryEntry(entry, mode); | ||
|
|
||
| // No POSIX mode bits are present, so extract-zip would synthesise the mode | ||
| // from defaultDirMode/defaultFileMode (falling back to 0o755/0o644). Resolve | ||
| // that same default here so the owner bits below are guaranteed even when the | ||
| // caller-supplied defaults omit them. | ||
| if (mode === 0) { | ||
| mode = resolveDefaultMode(isDir, opts); | ||
| } | ||
|
|
||
| const ownerBits = isDir ? OWNER_DIR_BITS : OWNER_FILE_BITS; | ||
| let nextMode = mode | ownerBits; | ||
|
|
||
| // A directory that was only inferred (trailing slash / Windows attribute) | ||
| // may lack the POSIX directory type bit. Set it so extract-zip still | ||
| // classifies the rewritten mode as a directory. | ||
| if (isDir && (nextMode & S_IFMT) !== S_IFDIR) { | ||
| nextMode = (nextMode & ~S_IFMT) | S_IFDIR; | ||
| } | ||
|
|
||
| // The mode extract-zip would already produce grants the owner the required | ||
| // access and needs no type-bit fix, so leave the entry untouched. | ||
| if (nextMode === mode) { | ||
| return; | ||
| } | ||
|
|
||
| // Write the new POSIX mode back into the high 16 bits while preserving the | ||
| // low 16 bits (DOS attributes and other zip-specific flags). | ||
| const lowBits = entry.externalFileAttributes & 0xffff; | ||
| entry.externalFileAttributes = (((nextMode & 0xffff) << 16) | lowBits) >>> 0; | ||
| } | ||
|
|
||
| module.exports = ensureOwnerPermissions; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // POSIX `stat` mode constants, matching the ones extract-zip uses internally | ||
| // when it derives an entry's type and permissions from its external attributes. | ||
| const S_IFMT = 0o170000; // bit mask for the file type bit fields | ||
| const S_IFDIR = 0o040000; // directory | ||
| const S_IFLNK = 0o120000; // symbolic link | ||
|
|
||
| // Reads the POSIX mode an entry carries in the high 16 bits of its external | ||
| // file attributes, exactly as extract-zip does when extracting. | ||
| function getEntryMode(entry) { | ||
| return (entry.externalFileAttributes >> 16) & 0xffff; | ||
| } | ||
|
|
||
| module.exports = { | ||
| S_IFMT, | ||
| S_IFDIR, | ||
| S_IFLNK, | ||
| getEntryMode, | ||
| }; |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.