Skip to content
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

Throw TypeError if regexp is passed to startsWith, endsWith, includes #782

Merged
merged 1 commit into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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