← Back to Pull requests

fix(web): harden csv-safe, seo, merchant-directory, and sold-count bounds (#848) #849

Closed
unknown closed this pull request without merging on Oct 24, 2026 12:46 UTC
View on GitHub
unknown unknown commented on Oct 24, 2026 12:41 UTC
Author

Closes #848

Summary


  • CSV Safe (pps/web/src/lib/csv-safe.ts): Strip non-printable control characters ([\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]+) before checking formula-injection prefixes in escapeCsvCell, cap cell length at 10_000, and cap columns (200) and rows (10_000) in uildSafeCsvContent.

  • SEO Helpers (pps/web/src/lib/seo.ts): Guard non-finite max and strip control characters in itTitle; block protocol-relative URLs (//, /\), embedded credentials, and non-http(s) protocols in bsoluteUrl.

  • Merchant Directory (pps/web/src/lib/merchant-directory.ts): Validate platform against /^[a-z0-9_-]{1,40}$/ and strip control characters on query in parseMerchantDirectoryFilters; cap limit at 500 in pageMerchantDirectory.

  • Sold Count (pps/web/src/lib/sold-count.ts): Reject overflowed historicalSold > 100_000_000, strip control characters and bound historicalSoldDisplay <= 40 chars, and canonicalize locale tags via Intl.getCanonicalLocales with fallback to 'vi-VN'.
  • ---

    <!-- dos-pr-summary -->

    📌 TL;DR


    This PR hardens multiple utility modules (seo, csv-safe, merchant-directory, sold-count) against input validation vulnerabilities, including protocol-relative URL injection, CSV formula injection, control character smuggling, and DoS vectors via excessive data sizes or invalid locale tags.

    🎯 Type of Change


  • 🚀 New feature

  • 🐛 Bugfix

  • 🧹 Refactor

  • ⚡ Performance

  • 📚 Documentation

  • ⚙️ CI / Configuration
  • 🔍 Changes Walkthrough


    | File | Summary of Changes |
    | :--- | :--- |
    | apps/web/src/lib/seo.ts | absoluteUrl: Added strict validation to reject protocol-relative URLs (//), credential-included URLs, non-HTTP(S) protocols, and cross-origin relative paths. fitTitle: Sanitized candidates by stripping control characters and normalizing whitespace; added bounds checking for the max parameter to prevent NaN/Infinity issues. |
    | apps/web/src/lib/csv-safe.ts | escapeCsvCell: Strips control characters (C0/C1) and truncates cells to 10,000 chars to prevent buffer bloat. buildSafeCsvContent: Enforces hard limits on columns (200) and rows (10,000) to mitigate DoS via massive CSV generation. |
    | apps/web/src/lib/merchant-directory.ts | parseMerchantDirectoryFilters: Sanitizes query by stripping control chars and normalizing whitespace. Validates platform against a strict regex (/^[a-z0-9_-]{1,40}$/) to prevent XSS/injection. pageMerchantDirectory: Caps the limit parameter at 500 to prevent excessive memory/CPU usage. |
    | apps/web/src/lib/sold-count.ts | formatSoldCount: Introduces MAX_DISPLAY_SOLD_COUNT (100M) to reject overflow values. Adds resolveSafeSoldLocale to validate and canonicalize locale tags, falling back to vi-VN for invalid inputs. Sanitizes display labels by stripping control characters. |
    | apps/web/src/lib/__tests__/seo.test.ts | Added test cases verifying the new security guards for absoluteUrl (phishing/credential injection), fitTitle (control chars/NaN), and CSV/merchant-directory bounds. |
    | apps/web/src/lib/__tests__/sold-count.test.ts | Added test cases for overflow rejection, control character stripping in labels, and invalid locale fallback. |

    📊 Architectural Flow


    flowchart TD
        subgraph Input_Layer [Untrusted Input]
            A[URL Path] --> B[absoluteUrl]
            C[CSV Data] --> D[buildSafeCsvContent]
            E[Query Params] --> F[parseMerchantDirectoryFilters]
            G[Sold Count/Locale] --> H[formatSoldCount]
        end
    
        subgraph Sanitization_Layer [Security & Validation]
            B --> B1{Protocol/Origin Check}
            B1 -->|Invalid| B2[Return SITE_URL]
            B1 -->|Valid| B3[Return Clean URL]
            
            D --> D1[Strip Control Chars]
            D1 --> D2[Truncate Cell > 10k]
            D2 --> D3[Limit Rows/Cols]
            
            F --> F1[Strip Control Chars]
            F1 --> F2[Regex Validate Platform]
            F2 --> F3[Cap Pagination Limit]
            
            H --> H1[Validate Locale]
            H1 --> H2[Check Overflow > 100M]
            H2 --> H3[Sanitize Label]
        end
    
        subgraph Output_Layer [Safe Output]
            B3 --> O1[SEO Metadata]
            D3 --> O2[Safe CSV File]
            F3 --> O3[Filtered Directory]
            H3 --> O4[Formatted Count]
        end
    

    <!-- /dos-pr-summary -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Improved page titles and links by cleaning invalid text and using a safe site URL when a link cannot be resolved.
    Made CSV exports more reliable by cleaning cell content, limiting oversized data, and preserving negative numeric values.
    Improved merchant directory searches and filters by cleaning query text, handling invalid platforms, and keeping page sizes within bounds.
    * Improved sold-count displays by handling invalid or unusually large counts and formatting labels and numbers more consistently across locales.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 636 lines (+477 / -159) across 7 files Reviewer: claude[bot] Review duration: 5m
    All Vouch review confidence checks passed
    Residual risk 0.31 = Change Risk (0.50) × (1 − Review Confidence (0.39)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 636 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 31% · Reviewer familiarity: 35%
    38%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #849"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 39.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.4% (MEDIUM) · Higher = larger blast radius
    50.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 39.0% · Lower = rushed or shallow review
    39.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.8% · Safe to merge
    30.8%
    Repository