The headless browser for AI agents and web scraping
fix(obscura-js): btoa must encode Latin-1, not UTF-8 (#996)
Andrey Mnatsakanov00dd6c7f810e
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 2735832d..8b098651 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -10907,7 +10907,7 @@ const _mkStore = () => {globalThis.localStorage = _mkStore();globalThis.sessionStorage = _mkStore();-globalThis.btoa = globalThis.btoa || ((s) => { const b = new TextEncoder().encode(s); const c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let r=""; for(let i=0;i<b.length;i+=3){const a=b[i],bb=b[i+1]??0,cc=b[i+2]??0; r+=c[a>>2]+c[((a&3)<<4)|(bb>>4)]+(i+1<b.length?c[((bb&15)<<2)|(cc>>6)]:"=")+(i+2<b.length?c[cc&63]:"=");} return r; });+globalThis.btoa = globalThis.btoa || ((s) => { s = String(s); const b = new Uint8Array(s.length); for (let i = 0; i < s.length; i++) { const cp = s.charCodeAt(i); if (cp > 0xFF) throw new DOMException("The string to be encoded contains characters outside of the Latin1 range.", "InvalidCharacterError"); b[i] = cp; } const c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let r=""; for(let i=0;i<b.length;i+=3){const a=b[i],bb=b[i+1]??0,cc=b[i+2]??0; r+=c[a>>2]+c[((a&3)<<4)|(bb>>4)]+(i+1<b.length?c[((bb&15)<<2)|(cc>>6)]:"=")+(i+2<b.length?c[cc&63]:"=");} return r; });globalThis.atob = globalThis.atob || ((s) => {const c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";const r=[];diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex d9d7f2b9..0300e447 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -13133,6 +13133,26 @@ mod tests {assert_eq!(result.as_f64().unwrap() as usize, 180_000);}+ // #996: btoa maps each code unit to one Latin-1 byte and throws for code+ // points above 0xFF — it must not UTF-8-encode the input.+ #[test]+ fn btoa_encodes_latin1_not_utf8() {+ let mut rt = setup_runtime("<html><body></body></html>");+ // é (U+00E9) is the single byte 0xE9 -> "6Q==", not UTF-8's "w6k=".+ assert_eq!(rt.evaluate("btoa('é')").unwrap(), serde_json::json!("6Q=="));+ assert_eq!(+ rt.evaluate("btoa('hello')").unwrap(),+ serde_json::json!("aGVsbG8=")+ );+ // A code point above 0xFF must throw InvalidCharacterError.+ let name = rt+ .evaluate(+ r#"(() => { try { btoa('\u{1F600}'); return 'no-throw'; } catch (e) { return e.name; } })()"#,+ )+ .unwrap();+ assert_eq!(name, serde_json::json!("InvalidCharacterError"));+ }+#[test]fn navigation_api_updates_current_entry_state() {let mut rt = setup_runtime("<html><body></body></html>");