Skip to content

Commit

Permalink
fix: avoid endless loop when handling symlinks #108
Browse files Browse the repository at this point in the history
  • Loading branch information
q0rban committed Oct 22, 2024
1 parent f9f474b commit 1fb8b85
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions lib/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,23 @@ function stepDetect(
}
}

function findCommonJunctionPoint(file: string, realFile: string) {
async function findCommonJunctionPoint(file: string, realFile: string) {
// find common denominator => where the link changes
while (toNormalizedRealPath(path.dirname(file)) === path.dirname(realFile)) {
while (true) {
const stats = await fs.lstat(file);

if (stats.isSymbolicLink()) {
return { file, realFile };
}

file = path.dirname(file);
realFile = path.dirname(realFile);
}

return { file, realFile };
// If the directory is /, break out of the loop and log an error.
if (file === path.parse(file).root || realFile === path.parse(realFile).root) {
throw new Error('Reached root directory without finding a common junction point');
}
}
}

export interface WalkerParams {
Expand Down Expand Up @@ -399,10 +408,15 @@ class Walker {
}
}

appendSymlink(file: string, realFile: string) {
const a = findCommonJunctionPoint(file, realFile);
file = a.file;
realFile = a.realFile;
async appendSymlink(file: string, realFile: string) {
try {
const a = await findCommonJunctionPoint(file, realFile);
file = a.file;
realFile = a.realFile;
} catch (error) {
log.error((error as Error).message);
return;
}

if (!this.symLinks[file]) {
const dn = path.dirname(file);
Expand Down Expand Up @@ -456,7 +470,7 @@ class Walker {
});
}

appendBlobOrContent(task: Task) {
async appendBlobOrContent(task: Task) {
if (strictVerify) {
assert(task.file === normalizePath(task.file));
}
Expand Down Expand Up @@ -486,7 +500,7 @@ class Walker {
}

this.append({ ...task, file: realFile });
this.appendSymlink(task.file, realFile);
await this.appendSymlink(task.file, realFile);
this.appendStat({
file: task.file,
store: STORE_STAT,
Expand Down Expand Up @@ -514,7 +528,7 @@ class Walker {
]);
}

this.appendBlobOrContent({
await this.appendBlobOrContent({
file: normalizePath(script),
marker,
store: STORE_BLOB,
Expand All @@ -534,7 +548,7 @@ class Walker {
const stat = await fs.stat(asset);

if (stat.isFile()) {
this.appendBlobOrContent({
await this.appendBlobOrContent({
file: normalizePath(asset),
marker,
store: STORE_CONTENT,
Expand All @@ -558,14 +572,14 @@ class Walker {
// 2) non-source (non-js) files of top-level package are shipped as CONTENT
// 3) parsing some js 'files' of non-top-level packages fails, hence all CONTENT
if (marker.toplevel) {
this.appendBlobOrContent({
await this.appendBlobOrContent({
file,
marker,
store: isDotJS(file) ? STORE_BLOB : STORE_CONTENT,
reason: configPath,
});
} else {
this.appendBlobOrContent({
await this.appendBlobOrContent({
file,
marker,
store: STORE_CONTENT,
Expand Down Expand Up @@ -754,7 +768,7 @@ class Walker {
}

if (stat && stat.isFile()) {
this.appendBlobOrContent({
await this.appendBlobOrContent({
file,
marker,
store: STORE_CONTENT,
Expand Down Expand Up @@ -857,15 +871,15 @@ class Walker {
normalizePath(newPackageForNewRecords.packageJson),
);
}
this.appendBlobOrContent({
await this.appendBlobOrContent({
file: newPackageForNewRecords.packageJson,
marker: newPackageForNewRecords.marker,
store: STORE_CONTENT,
reason: record.file,
});
}

this.appendBlobOrContent({
await this.appendBlobOrContent({
file: newFile,
marker: newPackageForNewRecords ? newPackageForNewRecords.marker : marker,
store: STORE_BLOB,
Expand Down Expand Up @@ -921,7 +935,7 @@ class Walker {

if (store === STORE_BLOB) {
if (unlikelyJavascript(record.file) || isDotNODE(record.file)) {
this.appendBlobOrContent({
await this.appendBlobOrContent({
file: record.file,
marker,
store: STORE_CONTENT,
Expand All @@ -930,7 +944,7 @@ class Walker {
}

if (marker.public || marker.hasDictionary) {
this.appendBlobOrContent({
await this.appendBlobOrContent({
file: record.file,
marker,
store: STORE_CONTENT,
Expand Down Expand Up @@ -1090,15 +1104,15 @@ class Walker {

entrypoint = normalizePath(entrypoint);

this.appendBlobOrContent({
await this.appendBlobOrContent({
file: entrypoint,
marker,
store: STORE_BLOB,
});

if (addition) {
addition = normalizePath(addition);
this.appendBlobOrContent({
await this.appendBlobOrContent({
file: addition,
marker,
store: STORE_CONTENT,
Expand Down

0 comments on commit 1fb8b85

Please sign in to comment.