Skip to content

Commit 9e691e7

Browse files
authored
fix(frontmatter): Restrict code fence length (#16334)
### What does this PR try to resolve? Port rust-lang/rust#149358 over to Cargo ### How to test and review this PR?
2 parents bbb5c5b + 5c49e2b commit 9e691e7

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/cargo/util/frontmatter.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ impl<'s> ScriptSource<'s> {
7575
raw.len()..raw.len(),
7676
).push_visible_span(open_start..open_end));
7777
}
78+
_ if u8::try_from(fence_length).is_err() => {
79+
return Err(FrontmatterError::new(
80+
format!(
81+
"too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found {fence_length}"
82+
),
83+
open_start..open_end,
84+
));
85+
}
7886
_ => {}
7987
}
8088
source.open = Some(open_start..open_end);

tests/testsuite/script/rustc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ fn fence_mismatch_2() {
270270
assert_failure(fixture_path, assertion_path);
271271
}
272272

273+
#[cargo_test(nightly, reason = "-Zscript is unstable")]
274+
#[rustfmt::skip] // code-generated
275+
fn fence_too_many_dashes() {
276+
let fixture_path = "tests/testsuite/script/rustc_fixtures/fence-too-many-dashes.rs";
277+
let assertion_path = "tests/testsuite/script/rustc_fixtures/fence-too-many-dashes.stderr";
278+
assert_failure(fixture_path, assertion_path);
279+
}
280+
273281
#[cargo_test(nightly, reason = "-Zscript is unstable")]
274282
#[rustfmt::skip] // code-generated
275283
fn fence_unclosed_1() {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2+
[dependencies]
3+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4+
5+
#![feature(frontmatter)]
6+
7+
// check that we limit fence lengths
8+
9+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[ERROR] too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found 256
2+
--> script:1:1
3+
|
4+
1 | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)