Skip to content

Commit 347dac1

Browse files
authored
Merge pull request #337 from brave/from-content-blocking
Don't crash when converting content blocking rules with $from option
2 parents f3c7fbb + de7120e commit 347dac1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/content_blocking.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ pub enum CbRuleCreationFailure {
202202
ScriptletInjectionsNotSupported,
203203
/// Valid content blocking rules can only include ASCII characters.
204204
RuleContainsNonASCII,
205+
/// `from` as a `domain` alias is not currently supported in content blocking syntax.
206+
FromNotSupported,
205207
}
206208

207209
impl TryFrom<ParsedFilter> for CbRuleEquivalent {
@@ -403,8 +405,13 @@ impl TryFrom<NetworkFilter> for CbRuleEquivalent {
403405
// Unwraps are okay here - any rules with opt_domains or opt_not_domains must have
404406
// an options section delimited by a '$' character, followed by a `domain=` option.
405407
let opts = &raw_line[find_char(b'$', raw_line.as_bytes()).unwrap() + "$".len()..];
408+
let domain_start_index = if let Some(index) = memmem::find(opts.as_bytes(), b"domain=") {
409+
index
410+
} else {
411+
return Err(CbRuleCreationFailure::FromNotSupported);
412+
};
406413
let domains_start =
407-
&opts[memmem::find(opts.as_bytes(), b"domain=").unwrap() + "domain=".len()..];
414+
&opts[domain_start_index + "domain=".len()..];
408415
let domains = if let Some(comma) = find_char(b',', domains_start.as_bytes()) {
409416
&domains_start[..comma]
410417
} else {
@@ -1356,6 +1363,8 @@ mod filterset_tests {
13561363
// unicode characters
13571364
"||rgmechanics.info/uploads/660х90_",
13581365
"||insaattrendy.com/Upload/bükerbanner*.jpg",
1366+
// from domain
1367+
"/siropu/am/core.min.js$script,important,from=~audi-sport.net|~hifiwigwam.com",
13591368
], Default::default());
13601369

13611370
let (cb_rules, used_rules) = set.into_content_blocking()?;

0 commit comments

Comments
 (0)