mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
dom: Always replace unpaired surrogates when handling page text (#35381)
Background: > JavaScript strings are potentially ill-formed UTF-16 (arbitrary > Vec<u16>) and can contain unpaired surrogates. Rust’s String type is > well-formed UTF-8 and can not contain any surrogate. Surrogates are > never emitted when decoding bytes from the network, but they can sneak > in through document.write, the Element.innerHtml setter, or other DOM > APIs. In 2015, Servo launched an experiment to see if unpaired surrogates cropped up in page content. That experiment caused Servo to panic if unpaired surrogates were encountered with a request to report the page to bug #6564. During that time several pages were reported with unpaired surrogates, causing Servo to panic. In addition, when running the WPT tests Servo will never panic due to the `-Z replace-surrogates` option being passed by the test driver. Motivation: After this 10 year experiment, it's clear that unpaired surrogates are a real concern in page content. Several reports were filed of Servo panicking after encountering them in real world pages. A complete fix for this issue would be to somehow maintain unpaired surrogates in the DOM, but that is a much larger task than simply emitting U+FFD instead of an unpaired surrogate. Since it is clear that this kind of content exists, it is better for Servo to try its best to handle the content rather than crash as production browsers should not crash due to user content when possible. In this change, I modify Servo to always replace unpaired surrogates. It would have been ideal to only crash when debug assertions are enabled, but debug assertions are enabled by default in release mode -- so this wouldn't be effective for WPT tests. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
b483cdb786
commit
75cf3d7265
10 changed files with 17 additions and 90 deletions
|
@ -133,15 +133,10 @@ impl CharacterDataMethods<crate::DomTypeHolder> for CharacterData {
|
|||
|
||||
// https://dom.spec.whatwg.org/#dom-characterdata-substringdata
|
||||
fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString> {
|
||||
let replace_surrogates = self
|
||||
.upcast::<Node>()
|
||||
.owner_doc()
|
||||
.window()
|
||||
.replace_surrogates();
|
||||
let data = self.data.borrow();
|
||||
// Step 1.
|
||||
let mut substring = String::new();
|
||||
let remaining = match split_at_utf16_code_unit_offset(&data, offset, replace_surrogates) {
|
||||
let remaining = match split_at_utf16_code_unit_offset(&data, offset) {
|
||||
Ok((_, astral, s)) => {
|
||||
// As if we had split the UTF-16 surrogate pair in half
|
||||
// and then transcoded that to UTF-8 lossily,
|
||||
|
@ -154,7 +149,7 @@ impl CharacterDataMethods<crate::DomTypeHolder> for CharacterData {
|
|||
// Step 2.
|
||||
Err(()) => return Err(Error::IndexSize),
|
||||
};
|
||||
match split_at_utf16_code_unit_offset(remaining, count, replace_surrogates) {
|
||||
match split_at_utf16_code_unit_offset(remaining, count) {
|
||||
// Steps 3.
|
||||
Err(()) => substring += remaining,
|
||||
// Steps 4.
|
||||
|
@ -191,16 +186,11 @@ impl CharacterDataMethods<crate::DomTypeHolder> for CharacterData {
|
|||
fn ReplaceData(&self, offset: u32, count: u32, arg: DOMString) -> ErrorResult {
|
||||
let mut new_data;
|
||||
{
|
||||
let replace_surrogates = self
|
||||
.upcast::<Node>()
|
||||
.owner_doc()
|
||||
.window()
|
||||
.replace_surrogates();
|
||||
let data = self.data.borrow();
|
||||
let prefix;
|
||||
let replacement_before;
|
||||
let remaining;
|
||||
match split_at_utf16_code_unit_offset(&data, offset, replace_surrogates) {
|
||||
match split_at_utf16_code_unit_offset(&data, offset) {
|
||||
Ok((p, astral, r)) => {
|
||||
prefix = p;
|
||||
// As if we had split the UTF-16 surrogate pair in half
|
||||
|
@ -214,7 +204,7 @@ impl CharacterDataMethods<crate::DomTypeHolder> for CharacterData {
|
|||
};
|
||||
let replacement_after;
|
||||
let suffix;
|
||||
match split_at_utf16_code_unit_offset(remaining, count, replace_surrogates) {
|
||||
match split_at_utf16_code_unit_offset(remaining, count) {
|
||||
// Steps 3.
|
||||
Err(()) => {
|
||||
replacement_after = "";
|
||||
|
@ -318,17 +308,7 @@ impl<'dom> LayoutCharacterDataHelpers<'dom> for LayoutDom<'dom, CharacterData> {
|
|||
/// The two string slices are such that:
|
||||
/// `before == s.to_utf16()[..offset - 1].to_utf8()` and
|
||||
/// `after == s.to_utf16()[offset + 1..].to_utf8()`
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Note that the third variant is only ever returned when the `-Z replace-surrogates`
|
||||
/// command-line option is specified.
|
||||
/// When it *would* be returned but the option is *not* specified, this function panics.
|
||||
fn split_at_utf16_code_unit_offset(
|
||||
s: &str,
|
||||
offset: u32,
|
||||
replace_surrogates: bool,
|
||||
) -> Result<(&str, Option<char>, &str), ()> {
|
||||
fn split_at_utf16_code_unit_offset(s: &str, offset: u32) -> Result<(&str, Option<char>, &str), ()> {
|
||||
let mut code_units = 0;
|
||||
for (i, c) in s.char_indices() {
|
||||
if code_units == offset {
|
||||
|
@ -338,17 +318,9 @@ fn split_at_utf16_code_unit_offset(
|
|||
code_units += 1;
|
||||
if c > '\u{FFFF}' {
|
||||
if code_units == offset {
|
||||
if replace_surrogates {
|
||||
debug_assert_eq!(c.len_utf8(), 4);
|
||||
return Ok((&s[..i], Some(c), &s[i + c.len_utf8()..]));
|
||||
}
|
||||
panic!(
|
||||
"\n\n\
|
||||
Would split a surrogate pair in CharacterData API.\n\
|
||||
If you see this in real content, please comment with the URL\n\
|
||||
on https://github.com/servo/servo/issues/6873\n\
|
||||
\n"
|
||||
);
|
||||
debug_assert_eq!(c.len_utf8(), 4);
|
||||
warn!("Splitting a surrogate pair in CharacterData API.");
|
||||
return Ok((&s[..i], Some(c), &s[i + c.len_utf8()..]));
|
||||
}
|
||||
code_units += 1;
|
||||
}
|
||||
|
|
|
@ -374,10 +374,6 @@ pub(crate) struct Window {
|
|||
/// won't be loaded.
|
||||
userscripts_path: Option<String>,
|
||||
|
||||
/// Replace unpaired surrogates in DOM strings with U+FFFD.
|
||||
/// See <https://github.com/servo/servo/issues/6564>
|
||||
replace_surrogates: bool,
|
||||
|
||||
/// Window's GL context from application
|
||||
#[ignore_malloc_size_of = "defined in script_thread"]
|
||||
#[no_trace]
|
||||
|
@ -619,10 +615,6 @@ impl Window {
|
|||
self.userscripts_path.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn replace_surrogates(&self) -> bool {
|
||||
self.replace_surrogates
|
||||
}
|
||||
|
||||
pub(crate) fn get_player_context(&self) -> WindowGLContext {
|
||||
self.player_context.clone()
|
||||
}
|
||||
|
@ -2778,7 +2770,6 @@ impl Window {
|
|||
unminify_css: bool,
|
||||
local_script_source: Option<String>,
|
||||
userscripts_path: Option<String>,
|
||||
replace_surrogates: bool,
|
||||
user_agent: Cow<'static, str>,
|
||||
player_context: WindowGLContext,
|
||||
#[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
|
||||
|
@ -2864,7 +2855,6 @@ impl Window {
|
|||
prepare_for_screenshot,
|
||||
unminify_css,
|
||||
userscripts_path,
|
||||
replace_surrogates,
|
||||
player_context,
|
||||
throttled: Cell::new(false),
|
||||
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
|
||||
|
|
|
@ -318,10 +318,6 @@ pub struct ScriptThread {
|
|||
/// won't be loaded
|
||||
userscripts_path: Option<String>,
|
||||
|
||||
/// Replace unpaired surrogates in DOM strings with U+FFFD.
|
||||
/// See <https://github.com/servo/servo/issues/6564>
|
||||
replace_surrogates: bool,
|
||||
|
||||
/// An optional string allowing the user agent to be set for testing.
|
||||
user_agent: Cow<'static, str>,
|
||||
|
||||
|
@ -957,7 +953,6 @@ impl ScriptThread {
|
|||
local_script_source: opts.local_script_source.clone(),
|
||||
unminify_css: opts.unminify_css,
|
||||
userscripts_path: opts.userscripts.clone(),
|
||||
replace_surrogates: opts.debug.replace_surrogates,
|
||||
user_agent,
|
||||
player_context: state.player_context,
|
||||
node_ids: Default::default(),
|
||||
|
@ -3146,7 +3141,6 @@ impl ScriptThread {
|
|||
self.unminify_css,
|
||||
self.local_script_source.clone(),
|
||||
self.userscripts_path.clone(),
|
||||
self.replace_surrogates,
|
||||
self.user_agent.clone(),
|
||||
self.player_context.clone(),
|
||||
#[cfg(feature = "webgpu")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue