Skip to content

Commit

Permalink
Throw TypeError if regex is passed to startsWith, endsWith, includes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pt2121 authored Oct 3, 2020
1 parent b16f0ab commit aad860d
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 15 deletions.
49 changes: 34 additions & 15 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,15 @@ impl String {
// Then we convert it into a Rust String by wrapping it in from_value
let primitive_val = this.to_string(ctx)?;

// TODO: Should throw TypeError if pattern is regular expression
let search_string = args
.get(0)
.expect("failed to get argument for String method")
.to_string(ctx)?;
let arg = args.get(0).cloned().unwrap_or_else(Value::undefined);

if Self::is_regexp_object(&arg) {
ctx.throw_type_error(
"First argument to String.prototype.startsWith must not be a regular expression",
)?;
}

let search_string = arg.to_string(ctx)?;

let length = primitive_val.chars().count() as i32;
let search_length = search_string.chars().count() as i32;
Expand Down Expand Up @@ -374,11 +378,15 @@ impl String {
// Then we convert it into a Rust String by wrapping it in from_value
let primitive_val = this.to_string(ctx)?;

// TODO: Should throw TypeError if search_string is regular expression
let search_string = args
.get(0)
.expect("failed to get argument for String method")
.to_string(ctx)?;
let arg = args.get(0).cloned().unwrap_or_else(Value::undefined);

if Self::is_regexp_object(&arg) {
ctx.throw_type_error(
"First argument to String.prototype.endsWith must not be a regular expression",
)?;
}

let search_string = arg.to_string(ctx)?;

let length = primitive_val.chars().count() as i32;
let search_length = search_string.chars().count() as i32;
Expand Down Expand Up @@ -420,11 +428,15 @@ impl String {
// Then we convert it into a Rust String by wrapping it in from_value
let primitive_val = this.to_string(ctx)?;

// TODO: Should throw TypeError if search_string is regular expression
let search_string = args
.get(0)
.expect("failed to get argument for String method")
.to_string(ctx)?;
let arg = args.get(0).cloned().unwrap_or_else(Value::undefined);

if Self::is_regexp_object(&arg) {
ctx.throw_type_error(
"First argument to String.prototype.includes must not be a regular expression",
)?;
}

let search_string = arg.to_string(ctx)?;

let length = primitive_val.chars().count() as i32;

Expand Down Expand Up @@ -462,6 +474,13 @@ impl String {
}
}

fn is_regexp_object(value: &Value) -> bool {
match value {
Value::Object(ref obj) => obj.borrow().is_regexp(),
_ => false,
}
}

/// `String.prototype.replace( regexp|substr, newSubstr|function )`
///
/// The `replace()` method returns a new string with some or all matches of a `pattern` replaced by a `replacement`.
Expand Down
84 changes: 84 additions & 0 deletions boa/src/builtins/string/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,26 @@ fn starts_with() {
assert_eq!(forward(&mut engine, "zhLiteral.startsWith('中')"), "true");
}

#[test]
fn starts_with_with_regex_arg() {
let mut engine = Context::new();

let scenario = r#"
try {
'Saturday night'.startsWith(/Saturday/);
} catch (e) {
e.toString();
}
"#;

assert_eq!(
forward(
&mut engine, scenario
),
"\"TypeError: First argument to String.prototype.startsWith must not be a regular expression\""
)
}

#[test]
fn ends_with() {
let mut engine = Context::new();
Expand All @@ -344,6 +364,70 @@ fn ends_with() {
assert_eq!(forward(&mut engine, "zhLiteral.endsWith('文')"), "true");
}

#[test]
fn ends_with_with_regex_arg() {
let mut engine = Context::new();

let scenario = r#"
try {
'Saturday night'.endsWith(/night/);
} catch (e) {
e.toString();
}
"#;

assert_eq!(
forward(
&mut engine, scenario
),
"\"TypeError: First argument to String.prototype.endsWith must not be a regular expression\""
)
}

#[test]
fn includes() {
let mut engine = Context::new();
let init = r#"
var empty = new String('');
var en = new String('english');
var zh = new String('中文');
var emptyLiteral = '';
var enLiteral = 'english';
var zhLiteral = '中文';
"#;

forward(&mut engine, init);

assert_eq!(forward(&mut engine, "empty.includes('')"), "true");
assert_eq!(forward(&mut engine, "en.includes('g')"), "true");
assert_eq!(forward(&mut engine, "zh.includes('文')"), "true");

assert_eq!(forward(&mut engine, "emptyLiteral.includes('')"), "true");
assert_eq!(forward(&mut engine, "enLiteral.includes('g')"), "true");
assert_eq!(forward(&mut engine, "zhLiteral.includes('文')"), "true");
}

#[test]
fn includes_with_regex_arg() {
let mut engine = Context::new();

let scenario = r#"
try {
'Saturday night'.includes(/day/);
} catch (e) {
e.toString();
}
"#;

assert_eq!(
forward(
&mut engine, scenario
),
"\"TypeError: First argument to String.prototype.includes must not be a regular expression\""
)
}

#[test]
fn match_all() {
let mut engine = Context::new();
Expand Down

0 comments on commit aad860d

Please sign in to comment.