Skip to content
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

fix: avoid endless loop when handling symlinks #108 #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Author

@q0rban q0rban Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this fail gracefully like this, or should we not handle the error here?

}

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