Skip to content

The headless browser for AI agents and web scraping

fix(obscura): escape attribute name in Element::attribute()

Andrey Mnatsakanovbf4a795117d7
diff --git a/crates/obscura/src/page.rs b/crates/obscura/src/page.rs
index 38fb2d25..ca8a656f 100644
--- a/crates/obscura/src/page.rs
+++ b/crates/obscura/src/page.rs
@@ -164,9 +164,13 @@ impl Element {
/// Get an attribute value.
pub fn attribute(&self, name: &str) -> Option<String> {
let page = unsafe { &mut *(self.page as *mut Page) };
+ // Escape the name like query_selector escapes its selector, so a name
+ // containing a quote/backslash cannot break out of the JS string literal
+ // and inject code into the page.
+ let escaped = name.replace('\\', "\\\\").replace('\'', "\\'");
let val = page.evaluate(&format!(
"(function() {{ var el = globalThis._wrap && globalThis._wrap({}); return el ? el.getAttribute('{}') : null; }})()",
- self.node_id, name
+ self.node_id, escaped
));
if val.is_null() { None } else { Some(val.as_str().unwrap_or("").to_string()) }
}
diff --git a/crates/obscura/tests/attribute_injection.rs b/crates/obscura/tests/attribute_injection.rs
new file mode 100644
index 00000000..07859c7f
--- /dev/null
+++ b/crates/obscura/tests/attribute_injection.rs
@@ -0,0 +1,50 @@
+//! SEC-007 / #583 — `Element::attribute()` must escape the attribute name
+//! before interpolating it into page JS. A name containing a quote must not be
+//! able to break out of the `getAttribute('{name}')` string literal and run
+//! arbitrary JS in the page, the same guarantee `Page::query_selector` already
+//! gives for selectors.
+
+use obscura::Browser;
+
+#[tokio::test]
+async fn attribute_name_cannot_inject_js() {
+ let browser = Browser::new().unwrap();
+ let mut page = browser.new_page().await.unwrap();
+ page.goto("data:text/html,<div id=x data-safe=ok></div>")
+ .await
+ .unwrap();
+
+ // Canary the injection would flip from 0 to 1.
+ page.evaluate("globalThis.__pwned = 0");
+
+ let el = page.query_selector("#x").expect("element #x should be found");
+
+ // Payload breaks out of `getAttribute('{name}')` while staying a single
+ // valid expression (the wrapper places it inside `el ? ... : null`, which
+ // forbids a top-level comma). String concatenation carries the assignment:
+ // el.getAttribute('x' + (globalThis.__pwned = 1) + '')
+ let _ = el.attribute("x' + (globalThis.__pwned = 1) + '");
+
+ let pwned = page.evaluate("globalThis.__pwned");
+ assert_ne!(
+ pwned.as_f64(),
+ Some(1.0),
+ "attribute() must not execute JS injected via the attribute name (got {pwned:?})"
+ );
+}
+
+#[tokio::test]
+async fn attribute_reads_ordinary_names() {
+ let browser = Browser::new().unwrap();
+ let mut page = browser.new_page().await.unwrap();
+ page.goto("data:text/html,<div id=x data-safe=ok></div>")
+ .await
+ .unwrap();
+
+ let el = page.query_selector("#x").expect("element #x should be found");
+ assert_eq!(
+ el.attribute("data-safe").as_deref(),
+ Some("ok"),
+ "escaping must not break reading a normal attribute name"
+ );
+}