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: Enable getNode to follow directory links (fixes #248) #318

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

AndreasFranek
Copy link
Contributor

Fixes #248

As reported in the issue, soft links (or symlinks/symbolic links) can cause a TypeError to occur in asar.stat. This happens when a directory below the stat'ed path is actually a link.

This commit adds a test demonstrating the problem and fixes the problem by introducing a followLinks variable in getNode and resolving directory links, if it is set.

This is my first PR in this project so any feedback is welcome. I was not sure whether to implement the fix imperatively or recursively, for example. I.e., it is possible to follow the link like this instead:

  getNode (p, followLinks) {
    followLinks = typeof followLinks === 'undefined' ? true : followLinks
    var node = this.searchNodeFromDirectory(path.dirname(p))
    while (node.link && followLinks) {
      node = this.searchNodeFromDirectory(node.link)
    }
    const name = path.basename(p)
    if (name) {
      return node.files[name]
    } else {
      return node
    }
  }

@AndreasFranek AndreasFranek requested a review from a team as a code owner August 12, 2024 08:51
@AndreasFranek
Copy link
Contributor Author

Follow-up question, why is the followLinks parameter not defaulted to true?
I.e., why is it

  getFile (p, followLinks) {
    followLinks = typeof followLinks === 'undefined' ? true : followLinks

and not

  getFile (p, followLinks = true) {

?

@AndreasFranek AndreasFranek changed the title Enable getNode to follow directory links (fixes #248) fix: Enable getNode to follow directory links (fixes #248) Aug 12, 2024
@MarshallOfSound
Copy link
Member

Should probably land after #320

@AndreasFranek AndreasFranek force-pushed the 248-getNode-follow-links branch 2 times, most recently from d114c2a to 0b22b1d Compare September 24, 2024 15:57
@AndreasFranek
Copy link
Contributor Author

Rebased.

@AndreasFranek
Copy link
Contributor Author

Any further input on this?

test/api-spec.js Outdated
@@ -168,4 +168,8 @@ describe('api', function () {

assert.deepStrictEqual(topLevelFunctions, defaultExportFunctions);
});
it('should stat a symlinked file', async () => {
const stats = asar.statFile('test/input/stat-symlink.asar', 'real.txt', true);
return assert.strictEqual(stats.size, 19);
Copy link
Member

Choose a reason for hiding this comment

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

Can we assert that this is not a link instead of checking the size? I think it makes the intent clearer:

Suggested change
return assert.strictEqual(stats.size, 19);
return assert.strictEqual(stats.link, undefined);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Done.

When called from stat, getNode should follow directory links to
ensure that the correct node is returned. Otherwise, node.files[name]
will be undefined, causing a TypeError.
@AndreasFranek AndreasFranek force-pushed the 248-getNode-follow-links branch from 0b22b1d to a05f9b6 Compare November 5, 2024 13:36
@erikian erikian merged commit f30b93d into electron:main Nov 5, 2024
3 checks passed
Copy link

🎉 This PR is included in version 3.2.16 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

There is an error that seems to be caused by soft links
3 participants