Skip to content

fix(link-bins): make sure user has access to change file permissions #9751

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

aghArdeshir
Copy link
Contributor

@aghArdeshir aghArdeshir commented Jul 13, 2025

Tries to fix #3699

It checks if the file its trying to fixBin on, is actually owned by the current user. Or current user is root. Because chmod is only allowed if current user owns the target file (or is root).

@aghArdeshir aghArdeshir requested a review from zkochan as a code owner July 13, 2025 08:08
@aghArdeshir aghArdeshir marked this pull request as draft July 13, 2025 08:08
Comment on lines +600 to +602
if (filePath !== binFilePath) {
throw new Error(`Unexpected file path: ${filePath.toString()}. You should handle this case.`)
}
Copy link
Contributor Author

@aghArdeshir aghArdeshir Jul 13, 2025

Choose a reason for hiding this comment

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

I thought this check and throw would be useful, if in future a new functionality added to the link-bins causing stat check to be called, then this test would most probably fail, so better if it fails with a more descriptive error. I don't know, maybe its a YAGNI 🤷 Let me know what you think

@aghArdeshir aghArdeshir marked this pull request as ready for review July 13, 2025 09:17
@aghArdeshir aghArdeshir marked this pull request as draft July 13, 2025 09:19
@aghArdeshir aghArdeshir force-pushed the issue-3699-operation-not-permitted-chmod branch from 1bab584 to 9e47b01 Compare July 15, 2025 20:15
Because it makes more sense, and its not only about the user
@aghArdeshir aghArdeshir changed the title make sure file is owned by current user before trying to change permissions fix(link-bins): make sure file permissions can be changed before changing them Jul 16, 2025
// TODO: Does this happen on windows?
if (IS_WINDOWS) return true

const userId = process.getuid?.()
Copy link
Contributor Author

@aghArdeshir aghArdeshir Jul 16, 2025

Choose a reason for hiding this comment

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

process.getUid is added in node: v0.1.28, but still TypeScript thinks it may be underfined. So ?.

}
}

async function canFixBin (filePath: string): Promise<boolean> {
if (IS_WINDOWS) return true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Skips windows, as I don't know if this problem happens on Windows machines too or not?

@aghArdeshir aghArdeshir changed the title fix(link-bins): make sure file permissions can be changed before changing them fix(link-bins): make sure user has access to change file permissions Jul 16, 2025
@aghArdeshir aghArdeshir marked this pull request as ready for review July 16, 2025 18:34
Comment on lines +254 to 256
if (await canFixBin(cmd.path)) await fixBin(cmd.path, 0o755)
else globalWarn(`Skipped fixing bin permissions of \`${cmd.path}\` because the file is not owned by the current user`)
} catch (err: any) { // eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

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

What about an optimistic approach? If it fails, then check canFixBin. Also, can't we just check the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi. Thanks for the reply.

I didn't know what the error could be, and I assumed any error could be different on different OSes. So I went the most deterministic approach for only one particular error: when user does not have permission to change file permissions.

Copy link
Member

Choose a reason for hiding this comment

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

Still, there's no need in running canFixBin unless fixBin throws an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aaaaa I see. That's true.
For that though, I'll need to change/remove my test: "linkBins() should not try to change permissions of files not owned by current user"

I'll look into it. Thanks

@aghArdeshir aghArdeshir marked this pull request as draft August 12, 2025 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EPERM: operation not permitted, chmod
2 participants