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

feat: add execroot & runfiles symlink guards to js_binary #133

Merged
merged 1 commit into from
Jun 14, 2022

Conversation

gregmagolan
Copy link
Member

@gregmagolan gregmagolan commented May 27, 2022

Fixes #114

The fs patches here have been modified from their original implementation in rules_nodejs https://github.com/bazelbuild/rules_nodejs/blob/173a0252f273de48a6a237518e9cb0d9854cb8e6/packages/node-patches/src/fs.ts.

The have been improved to allow following symlinks within the sandbox, which is required for finding deps in a symlinks node_modules structure. Since the sandbox (on macos) is a tree of symlinks that all point back to their counterparts outside of the sandbox, the old implementation prevented following symlinks entirely (akin to passing `--preserve-symlinks) since every follow was an escape from the guarded sandbox.

In this implementation, if the readlink escape is a symlink to a symlink then we readlink the escaped symlink. For lstat we do the same. For realpath, if we the relative path from the link to the realpath can be remapped into the escaped root then it is.

In rules_js, the guards are setup to protect the execroot (which protects the sandbox when there is one and protects from escaping into the source tree when there isn't) and to protect the runfiles tree for the js_binary in the build, test or run target since that is a symlink tree as well. This is simpler than what was done in rules_nodejs which was to protect all of the dynamically linked node_modules trees.

@gregmagolan gregmagolan force-pushed the symlink_guards branch 11 times, most recently from 7b7c6c8 to 4a76052 Compare June 1, 2022 17:15
@gregmagolan gregmagolan closed this Jun 2, 2022
@gregmagolan gregmagolan reopened this Jun 2, 2022
@gregmagolan gregmagolan force-pushed the symlink_guards branch 4 times, most recently from 3d9e566 to 89eb420 Compare June 3, 2022 22:01
@gregmagolan gregmagolan force-pushed the symlink_guards branch 7 times, most recently from dd68379 to c7d9971 Compare June 13, 2022 23:25
@gregmagolan gregmagolan changed the title [WIP] feat: add sandbox symlink guards to js_binary feat: add sandbox symlink guards to js_binary Jun 13, 2022
@gregmagolan gregmagolan marked this pull request as ready for review June 13, 2022 23:30
@gregmagolan gregmagolan changed the title feat: add sandbox symlink guards to js_binary feat: add sandbox & runfiles symlink guards to js_binary Jun 13, 2022
@gregmagolan gregmagolan requested a review from alexeagle June 13, 2022 23:33
Copy link
Member

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

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

let's add the tests too, otherwise it's too tempting to leave it for later...

js/private/node-patches/src/fs.ts Outdated Show resolved Hide resolved
@gregmagolan gregmagolan force-pushed the symlink_guards branch 3 times, most recently from 0faad7c to 2b133a2 Compare June 13, 2022 23:56
@gregmagolan gregmagolan changed the title feat: add sandbox & runfiles symlink guards to js_binary feat: add execroot & runfiles symlink guards to js_binary Jun 13, 2022
@gregmagolan gregmagolan force-pushed the symlink_guards branch 2 times, most recently from 19bb590 to 2ec930e Compare June 14, 2022 00:55
@gregmagolan
Copy link
Member Author

let's add the tests too, otherwise it's too tempting to leave it for later...

Done. Brought unit tests in from rules_nodejs for the patches

@@ -15,7 +15,7 @@ console.log(c.id())
const fp = require('@e2e/lib')
console.log('--@e2e/lib--')
console.log(fp.id())
const rulesFooA = require('../external/rules_foo/foo/node_modules/@aspect-test/a')
const rulesFooA = require('../../rules_foo/foo/node_modules/@aspect-test/a')
Copy link
Member

Choose a reason for hiding this comment

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

Interesting change needed

Copy link
Member Author

Choose a reason for hiding this comment

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

yea, the path here is runfiles relative for this test. no legacy external runfiles :)

@gregmagolan gregmagolan merged commit 81142af into aspect-build:main Jun 14, 2022
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.

js rules should be hermetic and not follow symlinks out of the sandbox
2 participants