Skip to content

analyzer, cmd/capslock-git-diff: escape control characters in callpath filenames#300

Merged
jcd2 merged 1 commit into
google:mainfrom
MusGaas:sanitize-callpath-filenames
Apr 28, 2026
Merged

analyzer, cmd/capslock-git-diff: escape control characters in callpath filenames#300
jcd2 merged 1 commit into
google:mainfrom
MusGaas:sanitize-callpath-filenames

Conversation

@MusGaas
Copy link
Copy Markdown
Contributor

@MusGaas MusGaas commented Apr 27, 2026

Fixes #299.

token.Position.Filename can be set to an arbitrary string by //line and
/*line ...*/ directives in analyzed Go source. capslock copies that string
into cpb.Function_Site.Filename (after path.Base) and writes it to terminal
sinks unchanged, so a directive containing ANSI escape bytes or tabs reaches
the user's terminal.

This change escapes control characters at the three terminal-output sinks:

  • analyzer/compare.go printCallPath (-output=compare)
  • analyzer/static/verbose.tmpl (-output=v/-output=verbose), via a new
    escape entry in templateFuncMap
  • cmd/capslock-git-diff/main.go printCallPath

The escape helper uses strconv.Quote with the surrounding "s trimmed off,
matching the existing helper in cmd/capslock/capslock.go (used since version
output was added). Legitimate Go source filenames pass through unchanged;
only non-printable bytes are escaped.

The JSON output path is not touched: protojson already escapes control
bytes, and machine consumers should keep receiving raw filenames.

cmd/capslock-git-diff does not import analyzer, so the helper is
duplicated locally rather than exported from analyzer.

Test

analyzer/util_test.go adds a unit test for escapeControlChars covering
plain filenames, filenames with spaces, ANSI CSI bytes, embedded tabs, and
BEL. It also asserts that no C0 byte or DEL remains in the output for any
case. Existing tests continue to pass.

…h filenames

token.Position.Filename can be set to an arbitrary string by //line and
/*line ...*/ directives in analyzed Go source. capslock copies that
string into terminal output unmodified, allowing crafted source files
to emit ANSI escape sequences (CSI, OSC 8 hyperlinks, OSC 52 clipboard,
cursor manipulation) when scanned.

Apply strconv.Quote with surrounding quotes trimmed to filenames in the
three terminal-oriented sinks (verbose output, default callpath
rendering, capslock-git-diff output). Mirrors the existing
sanitization pattern in cmd/capslock/capslock.go. JSON output via
protojson is already safe.

Adds analyzer/util_test.go covering the escape behavior.
@jcd2 jcd2 self-requested a review April 28, 2026 12:47
Copy link
Copy Markdown
Collaborator

@jcd2 jcd2 left a comment

Choose a reason for hiding this comment

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

Nicely done, thanks!

This will make path separators on Windows double-backslashes, but that's ok for now.

@jcd2 jcd2 merged commit 6e08783 into google:main Apr 28, 2026
4 checks passed
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.

capslock terminal output does not escape ANSI control sequences from //line directives

2 participants