Skip to content

Fix catastrophic backtracking in copyright detection - #5591

Open
AKSHAYUJJWAL286 wants to merge 1 commit into
aboutcode-org:developfrom
AKSHAYUJJWAL286:fix-4223-copyright-timeout
Open

AKSHAYUJJWAL286 wants to merge 1 commit into
aboutcode-org:developfrom
AKSHAYUJJWAL286:fix-4223-copyright-timeout

Conversation

@AKSHAYUJJWAL286

Copy link
Copy Markdown

Summary

This PR addresses severe performance issues in ScanCode Toolkit's copyright detection caused by catastrophic regex backtracking.

Changes Made

  • Replaced the backtracking YR copyright regex matcher with a deterministic linear-time matcher.
  • Replaced the backtracking EMAIL regex matcher with a deterministic linear-time matcher.
  • Added regression tests covering valid inputs, invalid inputs, and adversarial cases.
  • Updated CHANGELOG.rst.
  • Added the contributor entry to AUTHORS.rst.

Testing and Validation

  • Full tests/cluecode/ suite: 5,190 passed, 15 xfailed (previous run).
  • tests/cluecode/test_copyrights_basic.py: 66 passed.
  • Copyright doctests: 4 passed.
  • Tested three Brotli-related files:
    • dictionary.bin
    • decode.js
    • decode.min.js
  • All nine ScanCode CLI test runs completed successfully with no scan errors.
  • git diff --check passed.
  • No new Ruff issues were introduced. Two pre-existing D301 findings remain in untouched code.

Scope and Limitations

The original approximately 103 MB binary mentioned in the issue was not available for direct testing. The shared Brotli-related reproducer files were tested successfully.

Related Issue

References #4223

Contributor Checklist

  • Changes include regression tests.
  • Updated CHANGELOG.rst.
  • Added contributor information to AUTHORS.rst.
  • Changes are committed in a dedicated feature branch.
  • PR has a descriptive title and references the related issue.

Signed-off-by: AKSHAY UJJWAL <ujjwalakshay201@gmail.com>
@knowprasad

Copy link
Copy Markdown

I reviewed the changes in src/cluecode/copyrights.py, ests/cluecode/test_copyrights_basic.py, CHANGELOG.rst, and AUTHORS.rst.

Replacing the backtracking YR and EMAIL lexer regexes with explicit linear-time matchers cleanly eliminates ReDoS timeouts on long/adversarial tokens while keeping lexer token classification intact.

A few specific observations from inspecting the implementation and tests:

  1. NFA-State DP Equivalence in is_year_or_year_range:
    Tracking �efore_year and has_year state arrays across _advance_year_punct, _advance_year_or_range, and _is_short_year preserves the exact grammar of the three prior YR patterns (_PUNCT prefixes, _YEAR_YEAR separators including 1960-_99 via _PUNCT + _is_short_year, and 20xx-203x / 20xx-2039a suffixes) in (n)$ time with an early '19' not in value and '20' not in value fast-reject.
  2. Deterministic Scanning in is_email:
    Stripping trailing [>).,], validating the 2- or 3-letter TLD position first to pin dot_idx, and only advancing past [A-Za-z][<()] when '<' or '(' is actually present (else: start = 0) eliminates backtracking over overlapping alpha/alnum prefixes while matching both @ and �t separators.
  3. Reference Parity & Adversarial Tests:
    Testing against the original compiled regexes (_REF_YR_SCRIPT, _REF_EMAIL_SCRIPT) in subprocesses alongside adversarial 2,000+ character inputs provides strong regression protection against grammar drift.

Solid fix for #4223.

@AKSHAYUJJWAL286

Copy link
Copy Markdown
Author

Thank you for taking the time to review the implementation and tests!

I really appreciate the detailed feedback, especially your observations on preserving YR grammar equivalence, deterministic email matching, and adversarial test coverage.

I'm glad the changes address the timeout issue. I'll be happy to address any further feedback on PR #5591.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants