Skip to content
Open
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
fix catastrophic backtracking in _ansi_escape_pat OSC 8 params
The params group (\w+=\w+:?)* lets adjacent key=value pairs abut
without a colon, so a run of word characters between two '=' can be
split in many ways and an unterminated OSC 8 sequence such as
"\x1b]8;a=" + "aaaa=" * 18 backtracks exponentially. tabulate() runs
this regex over every cell, so 96 bytes of input take ~30 s.

Require colon-separated pairs, as in the OSC 8 spec. Matching becomes
linear, the capture groups stay the same, and well-formed params match
as before.
  • Loading branch information
Chandan3456 committed Nov 7, 2026
commit bfaa8aac8b26a298986573830247aa47864faf80
2 changes: 1 addition & 1 deletion tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ def escape_empty(val):
|
# terminal hyperlinks
{_osc}8; # OSC opening
(\w+=\w+:?)* # key=value params list (submatch 2)
(\w+=\w+(?::\w+=\w+)*:?)? # key=value params list (submatch 2)
; # delimiter
([^{_esc}]+) # URI - anything but ESC (submatch 3)
{_st} # ST
Expand Down
12 changes: 12 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Regression tests."""

import time

from tabulate import DataRow, Line, TableFormat, tabulate

from common import assert_equal, skip
Expand Down Expand Up @@ -598,3 +600,13 @@ def test_github_escape_pipe_character():
result = tabulate([["foo|bar"]], headers=("spam|eggs",), tablefmt="github")
expected = "| spam\\|eggs |\n|:------------|\n| foo\\|bar |"
assert_equal(expected, result)


def test_osc8_link_params_no_catastrophic_backtracking():
"Regression: unterminated OSC 8 link params must not backtrack exponentially"
cell = "\x1b]8;a=" + "aaaa=" * 16
start = time.perf_counter()
result = tabulate([[cell]], tablefmt="plain")
elapsed = time.perf_counter() - start
assert_equal(cell, result)
assert elapsed < 1.0