Merge remote-tracking branch 'origin/main' into maintainer/pr-492-fix
This commit is contained in:
commit
82aa320084
18 files changed
+1248
-150
No files matched your search
Vendored
+112
-10
@@ -18,7 +18,7 @@
|
||||
'__obscura_hw', '__obscura_mem',
|
||||
'__documentReadyState__', '__currentUrl',
|
||||
// internal helpers (var-declared throughout the file)
|
||||
'__processDynScriptQueue', '_markNative', '_fpRand', '_fpNoise',
|
||||
'__processDynScriptQueue', '_decodeDataScriptUrl', '_markNative', '_fpRand', '_fpNoise',
|
||||
'_fpCache', '_getFp', '_fp', '_splitAsciiWhitespace',
|
||||
'_getElementsByClassName', '_docEncoding', '_docIsUtf8',
|
||||
'_isSpecialScheme', '_applyDocQueryEncoding', '_anchorBase',
|
||||
@@ -163,6 +163,51 @@ let _fpSeed = 0;
|
||||
// when SPAs dynamically insert multiple <script module> tags at once.
|
||||
let __dynScriptQueue = [];
|
||||
let __dynScriptBusy = false;
|
||||
function _decodeDataScriptUrl(url) {
|
||||
const comma = url.indexOf(',');
|
||||
if (!url.startsWith('data:') || comma < 5) {
|
||||
throw new TypeError('Invalid dynamic script data URL');
|
||||
}
|
||||
|
||||
const meta = url.slice(5, comma);
|
||||
const fragment = url.indexOf('#', comma + 1);
|
||||
const payload = url.slice(comma + 1, fragment < 0 ? url.length : fragment);
|
||||
if (meta.split(';').some(part => part.toLowerCase() === 'base64')) {
|
||||
let encoded = payload.replace(/[\r\n\t\f ]/g, '');
|
||||
const remainder = encoded.length % 4;
|
||||
if (remainder === 1 || !/^[A-Za-z0-9+/]*={0,2}$/.test(encoded) || /=/.test(encoded.slice(0, -2))) {
|
||||
throw new TypeError('Invalid dynamic script data URL base64');
|
||||
}
|
||||
if (remainder > 0) encoded += '='.repeat(4 - remainder);
|
||||
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(encoded)) {
|
||||
throw new TypeError('Invalid dynamic script data URL base64');
|
||||
}
|
||||
return new TextDecoder().decode(_base64ToUint8Array(encoded));
|
||||
}
|
||||
|
||||
const bytes = [];
|
||||
for (let i = 0; i < payload.length; i++) {
|
||||
const code = payload.charCodeAt(i);
|
||||
if (code === 0x25 && i + 2 < payload.length) {
|
||||
const hi = _hexv(payload.charCodeAt(i + 1));
|
||||
const lo = _hexv(payload.charCodeAt(i + 2));
|
||||
if (hi >= 0 && lo >= 0) {
|
||||
bytes.push(hi * 16 + lo);
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (code < 0x80) {
|
||||
bytes.push(code);
|
||||
} else {
|
||||
const character = String.fromCodePoint(payload.codePointAt(i));
|
||||
if (character.length === 2) i++;
|
||||
const encoded = new TextEncoder().encode(character);
|
||||
for (let j = 0; j < encoded.length; j++) bytes.push(encoded[j]);
|
||||
}
|
||||
}
|
||||
return new TextDecoder().decode(new Uint8Array(bytes));
|
||||
}
|
||||
async function __processDynScriptQueue() {
|
||||
if (__dynScriptBusy) return;
|
||||
__dynScriptBusy = true;
|
||||
@@ -176,11 +221,16 @@ async function __processDynScriptQueue() {
|
||||
if (task.isModule) {
|
||||
await import(task.url);
|
||||
} else {
|
||||
const raw = await Deno.core.ops.op_fetch_url(task.url, "GET", "{}", "", task.pageOrigin, "no-cors");
|
||||
const parsed = JSON.parse(raw);
|
||||
if (parsed.body) {
|
||||
let body;
|
||||
if (task.url.startsWith('data:')) {
|
||||
body = _decodeDataScriptUrl(task.url);
|
||||
} else {
|
||||
const raw = await Deno.core.ops.op_fetch_url(task.url, "GET", "{}", "", task.pageOrigin, "no-cors");
|
||||
body = JSON.parse(raw).body;
|
||||
}
|
||||
if (body) {
|
||||
globalThis.__currentScriptNid = task.nid;
|
||||
try { (0, eval)(parsed.body); }
|
||||
try { (0, eval)(body); }
|
||||
catch(e) { console.error('Dynamic script error (' + task.url + '):', e.message); }
|
||||
finally { globalThis.__currentScriptNid = task.prevNid || 0; }
|
||||
}
|
||||
@@ -1255,11 +1305,21 @@ class Element extends Node {
|
||||
set className(v) { this.setAttribute("class", v); }
|
||||
get namespaceURI() {
|
||||
// createElementNS records the requested namespace on _ns; an empty string
|
||||
// maps to the null namespace per spec. Elements made via createElement (or
|
||||
// parsed) have no _ns: default to XHTML, except <svg> which is SVG.
|
||||
// maps to the null namespace per spec.
|
||||
if (this._ns !== undefined) return this._ns === "" ? null : this._ns;
|
||||
if (this.localName === "svg") return "http://www.w3.org/2000/svg";
|
||||
return "http://www.w3.org/1999/xhtml";
|
||||
// Otherwise use the namespace the HTML tree builder assigned. Foreign
|
||||
// content puts the WHOLE <svg>/<math> subtree in that namespace, not just
|
||||
// the root, so deriving it from the tag name (the old `localName === "svg"`
|
||||
// check) left every descendant looking like HTML and skipped the SVG-only
|
||||
// reflections -- notably `get href()`, which then returned a plain string
|
||||
// instead of an SVGAnimatedString. An element's namespace never changes,
|
||||
// so cache it like _lname.
|
||||
if (this._nsCache !== undefined) return this._nsCache;
|
||||
let ns = _domParse("namespace_uri", this._nid) || "";
|
||||
// Nodes with no element name recorded fall back to the previous heuristic.
|
||||
if (!ns) ns = this.localName === "svg" ? "http://www.w3.org/2000/svg" : "http://www.w3.org/1999/xhtml";
|
||||
this._nsCache = ns;
|
||||
return ns;
|
||||
}
|
||||
// `inner_html` resolves a <template> to its contents document on the Rust
|
||||
// side (issue #463), so this needs no template special case.
|
||||
@@ -2648,6 +2708,13 @@ class Document extends Node {
|
||||
// returned a generic Event for every type, which broke libraries that call
|
||||
// createEvent('CustomEvent').initCustomEvent(...) — see issue #41.
|
||||
createEvent(type) {
|
||||
const normalized = String(type || '').toLowerCase();
|
||||
if (normalized === 'promiserejectionevent') {
|
||||
throw new DOMException(
|
||||
"The provided event type ('PromiseRejectionEvent') is invalid",
|
||||
'NotSupportedError'
|
||||
);
|
||||
}
|
||||
const map = {
|
||||
'customevent': CustomEvent, 'customevents': CustomEvent,
|
||||
'mouseevent': MouseEvent, 'mouseevents': MouseEvent,
|
||||
@@ -2662,8 +2729,9 @@ class Document extends Node {
|
||||
'popstateevent': PopStateEvent,
|
||||
'animationevent': AnimationEvent,
|
||||
'transitionevent': TransitionEvent,
|
||||
'storageevent': StorageEvent,
|
||||
};
|
||||
const Cls = map[String(type || '').toLowerCase()] || Event;
|
||||
const Cls = map[normalized] || Event;
|
||||
return new Cls('');
|
||||
}
|
||||
createRange() { return new Range(); }
|
||||
@@ -5042,6 +5110,40 @@ globalThis.ToggleEvent = class ToggleEvent extends Event {
|
||||
};
|
||||
_markNative(globalThis.ToggleEvent);
|
||||
|
||||
globalThis.PromiseRejectionEvent = class PromiseRejectionEvent extends Event {
|
||||
constructor(type, init) {
|
||||
if (arguments.length < 2 || init == null || !('promise' in Object(init))) {
|
||||
throw new TypeError(
|
||||
"Failed to construct 'PromiseRejectionEvent': required member promise is undefined."
|
||||
);
|
||||
}
|
||||
super(type, init);
|
||||
this.promise = init.promise;
|
||||
this.reason = init.reason;
|
||||
}
|
||||
};
|
||||
_markNative(globalThis.PromiseRejectionEvent);
|
||||
|
||||
globalThis.StorageEvent = class StorageEvent extends Event {
|
||||
constructor(type, init = {}) {
|
||||
super(type, init);
|
||||
this.key = init.key !== undefined ? init.key : null;
|
||||
this.oldValue = init.oldValue !== undefined ? init.oldValue : null;
|
||||
this.newValue = init.newValue !== undefined ? init.newValue : null;
|
||||
this.url = init.url || "";
|
||||
this.storageArea = init.storageArea || null;
|
||||
}
|
||||
initStorageEvent(type, bubbles, cancelable, key, oldValue, newValue, url, storageArea) {
|
||||
this.initEvent(type, bubbles, cancelable);
|
||||
this.key = key !== undefined ? key : null;
|
||||
this.oldValue = oldValue !== undefined ? oldValue : null;
|
||||
this.newValue = newValue !== undefined ? newValue : null;
|
||||
this.url = url || "";
|
||||
this.storageArea = storageArea || null;
|
||||
}
|
||||
};
|
||||
_markNative(globalThis.StorageEvent);
|
||||
|
||||
// AbortController / AbortSignal. AbortSignal is a real constructor with a
|
||||
// prototype, so feature-detection and `AbortSignal.prototype` access work. It
|
||||
// carries aborted/reason, supports throwIfAborted(), and fires "abort" to
|
||||
|
||||
@@ -303,6 +303,14 @@ fn op_dom_inner(state: &OpState, cmd: String, arg1: String, arg2: String) -> Str
|
||||
let name = dom.with_node(NodeId::new(nid), |n| n.as_element().map(|name| name.local.as_ref().to_ascii_uppercase())).flatten().unwrap_or_default();
|
||||
serde_json::to_string(&name).unwrap_or("\"\"".into())
|
||||
}
|
||||
// The tree builder already assigns foreign content (an <svg>/<math>
|
||||
// subtree) its own namespace; expose it so JS does not have to guess
|
||||
// the namespace from the tag name.
|
||||
"namespace_uri" => {
|
||||
let nid = arg1.parse::<u32>().unwrap_or(0);
|
||||
let ns = dom.with_node(NodeId::new(nid), |n| n.as_element().map(|name| name.ns.as_ref().to_string())).flatten().unwrap_or_default();
|
||||
serde_json::to_string(&ns).unwrap_or("\"\"".into())
|
||||
}
|
||||
"get_attribute" => {
|
||||
let nid = arg1.parse::<u32>().unwrap_or(0);
|
||||
let val = dom.with_node(NodeId::new(nid), |n| n.get_attribute(&arg2).map(|s| s.to_string())).flatten();
|
||||
@@ -570,24 +578,9 @@ fn op_console_msg(state: &OpState, #[string] level: &str, #[string] msg: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
// op_fetch_url backs JS-level `fetch()` and XHR. Pre-#139 it used a
|
||||
// process-wide `OnceLock<reqwest::Client>` initialised with no proxy, so
|
||||
// every JS network call bypassed the configured upstream proxy. We now
|
||||
// build a client per request, threading whatever `proxy_url` the page's
|
||||
// ObscuraHttpClient was configured with.
|
||||
//
|
||||
// The per-request build cost is negligible (≪1ms) compared with the actual
|
||||
// network round-trip; the simplification is worth not having to invalidate
|
||||
// a cache when the proxy is reconfigured between fetches.
|
||||
//
|
||||
// Process-wide cache keyed by proxy URL. Previously we built a fresh
|
||||
// reqwest::Client on every op_fetch_url call (every JS fetch(), XHR,
|
||||
// dynamic script load). Each build re-initialised TLS roots and a
|
||||
// fresh connection pool with zero reuse, costing ~5ms per fetch on top
|
||||
// of any real network work. On an asset-heavy page with 30+ subresources
|
||||
// that adds ~150ms of pure waste. With the cache, the first fetch on a
|
||||
// given proxy pays the build cost once and every subsequent fetch reuses
|
||||
// the same connection pool.
|
||||
// Fallback cache for runtimes that have no owning ObscuraHttpClient, such as
|
||||
// a standalone module loader. Browser pages use their context-scoped client
|
||||
// below so sequential V8 runtimes never share an async network pool (#453).
|
||||
static FETCH_CLIENT_CACHE: std::sync::OnceLock<
|
||||
std::sync::RwLock<std::collections::HashMap<String, reqwest::Client>>,
|
||||
> = std::sync::OnceLock::new();
|
||||
@@ -626,13 +619,9 @@ fn build_request_client(proxy_url: Option<&str>) -> Result<reqwest::Client, Stri
|
||||
// send().await errors, which op_fetch_url propagates and the fetch shim turns
|
||||
// into an XHR `error`/`loadend`. 30s matches the other clients in the
|
||||
// workspace; OBSCURA_FETCH_TIMEOUT_MS overrides it for tighter cloud limits.
|
||||
let timeout_ms: u64 = std::env::var("OBSCURA_FETCH_TIMEOUT_MS")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(30_000);
|
||||
let mut builder = reqwest::Client::builder()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.timeout(std::time::Duration::from_millis(timeout_ms))
|
||||
.timeout(fetch_timeout())
|
||||
// SSRF guard: also reject hostnames that resolve to a private/loopback IP.
|
||||
.dns_resolver(std::sync::Arc::new(obscura_net::SsrfGuardResolver::new(false)))
|
||||
// Be explicit about pool size: default is unbounded which is fine,
|
||||
@@ -651,6 +640,14 @@ fn build_request_client(proxy_url: Option<&str>) -> Result<reqwest::Client, Stri
|
||||
.map_err(|e| format!("failed to build reqwest::Client: {}", e))
|
||||
}
|
||||
|
||||
fn fetch_timeout() -> std::time::Duration {
|
||||
let timeout_ms = std::env::var("OBSCURA_FETCH_TIMEOUT_MS")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(30_000);
|
||||
std::time::Duration::from_millis(timeout_ms)
|
||||
}
|
||||
|
||||
/// Cap on the number of redirect hops op_fetch_url will follow.
|
||||
/// Matches reqwest's default policy of 10.
|
||||
const FETCH_REDIRECT_LIMIT: usize = 10;
|
||||
@@ -681,7 +678,7 @@ async fn op_fetch_url(
|
||||
}
|
||||
}
|
||||
|
||||
let (cookie_jar, in_flight, intercept_tx, proxy_url, callbacks) = {
|
||||
let (cookie_jar, in_flight, intercept_tx, proxy_url, callbacks, http_client) = {
|
||||
let state_borrow = state.borrow();
|
||||
let gs = state_borrow.borrow::<SharedState>().clone();
|
||||
let mut gs = gs.borrow_mut();
|
||||
@@ -713,7 +710,14 @@ async fn op_fetch_url(
|
||||
} else {
|
||||
None
|
||||
};
|
||||
(jar, in_flight, itx, proxy_url, gs.callbacks.clone())
|
||||
(
|
||||
jar,
|
||||
in_flight,
|
||||
itx,
|
||||
proxy_url,
|
||||
gs.callbacks.clone(),
|
||||
gs.http_client.clone(),
|
||||
)
|
||||
};
|
||||
|
||||
// Slots the interception channel can override via Continue so a consumer
|
||||
@@ -796,8 +800,11 @@ async fn op_fetch_url(
|
||||
let method = override_method.unwrap_or(method);
|
||||
let body = override_body.unwrap_or(body);
|
||||
|
||||
let client = cached_request_client(proxy_url.as_deref())
|
||||
.map_err(deno_error::JsErrorBox::generic)?;
|
||||
let client = match &http_client {
|
||||
Some(client) => client.request_client().await,
|
||||
None => cached_request_client(proxy_url.as_deref())
|
||||
.map_err(deno_error::JsErrorBox::generic)?,
|
||||
};
|
||||
|
||||
let request_origin = url::Url::parse(&url)
|
||||
.ok()
|
||||
@@ -878,6 +885,7 @@ async fn op_fetch_url(
|
||||
if needs_preflight {
|
||||
let preflight = client
|
||||
.request(reqwest::Method::OPTIONS, &url)
|
||||
.timeout(fetch_timeout())
|
||||
.header("Origin", &page_origin)
|
||||
.header("Access-Control-Request-Method", method.as_str())
|
||||
.header(
|
||||
@@ -911,7 +919,9 @@ async fn op_fetch_url(
|
||||
let mut current_body = body;
|
||||
let mut redirects_followed: usize = 0;
|
||||
let response = loop {
|
||||
let mut req = client.request(current_method.clone(), ¤t_url);
|
||||
let mut req = client
|
||||
.request(current_method.clone(), ¤t_url)
|
||||
.timeout(fetch_timeout());
|
||||
|
||||
if is_cross_origin {
|
||||
req = req.header("Origin", &page_origin);
|
||||
|
||||
@@ -3145,6 +3145,101 @@ mod tests {
|
||||
assert_eq!(kind, serde_json::json!(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_promise_rejection_event_requires_promise() {
|
||||
let mut rt = setup_runtime("<html><body></body></html>");
|
||||
let result = rt
|
||||
.evaluate(
|
||||
r#"(() => {
|
||||
const promise = Promise.resolve(1);
|
||||
const event = new PromiseRejectionEvent('unhandledrejection', {
|
||||
promise,
|
||||
reason: 'failed'
|
||||
});
|
||||
let missingPromiseThrows = false;
|
||||
try {
|
||||
new PromiseRejectionEvent('unhandledrejection');
|
||||
} catch (error) {
|
||||
missingPromiseThrows = error instanceof TypeError;
|
||||
}
|
||||
return [
|
||||
event instanceof Event,
|
||||
event.promise === promise,
|
||||
event.reason === 'failed',
|
||||
missingPromiseThrows
|
||||
];
|
||||
})()"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result, serde_json::json!([true, true, true, true]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_event_rejects_promise_rejection_event() {
|
||||
let mut rt = setup_runtime("<html><body></body></html>");
|
||||
let result = rt
|
||||
.evaluate(
|
||||
r#"(() => {
|
||||
try {
|
||||
document.createEvent('PromiseRejectionEvent');
|
||||
return null;
|
||||
} catch (error) {
|
||||
return [error.name, error instanceof DOMException];
|
||||
}
|
||||
})()"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
result,
|
||||
serde_json::json!(["NotSupportedError", true])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_storage_event_constructor_and_legacy_factory() {
|
||||
let mut rt = setup_runtime("<html><body></body></html>");
|
||||
let result = rt
|
||||
.evaluate(
|
||||
r#"(() => {
|
||||
const event = new StorageEvent('storage', {
|
||||
key: 'theme',
|
||||
oldValue: 'light',
|
||||
newValue: 'dark',
|
||||
url: 'https://example.test/'
|
||||
});
|
||||
const legacy = document.createEvent('StorageEvent');
|
||||
legacy.initStorageEvent(
|
||||
'storage', false, false, 'count', '1', '2',
|
||||
'https://example.test/', null
|
||||
);
|
||||
return [
|
||||
event instanceof Event,
|
||||
event.key,
|
||||
event.oldValue,
|
||||
event.newValue,
|
||||
event.url,
|
||||
legacy instanceof StorageEvent,
|
||||
legacy.key,
|
||||
legacy.newValue
|
||||
];
|
||||
})()"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
result,
|
||||
serde_json::json!([
|
||||
true,
|
||||
"theme",
|
||||
"light",
|
||||
"dark",
|
||||
"https://example.test/",
|
||||
true,
|
||||
"count",
|
||||
"2"
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_html_to_markdown_headings() {
|
||||
let mut rt = setup_runtime("<html><body><h1>Title</h1><h2>Sub</h2><p>Body</p></body></html>");
|
||||
|
||||
Reference in new issue
Block a user