Skip to content

Commit 313504f

Browse files
devversionjbedard
authored andcommitted
feat: add experimental js_binary(patch_node_esm_loader)
1 parent e758431 commit 313504f

38 files changed

+766
-121
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ js/private/coverage/coverage.js linguist-generated=true
66
js/private/devserver/js_run_devserver.mjs linguist-generated=true
77
js/private/watch/aspect_watch_protocol.mjs linguist-generated=true
88
js/private/watch/aspect_watch_protocol.d.mts linguist-generated=true
9-
js/private/node-patches/fs.cjs linguist-generated=true
9+
js/private/node-patches/fs*.cjs linguist-generated=true
1010
js/private/js_image_layer.mjs linguist-generated=true

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ examples/**/*-docs.md
66
js/private/coverage/coverage.js
77
js/private/devserver/js_run_devserver.mjs
88
js/private/node-patches/fs.cjs
9+
js/private/node-patches/fs_stat.cjs
910
js/private/watch/aspect_watch_protocol.mjs
1011
js/private/watch/aspect_watch_protocol.d.mts
1112
min/

docs/js_binary.md

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/js_run_binary.md

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/private/js_binary.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ _ATTRS = {
205205
which can lead to non-hermetic behavior.""",
206206
default = True,
207207
),
208+
"patch_node_esm_loader": attr.bool(
209+
doc = """Apply the internal lstat patch to prevent the program from following symlinks out of
210+
the execroot, runfiles and the sandbox even when using the ESM loader.
211+
212+
This flag only has an effect when `patch_node_fs` is True.""",
213+
default = False,
214+
),
208215
"include_sources": attr.bool(
209216
doc = """When True, `sources` from `JsInfo` providers in `data` targets are included in the runfiles of the target.""",
210217
default = True,
@@ -320,7 +327,10 @@ _ATTRS = {
320327
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
321328
"_node_patches_files": attr.label_list(
322329
allow_files = True,
323-
default = [Label("@aspect_rules_js//js/private/node-patches:fs.cjs")],
330+
default = [
331+
Label("@aspect_rules_js//js/private/node-patches:fs.cjs"),
332+
Label("@aspect_rules_js//js/private/node-patches:fs_stat.cjs"),
333+
],
324334
),
325335
"_node_patches": attr.label(
326336
allow_single_file = True,
@@ -391,6 +401,8 @@ def _bash_launcher(ctx, nodeinfo, entry_point_path, log_prefix_rule_set, log_pre
391401
if ctx.attr.patch_node_fs:
392402
# Set patch node fs API env if not already set to allow js_run_binary to override
393403
envs.append(_ENV_SET_IFF_NOT_SET.format(var = "JS_BINARY__PATCH_NODE_FS", value = "1"))
404+
if ctx.attr.patch_node_esm_loader:
405+
envs.append(_ENV_SET_IFF_NOT_SET.format(var = "JS_BINARY__PATCH_NODE_ESM_LOADER", value = "1"))
394406

395407
if ctx.attr.expected_exit_code:
396408
envs.append(_ENV_SET.format(

js/private/js_run_binary.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def js_run_binary(
4343
execution_requirements = None,
4444
stamp = 0,
4545
patch_node_fs = True,
46+
patch_node_esm_loader = False,
4647
allow_execroot_entry_point_with_no_copy_data_to_bin = False,
4748
use_default_shell_env = None,
4849
**kwargs):
@@ -224,6 +225,8 @@ def js_run_binary(
224225
When disabled, node programs can leave the execroot, runfiles and sandbox by following symlinks
225226
which can lead to non-hermetic behavior.
226227
228+
patch_node_esm_loader: additionally patch the Node.js ESM loader
229+
227230
allow_execroot_entry_point_with_no_copy_data_to_bin: Turn off validation that the `js_binary` tool
228231
has `copy_data_to_bin` set to True when `use_execroot_entry_point` is set to True.
229232
@@ -337,6 +340,11 @@ WARNING: js_library 'include_declarations' is deprecated. Use 'include_types' in
337340
# Disable node patches if requested
338341
if patch_node_fs:
339342
fixed_env["JS_BINARY__PATCH_NODE_FS"] = "1"
343+
344+
if patch_node_esm_loader:
345+
fixed_env["JS_BINARY__PATCH_NODE_ESM_LOADER"] = "1"
346+
else:
347+
fixed_env["JS_BINARY__PATCH_NODE_ESM_LOADER"] = "0"
340348
else:
341349
# Set explicitly to "0" so disable overrides any enable in the js_binary
342350
fixed_env["JS_BINARY__PATCH_NODE_FS"] = "0"

0 commit comments

Comments
 (0)