Skip to content

Commit

Permalink
fix: lossily UTF8 decode module source text (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Jun 7, 2024
1 parent 98d52cf commit ec38e42
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
10 changes: 3 additions & 7 deletions core/modules/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,7 @@ impl ModuleMap {

let module_id = match module_type {
ModuleType::JavaScript => {
let code =
ModuleSource::get_string_source(module_url_found.as_str(), code)
.map_err(ModuleError::Other)?;
let code = ModuleSource::get_string_source(code);

let (code_cache_info, module_url_found) =
if let Some(code_cache) = code_cache {
Expand Down Expand Up @@ -379,9 +377,7 @@ impl ModuleMap {
)));
}
ModuleType::Json => {
let code =
ModuleSource::get_string_source(module_url_found.as_str(), code)
.map_err(ModuleError::Other)?;
let code = ModuleSource::get_string_source(code);
self.new_json_module(scope, module_url_found, code)?
}
ModuleType::Other(module_type) => {
Expand Down Expand Up @@ -1757,7 +1753,7 @@ impl ModuleMap {
self.lazy_load_es_module_with_code(
scope,
module_specifier,
ModuleSource::get_string_source(specifier.as_str(), source.code)?,
ModuleSource::get_string_source(source.code),
if let Some(code_cache) = source.code_cache {
let loader = self.loader.borrow().clone();
Some(CodeCacheInfo {
Expand Down
16 changes: 6 additions & 10 deletions core/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::fast_string::FastString;
use crate::module_specifier::ModuleSpecifier;
use crate::FastStaticString;
use anyhow::bail;
use anyhow::Context;
use anyhow::Error;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -459,17 +458,14 @@ impl ModuleSource {
}
}

pub fn get_string_source(
specifier: &str,
code: ModuleSourceCode,
) -> Result<ModuleCodeString, AnyError> {
pub fn get_string_source(code: ModuleSourceCode) -> ModuleCodeString {
match code {
ModuleSourceCode::String(code) => Ok(code),
ModuleSourceCode::String(code) => code,
ModuleSourceCode::Bytes(bytes) => {
let str_ = String::from_utf8(bytes.to_vec()).with_context(|| {
format!("Can't convert source code to string for {}", specifier)
})?;
Ok(ModuleCodeString::from(str_))
match String::from_utf8_lossy(bytes.as_bytes()) {
Cow::Borrowed(s) => ModuleCodeString::from(s.to_owned()),
Cow::Owned(s) => ModuleCodeString::from(s),
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions core/modules/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,3 +1914,14 @@ fn ext_module_loader_relative() {
assert_eq!(result.as_str(), expected);
}
}

#[test]
fn invalid_utf8_module() {
let get_string_source = ModuleSource::get_string_source(
ModuleSourceCode::Bytes(ModuleCodeBytes::Static(b"// \xFE\xFE\xFF\xFF")),
);
assert_eq!(
get_string_source,
FastString::from_static("// \u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}")
);
}

0 comments on commit ec38e42

Please sign in to comment.