formacao
This commit is contained in:
+38
-63
@@ -128,8 +128,7 @@ export class Unpack extends Parser {
|
||||
this.chmod = !!opt.chmod;
|
||||
if (typeof opt.uid === 'number' || typeof opt.gid === 'number') {
|
||||
// need both or neither
|
||||
if (typeof opt.uid !== 'number' ||
|
||||
typeof opt.gid !== 'number') {
|
||||
if (typeof opt.uid !== 'number' || typeof opt.gid !== 'number') {
|
||||
throw new TypeError('cannot set owner without number uid and gid');
|
||||
}
|
||||
if (opt.preserveOwner) {
|
||||
@@ -145,13 +144,10 @@ export class Unpack extends Parser {
|
||||
this.setOwner = false;
|
||||
}
|
||||
// default true for root
|
||||
if (opt.preserveOwner === undefined &&
|
||||
typeof opt.uid !== 'number') {
|
||||
this.preserveOwner = !!(process.getuid && process.getuid() === 0);
|
||||
}
|
||||
else {
|
||||
this.preserveOwner = !!opt.preserveOwner;
|
||||
}
|
||||
this.preserveOwner =
|
||||
opt.preserveOwner === undefined && typeof opt.uid !== 'number' ?
|
||||
!!(process.getuid && process.getuid() === 0)
|
||||
: !!opt.preserveOwner;
|
||||
this.processUid =
|
||||
(this.preserveOwner || this.setOwner) && process.getuid ?
|
||||
process.getuid()
|
||||
@@ -163,9 +159,7 @@ export class Unpack extends Parser {
|
||||
// prevent excessively deep nesting of subfolders
|
||||
// set to `Infinity` to remove this restriction
|
||||
this.maxDepth =
|
||||
typeof opt.maxDepth === 'number' ?
|
||||
opt.maxDepth
|
||||
: DEFAULT_MAX_DEPTH;
|
||||
typeof opt.maxDepth === 'number' ? opt.maxDepth : DEFAULT_MAX_DEPTH;
|
||||
// mostly just for testing, but useful in some cases.
|
||||
// Forcibly trigger a chown on every entry, no matter what
|
||||
this.forceChown = opt.forceChown === true;
|
||||
@@ -223,7 +217,7 @@ export class Unpack extends Parser {
|
||||
return true;
|
||||
// strip off the root
|
||||
const [root, stripped] = stripAbsolutePath(p);
|
||||
const parts = stripped.replace(/\\/g, '/').split('/');
|
||||
const parts = stripped.replaceAll(/\\/g, '/').split('/');
|
||||
if (parts.includes('..') ||
|
||||
/* c8 ignore next */
|
||||
(isWindows && /^[a-z]:\.\.$/i.test(parts[0] ?? ''))) {
|
||||
@@ -239,20 +233,18 @@ export class Unpack extends Parser {
|
||||
// not ok!
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
// Resolve linkpath relative to the entry's directory.
|
||||
// `path.posix` is safe to use because we're operating on
|
||||
// tar paths, not a filesystem.
|
||||
const entryDir = path.posix.dirname(entry.path);
|
||||
const resolved = path.posix.normalize(path.posix.join(entryDir, p));
|
||||
// If the resolved path escapes (starts with ..), reject it
|
||||
if (resolved.startsWith('../') || resolved === '..') {
|
||||
this.warn('TAR_ENTRY_ERROR', `${field} escapes extraction directory`, {
|
||||
entry,
|
||||
[field]: p,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
// Resolve linkpath relative to the entry's directory.
|
||||
// `path.posix` is safe to use because we're operating on
|
||||
// tar paths, not a filesystem.
|
||||
const entryDir = path.posix.dirname(entry.path);
|
||||
const resolved = path.posix.normalize(path.posix.join(entryDir, parts.join('/')));
|
||||
// If the resolved path escapes (starts with ..), reject it
|
||||
if (resolved.startsWith('../') || resolved === '..') {
|
||||
this.warn('TAR_ENTRY_ERROR', `${field} escapes extraction directory`, {
|
||||
entry,
|
||||
[field]: p,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (root) {
|
||||
@@ -298,12 +290,10 @@ export class Unpack extends Parser {
|
||||
!this[STRIPABSOLUTEPATH](entry, 'linkpath')) {
|
||||
return false;
|
||||
}
|
||||
if (path.isAbsolute(entry.path)) {
|
||||
entry.absolute = normalizeWindowsPath(path.resolve(entry.path));
|
||||
}
|
||||
else {
|
||||
entry.absolute = normalizeWindowsPath(path.resolve(this.cwd, entry.path));
|
||||
}
|
||||
entry.absolute =
|
||||
path.isAbsolute(entry.path) ?
|
||||
normalizeWindowsPath(path.resolve(entry.path))
|
||||
: normalizeWindowsPath(path.resolve(this.cwd, entry.path));
|
||||
// if we somehow ended up with a path that escapes the cwd, and we are
|
||||
// not in preservePaths mode, then something is fishy! This should have
|
||||
// been prevented above, so ignore this for coverage.
|
||||
@@ -398,8 +388,7 @@ export class Unpack extends Parser {
|
||||
entry.uid !== this.processUid) ||
|
||||
(typeof entry.gid === 'number' &&
|
||||
entry.gid !== this.processGid))) ||
|
||||
(typeof this.uid === 'number' &&
|
||||
this.uid !== this.processUid) ||
|
||||
(typeof this.uid === 'number' && this.uid !== this.processUid) ||
|
||||
(typeof this.gid === 'number' && this.gid !== this.processGid));
|
||||
}
|
||||
[UID](entry) {
|
||||
@@ -409,9 +398,7 @@ export class Unpack extends Parser {
|
||||
return uint32(this.gid, entry.gid, this.processGid);
|
||||
}
|
||||
[FILE](entry, fullyDone) {
|
||||
const mode = typeof entry.mode === 'number' ?
|
||||
entry.mode & 0o7777
|
||||
: this.fmode;
|
||||
const mode = typeof entry.mode === 'number' ? entry.mode & 0o7777 : this.fmode;
|
||||
const stream = new fsm.WriteStream(String(entry.absolute), {
|
||||
// slight lie, but it can be numeric flags
|
||||
flags: getWriteFlag(entry.size),
|
||||
@@ -474,16 +461,14 @@ export class Unpack extends Parser {
|
||||
const uid = this[UID](entry);
|
||||
const gid = this[GID](entry);
|
||||
if (typeof uid === 'number' && typeof gid === 'number') {
|
||||
fs.fchown(fd, uid, gid, er => er ?
|
||||
fs.chown(abs, uid, gid, er2 => done(er2 && er))
|
||||
: done());
|
||||
fs.fchown(fd, uid, gid, er => er ? fs.chown(abs, uid, gid, er2 => done(er2 && er)) : done());
|
||||
}
|
||||
}
|
||||
done();
|
||||
});
|
||||
const tx = this.transform ? this.transform(entry) || entry : entry;
|
||||
if (tx !== entry) {
|
||||
tx.on('error', (er) => {
|
||||
tx.on('error', er => {
|
||||
this[ONERROR](er, entry);
|
||||
fullyDone();
|
||||
});
|
||||
@@ -492,9 +477,7 @@ export class Unpack extends Parser {
|
||||
tx.pipe(stream);
|
||||
}
|
||||
[DIRECTORY](entry, fullyDone) {
|
||||
const mode = typeof entry.mode === 'number' ?
|
||||
entry.mode & 0o7777
|
||||
: this.dmode;
|
||||
const mode = typeof entry.mode === 'number' ? entry.mode & 0o7777 : this.dmode;
|
||||
this[MKDIR](String(entry.absolute), mode, er => {
|
||||
if (er) {
|
||||
this[ONERROR](er, entry);
|
||||
@@ -630,9 +613,7 @@ export class Unpack extends Parser {
|
||||
}
|
||||
if (st.isDirectory()) {
|
||||
if (entry.type === 'Directory') {
|
||||
const needChmod = this.chmod &&
|
||||
entry.mode &&
|
||||
(st.mode & 0o7777) !== entry.mode;
|
||||
const needChmod = this.chmod && entry.mode && (st.mode & 0o7777) !== entry.mode;
|
||||
const afterChmod = (er) => this[MAKEFS](er ?? null, entry, done);
|
||||
if (!needChmod) {
|
||||
return afterChmod();
|
||||
@@ -742,9 +723,7 @@ export class UnpackSync extends Unpack {
|
||||
}
|
||||
if (st.isDirectory()) {
|
||||
if (entry.type === 'Directory') {
|
||||
const needChmod = this.chmod &&
|
||||
entry.mode &&
|
||||
(st.mode & 0o7777) !== entry.mode;
|
||||
const needChmod = this.chmod && entry.mode && (st.mode & 0o7777) !== entry.mode;
|
||||
const [er] = needChmod ?
|
||||
callSync(() => {
|
||||
fs.chmodSync(String(entry.absolute), Number(entry.mode));
|
||||
@@ -764,9 +743,7 @@ export class UnpackSync extends Unpack {
|
||||
this[MAKEFS](er, entry);
|
||||
}
|
||||
[FILE](entry, done) {
|
||||
const mode = typeof entry.mode === 'number' ?
|
||||
entry.mode & 0o7777
|
||||
: this.fmode;
|
||||
const mode = typeof entry.mode === 'number' ? entry.mode & 0o7777 : this.fmode;
|
||||
const oner = (er) => {
|
||||
let closeError;
|
||||
try {
|
||||
@@ -794,7 +771,7 @@ export class UnpackSync extends Unpack {
|
||||
/* c8 ignore stop */
|
||||
const tx = this.transform ? this.transform(entry) || entry : entry;
|
||||
if (tx !== entry) {
|
||||
tx.on('error', (er) => this[ONERROR](er, entry));
|
||||
tx.on('error', er => this[ONERROR](er, entry));
|
||||
entry.pipe(tx);
|
||||
}
|
||||
tx.on('data', (chunk) => {
|
||||
@@ -819,7 +796,7 @@ export class UnpackSync extends Unpack {
|
||||
try {
|
||||
fs.utimesSync(String(entry.absolute), atime, mtime);
|
||||
}
|
||||
catch (utimeser) {
|
||||
catch {
|
||||
er = futimeser;
|
||||
}
|
||||
}
|
||||
@@ -834,7 +811,7 @@ export class UnpackSync extends Unpack {
|
||||
try {
|
||||
fs.chownSync(String(entry.absolute), Number(uid), Number(gid));
|
||||
}
|
||||
catch (chowner) {
|
||||
catch {
|
||||
er = er || fchowner;
|
||||
}
|
||||
}
|
||||
@@ -843,9 +820,7 @@ export class UnpackSync extends Unpack {
|
||||
});
|
||||
}
|
||||
[DIRECTORY](entry, done) {
|
||||
const mode = typeof entry.mode === 'number' ?
|
||||
entry.mode & 0o7777
|
||||
: this.dmode;
|
||||
const mode = typeof entry.mode === 'number' ? entry.mode & 0o7777 : this.dmode;
|
||||
const er = this[MKDIR](String(entry.absolute), mode);
|
||||
if (er) {
|
||||
this[ONERROR](er, entry);
|
||||
@@ -857,13 +832,13 @@ export class UnpackSync extends Unpack {
|
||||
fs.utimesSync(String(entry.absolute), entry.atime || new Date(), entry.mtime);
|
||||
/* c8 ignore next */
|
||||
}
|
||||
catch (er) { }
|
||||
catch { }
|
||||
}
|
||||
if (this[DOCHOWN](entry)) {
|
||||
try {
|
||||
fs.chownSync(String(entry.absolute), Number(this[UID](entry)), Number(this[GID](entry)));
|
||||
}
|
||||
catch (er) { }
|
||||
catch { }
|
||||
}
|
||||
done();
|
||||
entry.resume();
|
||||
@@ -887,7 +862,7 @@ export class UnpackSync extends Unpack {
|
||||
}
|
||||
}
|
||||
[ENSURE_NO_SYMLINK](_entry, cwd, parts, done, onError) {
|
||||
if (this.preservePaths || !parts.length)
|
||||
if (this.preservePaths || parts.length === 0)
|
||||
return done();
|
||||
let t = cwd;
|
||||
for (const p of parts) {
|
||||
|
||||
Reference in New Issue
Block a user