Skip to content

Commit

Permalink
Remove eval-like functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonforal committed Jan 17, 2025
1 parent 5f41ee2 commit ef2c64c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/static-analysis-kernel/src/analysis/ddsa_lib/v8_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const BASE_FLAGS: &str = concat!(
// Performance: compile JavaScript eagerly
" --no-lazy",
" --no-lazy-streaming",
// Don't allow "eval"-like functionality.
" --disallow-code-generation-from-strings",
);

/// An instance of the v8 platform.
Expand Down Expand Up @@ -122,6 +124,7 @@ pub fn initialize_v8(thread_pool_size: u32) -> V8Platform<Initialized> {
#[cfg(test)]
mod tests {
use super::{initialize_v8, BASE_FLAGS};
use crate::analysis::ddsa_lib::test_utils::{cfg_test_v8, try_execute};

/// `initialize_v8` can effectively only be called once.
#[test]
Expand All @@ -140,4 +143,23 @@ mod tests {
fn v8_contradictory_flags_abort() {
assert!(BASE_FLAGS.contains("--abort-on-contradictory-flags"));
}

/// v8 is initialized without the ability to run `eval`-like functions.
#[test]
fn v8_eval_like_disabled() {
let v8 = cfg_test_v8();
let mut rt = v8.new_runtime();
let scope = &mut rt.v8_handle_scope();
let samples = [
"eval('1 + 2');",
"new Function('a', 'b', 'return a + b;')(1, 2);",
];
for code in samples {
let res = try_execute(scope, code);
assert_eq!(
res.unwrap_err(),
"EvalError: Code generation from strings disallowed for this context"
);
}
}
}

0 comments on commit ef2c64c

Please sign in to comment.