Skip to content

Commit a4ec7df

Browse files
authored
feat(unstable): --unstable-unsafe-proto (#21313)
Closes #21276
1 parent 00e4c47 commit a4ec7df

File tree

10 files changed

+55
-45
lines changed

10 files changed

+55
-45
lines changed

cli/args/flags.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -844,45 +844,11 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::error::Result<Flags> {
844844
if matches.get_flag("unstable") {
845845
flags.unstable = true;
846846
}
847-
if matches.get_flag("unstable-broadcast-channel") {
848-
flags.unstable_features.push(
849-
deno_runtime::deno_broadcast_channel::UNSTABLE_FEATURE_NAME.to_string(),
850-
);
851-
}
852-
if matches.get_flag("unstable-ffi") {
853-
flags
854-
.unstable_features
855-
.push(deno_runtime::deno_ffi::UNSTABLE_FEATURE_NAME.to_string());
856-
}
857-
if matches.get_flag("unstable-fs") {
858-
flags
859-
.unstable_features
860-
.push(deno_runtime::deno_fs::UNSTABLE_FEATURE_NAME.to_string());
861-
}
862-
if matches.get_flag("unstable-http") {
863-
flags
864-
.unstable_features
865-
.push(deno_runtime::ops::http::UNSTABLE_FEATURE_NAME.to_string());
866-
}
867-
if matches.get_flag("unstable-kv") {
868-
flags
869-
.unstable_features
870-
.push(deno_runtime::deno_kv::UNSTABLE_FEATURE_NAME.to_string());
871-
}
872-
if matches.get_flag("unstable-net") {
873-
flags
874-
.unstable_features
875-
.push(deno_runtime::deno_net::UNSTABLE_FEATURE_NAME.to_string());
876-
}
877-
if matches.get_flag("unstable-worker-options") {
878-
flags
879-
.unstable_features
880-
.push(deno_runtime::ops::worker_host::UNSTABLE_FEATURE_NAME.to_string());
881-
}
882-
if matches.get_flag("unstable-cron") {
883-
flags
884-
.unstable_features
885-
.push(deno_runtime::deno_cron::UNSTABLE_FEATURE_NAME.to_string());
847+
848+
for (name, _, _) in crate::UNSTABLE_GRANULAR_FLAGS {
849+
if matches.get_flag(&format!("unstable-{}", name)) {
850+
flags.unstable_features.push(name.to_string());
851+
}
886852
}
887853

888854
flags.unstable_bare_node_builtins =

cli/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ pub(crate) static UNSTABLE_GRANULAR_FLAGS: &[(
310310
"Enable unstable Deno.cron API",
311311
8,
312312
),
313+
(
314+
"unsafe-proto",
315+
"Enable unsafe __proto__ support. This is a security risk.",
316+
9,
317+
),
313318
];
314319

315320
pub(crate) fn unstable_exit_cb(_feature: &str, api_name: &str) {

cli/tests/integration/run_tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4716,3 +4716,17 @@ itest!(workspaces_nested_member {
47164716
http_server: true,
47174717
exit_code: 1,
47184718
});
4719+
4720+
itest!(unsafe_proto {
4721+
args: "run -A run/unsafe_proto/main.js",
4722+
output: "run/unsafe_proto/main.out",
4723+
http_server: false,
4724+
exit_code: 0,
4725+
});
4726+
4727+
itest!(unsafe_proto_flag {
4728+
args: "run -A --unstable-unsafe-proto run/unsafe_proto/main.js",
4729+
output: "run/unsafe_proto/main_with_unsafe_proto_flag.out",
4730+
http_server: false,
4731+
exit_code: 0,
4732+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
console.log(Object.hasOwn(Object.prototype, "__proto__"));
2+
3+
new Worker(import.meta.resolve("./worker.js"), {
4+
type: "module",
5+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
false
2+
false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
true
2+
true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log(Object.hasOwn(Object.prototype, "__proto__"));
2+
close();

cli/worker.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ impl CliMainWorkerFactory {
570570
// TODO(bartlomieju): this is cruft, update FeatureChecker to spit out
571571
// list of enabled features.
572572
let feature_checker = shared.feature_checker.clone();
573-
let mut unstable_features = Vec::with_capacity(8);
573+
let mut unstable_features =
574+
Vec::with_capacity(crate::UNSTABLE_GRANULAR_FLAGS.len());
574575
for (feature_name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS {
575576
if feature_checker.check(feature_name) {
576577
unstable_features.push(*id);
@@ -768,7 +769,8 @@ fn create_web_worker_callback(
768769
// TODO(bartlomieju): this is cruft, update FeatureChecker to spit out
769770
// list of enabled features.
770771
let feature_checker = shared.feature_checker.clone();
771-
let mut unstable_features = Vec::with_capacity(8);
772+
let mut unstable_features =
773+
Vec::with_capacity(crate::UNSTABLE_GRANULAR_FLAGS.len());
772774
for (feature_name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS {
773775
if feature_checker.check(feature_name) {
774776
unstable_features.push(*id);

runtime/js/90_deno_ns.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ const denoNsUnstableById = {
208208
8: {
209209
cron: cron.cron,
210210
},
211+
// Unsafe proto
212+
// 9: {},
211213
};
212214

213215
// when editing this list, also update unstableDenoProps in cli/tsc/99_main_compiler.js

runtime/js/99_main.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22

3-
// Removes the `__proto__` for security reasons.
4-
// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
5-
delete Object.prototype.__proto__;
6-
73
// Remove Intl.v8BreakIterator because it is a non-standard API.
84
delete Intl.v8BreakIterator;
95

@@ -14,6 +10,7 @@ const primordials = globalThis.__bootstrap.primordials;
1410
const {
1511
ArrayPrototypeFilter,
1612
ArrayPrototypeIndexOf,
13+
ArrayPrototypeIncludes,
1714
ArrayPrototypeMap,
1815
ArrayPrototypePush,
1916
ArrayPrototypeShift,
@@ -570,6 +567,12 @@ function bootstrapMainRuntime(runtimeOptions) {
570567
}
571568
}
572569

570+
if (!ArrayPrototypeIncludes(unstableFeatures, /* unsafe-proto */ 9)) {
571+
// Removes the `__proto__` for security reasons.
572+
// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
573+
delete Object.prototype.__proto__;
574+
}
575+
573576
// Setup `Deno` global - we're actually overriding already existing global
574577
// `Deno` with `Deno` namespace from "./deno.ts".
575578
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
@@ -668,6 +671,13 @@ function bootstrapWorkerRuntime(
668671
ObjectAssign(finalDenoNs, denoNsUnstableById[id]);
669672
}
670673
}
674+
675+
if (!ArrayPrototypeIncludes(unstableFeatures, /* unsafe-proto */ 9)) {
676+
// Removes the `__proto__` for security reasons.
677+
// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
678+
delete Object.prototype.__proto__;
679+
}
680+
671681
ObjectDefineProperties(finalDenoNs, {
672682
pid: util.getterOnly(opPid),
673683
noColor: util.getterOnly(() => ops.op_bootstrap_no_color()),

0 commit comments

Comments
 (0)