generator icon indicating copy to clipboard operation
generator copied to clipboard

[FEATURE] Flag PR diffs with off-screen whitespace padding, invisible Unicode, or extreme line lengths

Open devxdh opened this issue 1 week ago • 2 comments

Why do we need this improvement?

Goal & scope: This check is for diff visibility, not malware scanning. Its only job is to make sure added code is actually visible to reviewers in GitHub's diff view rather than pushed off-screen or masked with invisible characters. Normal-width, multi-line code sits in plain sight for human reviewers, CodeRabbit, and SonarCloud to handle.

In the recent PolinRider incident (#2277 and #2278), the attacker hid an obfuscated loader in api.js and 14 jest.config.js files by putting hundreds of tabs at the start of a line. In GitHub's diff view, the code was pushed off-screen and the added line looked empty.

Linters and review bots alone leave a gap here:

  1. npm run lint (turbo lint) only runs inside workspace packages (apps/*, packages/**), so root-level files like api.js are never linted.
  2. Bots like CodeRabbit flag obfuscated code, but a PR author with bad intent can click "Resolve conversation" to collapse and hide those comments before a reviewer sees them.

A read-only CI check keeps off-screen padding and invisible characters visible as a failed status check on the PR bar so it cannot be resolved away.

How will this change help?

  • Catches off-screen visual padding (tabs, spaces, mixed tab/space runs, blank-rendering Unicode), invisible/BiDi Unicode (Trojan Source), and multi-kilobyte single-line additions across all changed files in a PR, including root files, configs, and Jest .snap files.
  • Runs on pull_request_target using only pulls.listFiles with no actions/checkout and no write permissions (pull-requests: read only), so it runs safely on fork PRs even before a maintainer approves CI or pulls the branch locally.

Screenshots

No response

How could it be implemented/designed?

Add a standalone, read-only workflow (for example, .github/workflows/check-hidden-diff.yml) on pull_request_target (opened, reopened, synchronize) with permissions: {} at root, pull-requests: read on the job, and # zizmor: ignore[dangerous-triggers] (no checkout, no ${{ }} interpolation in JS).

Why visual column width (100 cols) + 3,000 chars + blank Unicode?

Checking all 596 tracked files in this repo gives these baselines:

  • Most consecutive tabs in a row: 4 (32 visual columns at 8-col GitHub tab stops)
  • Most consecutive spaces in a row: 65 in auto-generated README.md template tables, 33 in code/YAML/configs
  • Invisible / BiDi / blank-rendering Unicode chars: none found
  • Longest line (including package-lock.json and .snap): 1,449 chars

Instead of raw tab/space counts (which can be bypassed by interleaving 14 tabs + 1 space + 14 tabs for 224+ blank columns), we measure visual column width of each whitespace run (\t = 8 columns, any other whitespace = 1 column) and flag runs >= 100 columns:

  • Catches 13+ tabs, 100+ spaces, and mixed \t + space runs cleanly, while staying safely above README.md tables (65 cols) and code (32–33 cols).
  • Also flags invisible/BiDi/blank-rendering Unicode (\u202A-\u202E, \u2066-\u2069, \u200B-\u200C, \u2060, \uFEFF, \u2800, \u3164, \u180E, \u00AD, etc., keeping \u200D out so markdown emojis still work).
  • Flags added lines over 3,000 chars (>2× the repo's 1,449-char max) to catch tabless minified one-liners.
  • Logs core.warning when GitHub omits file.patch on >300KB diffs instead of silently skipping or building a blob-diffing engine.

Example script (actions/github-script)

// measure visual width (github renders tabs up to 8 cols) so interleaved tabs + spaces can't bypass
const MAX_WS_COLUMNS = 100;
const MAX_LINE_LENGTH = 3000;

// catch bidi overrides (trojan source), zero-width chars, and blank-rendering fillers
// (excludes \u200D so compound emojis in markdown docs still work)
const invisibleRe = /[\u00AD\u034F\u180E\u200B\u200C\u200E\u200F\u202A-\u202E\u2060\u2066-\u2069\u2800\u3164\uFEFF\uFFA0\u061C]/;

function hasExcessivePadding(line) {
  for (const run of line.matchAll(/[^\S\r\n]+/g)) {
    let cols = 0;
    for (const ch of run[0]) cols += ch === '\t' ? 8 : 1;
    if (cols >= MAX_WS_COLUMNS) return true;
  }
  return false;
}

const files = await github.paginate(github.rest.pulls.listFiles, {
  owner: context.repo.owner,
  repo: context.repo.repo,
  pull_number: context.payload.pull_request.number,
  per_page: 100,
});

const findings = [];

for (const file of files) {
  if (!file.patch) {
    if (file.status !== 'removed') {
      core.warning(`Patch omitted by GitHub for ${file.filename}; inspect manually if non-binary.`);
    }
    continue;
  }

  for (const line of file.patch.split('\n')) {
    if (!line.startsWith('+') || line.startsWith('+++')) continue;
    const content = line.slice(1);

    if (content.length > MAX_LINE_LENGTH || hasExcessivePadding(content)) {
      findings.push(`${file.filename}: excessive line length or whitespace padding`);
      break;
    }

    if (invisibleRe.test(content)) {
      findings.push(`${file.filename}: invisible, blank-filler, or BiDi Unicode character`);
      break;
    }
  }
}

if (findings.length > 0) {
  core.setFailed(`Suspicious diff formatting found:\n- ${findings.join('\n- ')}`);
}

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

None

devxdh avatar Nov 26 '26 13:11 devxdh

@devxdh thanks for the research here. I ran the example script from this issue against some crafted added lines to see what gets through. It catches the attack as it happened (A), but the other cases below pass. code stands for any hidden code.

Added line Hidden code starts at column Result
A 315 tabs, then code (the real attack) ~2520 caught
B ++i; + 315 tabs, then code ~2520 missed
C 12 tabs + U+200D (zero-width joiner), repeated 26 times, then code ~2496 missed
D 12 tabs + a carriage return, repeated 26 times, then code ~2496 missed
E 12 tabs + U+2063 (invisible separator), repeated 26 times, then code ~2496 missed
F a normal ~350-character line, then code, no padding ~349 missed
G 96 characters of real code + 12 tabs, then code ~192 missed
H 99 x U+3000 (ideographic space), then code ~198 missed
I a file for which the API returns no patch n/a check passes

Why each one gets through

1. Added lines that start with ++ are never scanned (B). The loop skips lines starting with +++ to ignore the diff file header. But the patch from pulls.listFiles has no file header, it starts at the @@ line (I checked on #2337). So an added line whose content starts with ++ arrives as +++... and is skipped. B is the original attack with 4 extra characters.

2. A file with no patch passes (I). When file.patch is missing the script only calls core.warning, so the check stays green and the warning sits in the run log. The PR author controls how big a file's diff is and whether the file looks binary, so they can choose this path. I could not confirm GitHub's exact size limit: none of the last 40 merged PRs here had a file without a patch.

3. A gap can be split with an invisible character that is not on the list (C, D, E). Width is measured per whitespace run, so one non-whitespace invisible character every 12 tabs keeps each run at 96 columns. U+200D is left out of the list on purpose (emojis) and works as the separator. A carriage return is excluded from the run regex and is not on the list either. In total, 152 of the 170 Unicode format characters (category Cf) are not on the list, and neither are the non-whitespace control characters. I did not check how GitHub draws each of these in the diff view.

4. No padding is needed at all (F, G). The rule measures the size of a gap, not where the code ends up on screen. Anything past roughly column 150-200 (depending on screen width) is already off-screen in the diff view, and the length limit is 3,000 characters. So code at the end of a long normal line passes, and so does real code followed by a 96-column gap. Long lines are not unusual here: 96 code/config lines in the repo are already wider than 200 columns (the widest is 585).

5. Wide blank characters count as 1 column (H). U+3000 is whitespace, so 99 of them count as 99 columns. It is a full-width character, so if GitHub draws it that way the gap on screen is about twice as wide.

Two limits outside the script

  • The failed check belongs to a commit. If the author force-pushes back to clean code, the red check goes away with that commit, the same way the CodeRabbit comment became outdated. So "cannot be resolved away" holds only together with the force-pushed label from #2337.
  • Variants with no padding are not flagged, for example the .vscode/tasks.json task plus fake font file used on CircuitVerse. I know the issue puts this out of scope, I am only noting it.

How easy is it to get around?

Easy, as written. The workflow is public, so someone can read the rules, run the same script locally as many times as they want before opening a PR, and they need only one gap. It stops a repeat of the exact same payload, but not someone who reads the workflow first.

Adi-204 avatar Nov 26 '26 14:11 Adi-204

@Adi-204 thank you. I completely agree and acknowledge that public rules here are too easy for an attacker to adapt around, and even if we take every trick into account its just not worth the effort.

devxdh avatar Nov 26 '26 14:11 devxdh