Skip to content

fix: strip control characters from file names in posix builtins (3.x backport) - #2240

Merged
gnodet merged 3 commits into
jline-3.xfrom
backport/3.x/strip-control-chars-filenames
Sep 8, 2026
Merged

gnodet merged 3 commits into
jline-3.xfrom
backport/3.x/strip-control-chars-filenames

Conversation

@gnodet

@gnodet gnodet commented Sep 7, 2026

Copy link
Copy Markdown
Member

Backport of 92cb769 from master to jline-3.x.

Wraps filesystem-derived names with stripControlChars() before writing to the terminal, preventing escape injection via crafted filenames. An entry such as report<ESC>]0;pwned<BEL>.txt would otherwise drive the terminal (set window title, write clipboard via OSC 52, etc.).

Affected commands: ls, head, tail, wc, grep.

Manually adapted (not cherry-picked) because the 3.x posix builtins differ significantly from master — the wc, head, tail, and grep implementations were refactored on master. Includes the full test suite from master.

Backport of 92cb769 from master. Wraps filesystem-derived names
(file names, symlink targets, head/tail headers, grep prefixes, ls
multi-dir headers) with stripControlChars() to prevent terminal
escape injection via crafted filenames.

(cherry picked from commit 92cb769)
@gnodet gnodet added the bug label Sep 7, 2026
@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 843f9d03-cdf5-4bbc-8623-127667201463

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

@gnodet gnodet left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security fix is correctly scoped and well-implemented. One gap: the tail command's multi-file header is patched but has no regression test.

What the fix does: stripControlChars() uses Character.isISOControl() which covers C0 (0x00–0x1F) and C1 (0x7F–0x9F) control characters — correct set for terminal injection defense. All 9 filesystem-derived name output sites in the 3.x PosixCommands are covered: wc (1), head (1), tail (1), grep (3), ls display+symlink (2), ls multi-dir header (1).

The gap: The tail multi-file header (==> arg <==) is patched (diff hunk at line 1190), but there is no corresponding tailStripsControlCharsInFileHeader regression test. The other five commands each have at least one test; tail is missing.

Suggested addition to PosixCommandsControlCharTest:

@Test
void tailStripsControlCharsInFileHeader() throws Exception {
    Path evil = tempDir.resolve(EVIL);
    Files.write(evil, "hello\n".getBytes(StandardCharsets.UTF_8));
    Path plain = tempDir.resolve("plain.txt");
    Files.write(plain, "world\n".getBytes(StandardCharsets.UTF_8));
    // More than one source makes tail emit the "==> name <==" headers.
    PosixCommands.tail(context, new String[] {"tail", evil.toString(), plain.toString()});
    assertNoControlBytes(output());
}

This review was generated by an AI agent, Hermès, on behalf of @gnodet.

@gnodet

gnodet commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

Added tailStripsControlCharsInFileHeader regression test in 9496133. All 7 control-char tests pass. ✅

@sonarqubecloud

sonarqubecloud Bot commented Sep 7, 2026

Copy link
Copy Markdown

@gnodet
gnodet merged commit 2c6f1ff into jline-3.x Sep 8, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant