Repository navigation
feat: add page context awareness to chat assistant - #19
Conversation
This feature allows the chat assistant to be aware of the page where the widget is embedded, enabling more contextually relevant answers. Changes: - Frontend: Capture page URL and title, send with API requests via page_context field. New includePageContext config option (default: true) - API: Add PageContext model and page_context field to AskRequest, pass through to HEDAssistant - Agent: Add PageContext dataclass, include in system prompt when provided, add fetch_page_content tool to retrieve page content when the user's question relates to the embedded page - Dependencies: Add markdownify for HTML-to-markdown conversion
Security improvements: - Add SSRF protection with IP/domain validation (blocks private IPs, localhost, link-local, and reserved ranges) - Validate redirect targets to prevent redirect-based SSRF - Disable automatic redirects and handle them manually with validation - Add URL scheme validation at API layer (max length, valid scheme) - Add title max length validation to prevent DoS - Validate content-type to ensure HTML responses only Tool design improvements: - Create bound fetch_current_page tool that only fetches the specific page URL from page_context (prevents LLM from requesting arbitrary URLs) - Move imports to module level for better performance Privacy improvements: - Add privacy notice in frontend widget when page context is enabled - Add configurable showPageContextNotice and pageContextNoticeText options Tests: - Add comprehensive tests for SSRF protection (37 new tests) - Test localhost, private IPs, link-local, reserved IPs are blocked - Test valid public URLs are allowed - Test PageContext model validation - Test HEDAssistant with and without page context
PR Review SummaryCritical Issues (3 found)1. Silent DNS Failure HandlingFile: except socket.gaierror:
pass # Allow, will fail at fetch timeFix: Return an error instead of silently passing - DNS failures should be treated as security-relevant events. 2. Broad Exception HandlersFile: except Exception as e:
return False, f"URL validation failed: {e}"Fix: Catch specific exceptions (ValueError, socket.error, etc.) to avoid masking bugs. 3. TOCTOU Race ConditionFile: Important Issues (3 found)1. Silent URL Scheme ConversionFile: 2. Missing LoggingIssue: Security-relevant events (blocked URLs, SSRF attempts) are not logged. 3. Missing Redirect SSRF TestsFile: Suggestions
Strengths
Recommended Action
|
- Fix silent DNS failure handling: return error instead of passing - Add specific exception handling for socket errors (gaierror, herror, timeout) - Add comprehensive logging for all security events - Make URL validation explicit: raise ValueError for invalid schemes - Return resolved IP from is_safe_url to mitigate TOCTOU attacks - Add comprehensive tests for redirect SSRF, content-type validation, timeout handling, and tool invocation - Achieve 87% coverage for page context code in hed.py
Manual Testing CompletedPerformed end-to-end testing of the page context awareness feature using the chat widget: Test Setup
Test ResultsPage Context Detection: PASSED
SSRF Protection: PASSED
Verification
|
Summary
This PR adds page context awareness to the HED chat assistant, allowing it to understand and reference the page where the user is asking questions from. This enables more contextually relevant answers when users are browsing HED documentation.
Changes:
Frontend: Capture and send page URL/title with API requests
includePageContextconfig option (default: true)showPageContextNoticeandpageContextNoticeTextoptionsAPI: Add
PageContextmodel with URL validationAgent: Add
fetch_current_pagetool and system prompt contextDependencies: Add
markdownifyfor HTML-to-markdown conversionSecurity Measures
Security Review Fixes
Addressed all findings from PR review:
Critical Issues (Fixed):
is_safe_urlfor use in fetchImportant Issues (Fixed):
Test plan
uv run pytest tests/- all 362 tests pass)