-
Notifications
You must be signed in to change notification settings - Fork 522
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(builtin): don't allow symlinks to escape or enter bazel managed node_module folders #1800
Merged
gregmagolan
merged 1 commit into
bazel-contrib:master
from
gregmagolan:node_symlinks_fixes
Apr 10, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const fs = require('fs'); | ||
const args = process.argv.slice(2); | ||
fs.writeFileSync(args.shift(), JSON.stringify(process.env, null, 2), 'utf-8'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); | ||
const isWindows = process.platform === 'win32'; | ||
const runfilesExt = isWindows ? 'bat' : 'sh'; | ||
|
||
function normPath(p) { | ||
let result = p.replace(/\\/g, '/'); | ||
if (isWindows) { | ||
// On Windows, we normalize to lowercase for so path mismatches such as 'C:/Users' and | ||
// 'c:/users' don't break the specs. | ||
result = result.toLowerCase(); | ||
if (/[a-zA-Z]\:/.test(result)) { | ||
// Handle c:/ and /c/ mismatch | ||
result = `/${result[0]}${result.slice(2)}`; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
function expectPathsToMatch(a, b) { | ||
if (Array.isArray(a) && Array.isArray(b)) { | ||
a = a.map(p => normPath(p)); | ||
b = b.map(p => normPath(p)); | ||
expect(a).toEqual(b); | ||
} else { | ||
expect(normPath(a)).toBe(normPath(b)); | ||
} | ||
} | ||
|
||
describe('launcher.sh environment', function() { | ||
it('should setup correct bazel environment variables when in runfiles', function() { | ||
const runfilesRoot = normPath(process.env['RUNFILES']); | ||
const match = runfilesRoot.match(/\/bazel-out\//); | ||
expect(!!match).toBe(true); | ||
const execroot = runfilesRoot.slice(0, match.index); | ||
expectPathsToMatch(path.basename(runfilesRoot), `env_test.${runfilesExt}.runfiles`); | ||
expectPathsToMatch(process.env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs'); | ||
expectPathsToMatch(process.env['BAZEL_TARGET'], '//internal/node/test:env_test'); | ||
expectPathsToMatch(process.cwd(), `${process.env['RUNFILES']}/build_bazel_rules_nodejs`); | ||
expectPathsToMatch(process.env['PWD'], `${process.env['RUNFILES']}/build_bazel_rules_nodejs`); | ||
expectPathsToMatch(process.env['BAZEL_PATCH_ROOT'], process.env['RUNFILES']); | ||
expectPathsToMatch(process.env['BAZEL_NODE_MODULES_ROOT'], 'npm/node_modules'); | ||
const expectedGuards = [ | ||
`${execroot}/node_modules`, | ||
`${runfilesRoot}/npm/node_modules`, | ||
`${runfilesRoot}/build_bazel_rules_nodejs/external/npm/node_modules`, | ||
] | ||
expectPathsToMatch(process.env['BAZEL_PATCH_GUARDS'].split(','), expectedGuards); | ||
}); | ||
|
||
it('should setup correct bazel environment variables when in execroot with no third party deps', | ||
function() { | ||
const env = require(runfiles.resolvePackageRelative('dump_build_env.json')); | ||
// On Windows, the RUNFILES path ends in a /MANIFEST segment in this context | ||
const runfilesRoot = normPath(isWindows ? path.dirname(env['RUNFILES']) : env['RUNFILES']); | ||
const match = runfilesRoot.match(/\/bazel-out\//); | ||
expect(!!match).toBe(true); | ||
const execroot = runfilesRoot.slice(0, match.index); | ||
expectPathsToMatch(path.basename(runfilesRoot), `dump_build_env.${runfilesExt}.runfiles`); | ||
expectPathsToMatch(env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs'); | ||
expectPathsToMatch(env['BAZEL_TARGET'], '//internal/node/test:dump_build_env'); | ||
expectPathsToMatch(env['PWD'], execroot); | ||
expectPathsToMatch(env['BAZEL_PATCH_ROOT'], path.dirname(execroot)); | ||
expectPathsToMatch(env['BAZEL_NODE_MODULES_ROOT'], 'build_bazel_rules_nodejs/node_modules'); | ||
const expectedGuards = [ | ||
`${execroot}/node_modules`, | ||
] | ||
expectPathsToMatch(env['BAZEL_PATCH_GUARDS'].split(','), expectedGuards); | ||
}); | ||
|
||
it('should setup correct bazel environment variables when in execroot with third party deps', | ||
function() { | ||
const env = require(runfiles.resolvePackageRelative('dump_build_env_alt.json')); | ||
// On Windows, the RUNFILES path ends in a /MANIFEST segment in this context | ||
const runfilesRoot = normPath(isWindows ? path.dirname(env['RUNFILES']) : env['RUNFILES']); | ||
const match = runfilesRoot.match(/\/bazel-out\//); | ||
expect(!!match).toBe(true); | ||
const execroot = runfilesRoot.slice(0, match.index); | ||
expectPathsToMatch( | ||
path.basename(runfilesRoot), `dump_build_env_alt.${runfilesExt}.runfiles`); | ||
expectPathsToMatch(env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs'); | ||
expectPathsToMatch(env['BAZEL_TARGET'], '//internal/node/test:dump_build_env_alt'); | ||
expectPathsToMatch(env['PWD'], execroot); | ||
expectPathsToMatch(env['BAZEL_PATCH_ROOT'], path.dirname(execroot)); | ||
expectPathsToMatch(env['BAZEL_NODE_MODULES_ROOT'], 'npm/node_modules'); | ||
const expectedGuards = [ | ||
`${execroot}/node_modules`, | ||
`${path.dirname(execroot)}/npm/node_modules`, | ||
`${execroot}/external/npm/node_modules`, | ||
] | ||
expectPathsToMatch(env['BAZEL_PATCH_GUARDS'].split(','), expectedGuards); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like this doesn't work if your node_modules is in a subdirectory, which we support - but I think that's what BAZEL_NODE_MODULES_ROOT is doing below? maybe this needs a comment that we do this in a catch-all case where we don't have a BAZEL_NODE_MODULES_ROOT - but then why does that happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BAZEL_NODE_MODULES_ROOT is the path to the external workspace such as
npm/node_modules
.This path is actually the path to the linker symlink node_modules under execroot which should work regardless of where node_modules is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BAZEL_NODE_MODULES_ROOT is always set in templated_args above:
so below is just a sanity check