Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.0.2

- Remove `stacker` and nine transitive Rust dependencies from source builds.
CSS queries now use the calling thread's stack. Deeply nested selectors can
crash Python on unusually small worker stacks (e.g. 64 KiB).
- Pin the benchmark charts on PyPI to the release commit.

## 0.0.1

- Parse HTML from Python and query elements with CSS selectors.
Expand Down
87 changes: 1 addition & 86 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astral-html-python"
version = "0.0.1"
version = "0.0.2"
edition = "2024"
rust-version = "1.97"
description = "A Rust-backed HTML parser and CSS selector engine for Python"
Expand Down Expand Up @@ -35,7 +35,6 @@ pyo3 = "0.29"
selectors = "0.41"
cssparser = "0.38"
precomputed-hash = "0.1"
stacker = "0.1.25"

[profile.release]
lto = "thin"
Expand Down
7 changes: 5 additions & 2 deletions docs/parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ and `InvalidSelectorError` inherit from `ValueError`.
Servo parses CSS, but its matching callbacks cannot return an error. The
extension walks the parsed selectors itself so it can propagate a limit error
with Rust’s `Result`. Builders can choose `panic = "abort"` or `panic = "unwind"`
and get the same Python exception. On small Python worker stacks, `stacker`
provides room for recursive selector matching.
and get the same Python exception.

CSS parsing and matching recurse on the calling thread's stack. Avoid shrinking
Python worker stacks: on a 64 KiB stack, a selector within the depth limits
can still overflow the stack and crash Python.

Applications processing untrusted pages should also bound response size and
concurrent parses. The tokenizer reads a start tag and its attributes even
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "astral-html"
version = "0.0.1"
version = "0.0.2"
description = "A Rust-backed HTML parser and CSS selector engine for Python"
requires-python = ">=3.14"
license = "MIT OR Apache-2.0"
Expand Down
16 changes: 0 additions & 16 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,6 @@ pub fn select(
root: Option<usize>,
css: &str,
first: bool,
) -> Result<Vec<usize>, SelectError> {
// Reuse the caller's stack when at least 1 MiB remains. Otherwise request
// 2 MiB so parsing, matching and dropping nested selectors have headroom
// even on the small Python worker stacks in test_selector_robustness.py.
stacker::maybe_grow(1024 * 1024, 2 * 1024 * 1024, || {
select_with_stack(tree, root, css, first)
})
}

// Keep the matcher's frame inside the guarded closure even under optimization.
#[inline(never)]
fn select_with_stack(
tree: &Tree,
root: Option<usize>,
css: &str,
first: bool,
) -> Result<Vec<usize>, SelectError> {
if css.len() > MAX_SELECTOR_BYTES {
return Err(SelectError::ResourceLimit(SELECTOR_SIZE_ERROR.to_owned()));
Expand Down
14 changes: 3 additions & 11 deletions tests/test_selector_robustness.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import subprocess
import sys
import threading
from concurrent.futures import ThreadPoolExecutor

import pytest
from astral_html import ResourceLimitError, parse


def test_selector_limits_on_a_small_worker_stack() -> None:
# A native stack overflow aborts Python. Run the boundary cases together
# on a small worker stack, including the ones expected to succeed.
def test_selector_limits_on_a_worker_thread() -> None:
# Isolate native stack overflows from pytest. Exercise the platform's
# default worker stack, which can be smaller than the main thread's.
subprocess.run(
[sys.executable, "-X", "faulthandler", __file__], timeout=30, check=True
)
Expand Down Expand Up @@ -298,12 +297,5 @@ def test_selector_cache_memory(


if __name__ == "__main__":
# If a platform disallows both small sizes, still test its default stack.
for stack_size in (65536, 131072):
try:
threading.stack_size(stack_size)
except RuntimeError, ValueError:
continue
break
with ThreadPoolExecutor(max_workers=1) as worker:
worker.submit(exercise_stack_limits).result()
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.