Skip to content

Commit

Permalink
Merge pull request #337 from brave/from-content-blocking
Browse files Browse the repository at this point in the history
Don't crash when converting content blocking rules with $from option
  • Loading branch information
antonok-edm authored Feb 21, 2024
2 parents f3c7fbb + de7120e commit 347dac1
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/content_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ pub enum CbRuleCreationFailure {
ScriptletInjectionsNotSupported,
/// Valid content blocking rules can only include ASCII characters.
RuleContainsNonASCII,
/// `from` as a `domain` alias is not currently supported in content blocking syntax.
FromNotSupported,
}

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

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

0 comments on commit 347dac1

Please sign in to comment.