openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

fix(memory,security,perf): chunker line splitting, DNS rebinding guard, regex caching

Open Liohtml opened this issue 6 months ago • 1 comments

Summary

Three targeted fixes addressing a known bug, a security gap, and a performance bottleneck:

  • Memory chunker oversize-line splitting — split_on_lines() previously emitted a single giant chunk for lines exceeding max_chars (acknowledged TODO in code). Now splits on word boundaries, falling back to hard char-split. Fixes #1882.
  • DNS rebinding SSRF protection — adds validate_url_with_dns_check() that resolves hostnames and re-validates resolved IPs against the private/local blocklist before making the HTTP request. Closes a gap where an attacker's domain could pass the allowlist then resolve to 127.0.0.1 at connection time.
  • TokenJuice regex caching — replaces per-call Regex::new() compilation in regex_match/regex_replace/regex_captures with a thread-local cache, eliminating O(n × patterns) compilation overhead on large tool outputs.

Changes

File What
src/openhuman/memory/chunker.rs New split_within_line() fn + 3 new tests
src/openhuman/tools/impl/network/url_guard.rs New validate_url_with_dns_check() fn + 3 new tests
src/openhuman/tokenjuice/reduce.rs Thread-local regex cache replacing fresh compilation

Test plan

  • [x] cargo check -p openhuman --lib — compiles clean (only pre-existing warnings)
  • [x] cargo test -p openhuman "memory::chunker" — 24/24 pass (3 new)
  • [x] cargo test -p openhuman "url_guard" — 43/43 pass (3 new)
  • [x] cargo test -p openhuman "tokenjuice::reduce" — 74/74 pass (0 new, all existing pass with cache)
  • [ ] Verify validate_url_with_dns_check is wired into http_request.rs and curl.rs callers (follow-up or reviewer can advise on migration path)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added DNS-based rebinding protection to URL validation
  • Bug Fixes

    • Chunking now splits overlong single lines, preferring word boundaries and preserving all content
  • Tests

    • Expanded tests for chunking edge cases (word-preserving and hard-split) and DNS-check behavior
  • Refactor

    • Improved regex handling with efficient thread-local caching

Review Change Stack

Liohtml avatar May 16 '26 14:05 Liohtml

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cbb38cd4-6193-4931-98a8-9f8a6a5465c5

📥 Commits

Reviewing files that changed from the base of the PR and between fe599f59847b9d2d110426bedba46f763afc754c and 0097b4137243d575c4fe504e1a8d7675f3b658cf.

📒 Files selected for processing (1)
  • src/openhuman/tools/impl/network/url_guard.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/openhuman/tools/impl/network/url_guard.rs

📝 Walkthrough

Walkthrough

Adds intra-line chunking to split oversized lines at word boundaries with UTF-8 safety, introduces a thread-local regex compilation cache, and adds DNS-based URL validation that rejects hosts resolving to private/local IPs with accompanying tests and docs.

Changes

Intra-line Chunking for Oversize Content

Layer / File(s) Summary
Split-within-line logic and integration
src/openhuman/memory/chunker.rs
split_on_lines clamps effective chunk size and delegates oversized lines to split_within_line, which iteratively emits <=max_chars parts, preferring space breaks and ensuring valid UTF-8 boundaries.
Oversize-line chunking test suite
src/openhuman/memory/chunker.rs
Long-line test strengthened to assert multiple chunks and per-chunk size bounds; added tests for word-boundary preservation and hard-split lossless reassembly.

Regex Compilation Caching

Layer / File(s) Summary
Thread-local regex cache and helpers
src/openhuman/tokenjuice/reduce.rs
Added a REGEX_CACHE thread-local HashMap<String, Regex> and get_or_compile; updated regex_match, regex_replace, and regex_captures to use cached compiled regexes with previous fallback behavior on compile failure.

DNS Rebinding Protection

Layer / File(s) Summary
DNS validation function and documentation
src/openhuman/tools/impl/network/url_guard.rs
Module docs on DNS rebinding added and validate_url_with_dns_check implemented: validates URL, resolves hostname (when not an IP literal), errors on resolution failure/empty results, and rejects any private/local resolved IP.
DNS validation test coverage
src/openhuman/tools/impl/network/url_guard.rs
Added tests covering localhost blocking, public-domain pass with tolerant DNS failure handling, and rejection when the host resolves to a private/local IP.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

  • tinyhumansai/openhuman#1926 — Implements DNS-rebinding-aware validation (validate_url_with_dns_check) described by that issue.
  • tinyhumansai/openhuman#1939 — Implements the thread-local REGEX_CACHE and related helper changes described in that issue.

Suggested labels

rust-core

Suggested reviewers

  • senamakel
  • graycyrus

Poem

🐰 I hopped through lines both long and wild,
splitting words with care like a tidy child.
Regexes cached to speed the race,
DNS checked to guard the place.
Happy hops — the code's compiled!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly references all three main changes: chunker line splitting, DNS rebinding guard, and regex caching, matching the PR's stated objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

[!TIP]

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 16 '26 14:05 coderabbitai[bot]