Skip to content

Commit 0fae87e

Browse files
committed
fix panic with too many scriptlet arguments
1 parent f8ae9ef commit 0fae87e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/resources/resource_storage.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn template_argument_regex(i: usize) -> Regex {
215215
fn patch_template_scriptlet(mut template: String, args: &[impl AsRef<str>]) -> String {
216216
// `regex` treats `$` as a special character. Instead, `$$` is interpreted as a literal `$`
217217
// character.
218-
args.iter().enumerate().for_each(|(i, arg)| {
218+
args.iter().take(TEMPLATE_ARGUMENT_RE.len()).enumerate().for_each(|(i, arg)| {
219219
template = TEMPLATE_ARGUMENT_RE[i]
220220
.replace(&template, arg.as_ref().replace('$', "$$"))
221221
.to_string();
@@ -559,6 +559,30 @@ mod scriptlet_storage_tests {
559559
);
560560
}
561561

562+
/// Currently, only 9 template arguments are supported - but reaching that limit should not
563+
/// cause a panic.
564+
#[test]
565+
fn patch_argslist_many_args() {
566+
let resources = ResourceStorage::from_resources([
567+
Resource {
568+
name: "abort-current-script.js".into(),
569+
aliases: vec!["acs.js".into()],
570+
kind: ResourceType::Mime(MimeType::ApplicationJavascript),
571+
content: base64::encode("{{1}} {{2}} {{3}} {{4}} {{5}} {{6}} {{7}} {{8}} {{9}} {{10}} {{11}} {{12}}"),
572+
dependencies: vec![],
573+
permission: Default::default(),
574+
},
575+
]);
576+
577+
let args = parse_scriptlet_args("acs, this, probably, is, going, to, break, brave, and, crash, it, instead, of, ignoring, it");
578+
assert_eq!(args, vec!["acs", "this", "probably", "is", "going", "to", "break", "brave", "and", "crash", "it", "instead", "of", "ignoring", "it"]);
579+
580+
assert_eq!(
581+
resources.get_scriptlet_resource("acs, this, probably, is, going, to, break, brave, and, crash, it, instead, of, ignoring, it", Default::default()),
582+
Ok("this probably is going to break brave and crash {{10}} {{11}} {{12}}".to_string()),
583+
);
584+
}
585+
562586
#[test]
563587
fn permissions() {
564588
const PERM0: PermissionMask = PermissionMask::from_bits(0b00000001);

0 commit comments

Comments
 (0)