Skip to content

Shebang matching is unanchored substring search: canonical tclsh/wish/ts-node shebangs trigger false Identity Conflicts (Tier 5) #3116

Closed

Description

@squid-protocol

Found by a design review of the language-detection cascade (gitgalaxy/standards/language_lens.py), with every case below reproduced against origin/main (2fa685e) by running the real detector: LanguageDetector(LANGUAGE_DEFINITIONS, {}).inspect(path, src).

Mechanism

_tier_2_fingerprint_check (gitgalaxy/standards/language_lens.py:682-691, the "Standard Executable Shebang Check" block) tests membership, not identity:

if content.startswith("#!"):
    first_line = content.split("\n", 1)[0].lower()
    for lang_id, data in self.languages.items():
        for trigger in data.get("shebangs", []):
            if trigger in first_line:      # <-- unanchored substring match
                return lang_id

So shell's "sh" trigger matches any shebang line containing the two characters sh, and javascript's "node" matches ts-node. It is also first-match-wins over self.languages.items(), so registry order silently picks which wrong answer you get.

The wrong shebang language then disagrees with the (correct) extension, the Identity Conflict Trap fires (language_lens.py:351-370), and the file is forced to Tier 5 "Absolute Distrust".

Verified failing cases

Reproduced on origin/main. Every one returns lang_id="undeterminable", intensity=0.0, lock_tier=5:

file shebang expected source_proof
hello.tcl #!/usr/bin/tclsh tcl Identity Conflict (tcl != shell)
gui.tcl #!/usr/bin/wish tcl Identity Conflict (tcl != shell)
j.tcl #!/usr/bin/env jimsh tcl Identity Conflict (tcl != shell)
s.swift #!/usr/bin/env swift-sh swift Identity Conflict (swift != shell)
r.scm #!/usr/bin/env racketsh scheme Identity Conflict (scheme != shell)
t.ts #!/usr/bin/env ts-node typescript Identity Conflict (typescript != javascript)

#!/usr/bin/tclsh and #!/usr/bin/wish are the canonical Tcl and Tcl/Tk shebangs. ts-node is a mainstream modern TypeScript case — this is not an exotic-language problem.

Each also carries an anomaly_flags entry, e.g. Identity Masking: Extension (tcl) vs Shebang (shell).

Controls that still pass: ok.sh + #!/bin/bash -> shell, intensity 0.999. p.ps1 + #!/usr/bin/pwsh -> powershell, intensity 0.999 — but note that one passes by accident of registry order, not by design: powershell sits at index 10 and shell at index 11, which is the only reason sh inside pwsh doesn't win.

Why this is worse than a plain misdetection

The Identity Conflict Trap converts a misdetection into two compounding harms:

  1. Total exclusion from analysis — the file contributes nothing, and no per-language rule runs on it.
  2. A reported security anomaly that reads as identity masquerading.

A false positive here does not look like a classification error to the user. It looks like a malware finding on their legitimate Tcl script. That is the worst available failure mode, and it is why this is filed as a bug at suggested priority: high rather than as detection polish.

Full latent collision set

From a sweep of every shebang trigger against every other on origin/main:

short trigger is a substring of current winner
sh (shell) tclsh, wish, jimsh (tcl) shell — wrong
sh (shell) racketsh (scheme) shell — wrong
sh (shell) swift-sh (swift) shell — wrong
sh (shell) jshell (java) java (idx 3 < 11) — right, by ordering
sh (shell) pwsh, powershell (powershell) powershell (idx 10 < 11) — right, by ordering
node (javascript) ts-node (typescript) javascript — wrong
python (python) micropython (embedded_python) python — wrong
go (go) cargo, cargo-script (rust) go — wrong

Eight of the eleven collisions currently resolve wrongly; the three that are right are right only because of where they happen to sit in the registry.

Suggested direction (record, do not pre-decide)

Match the shebang's executable basename as a token rather than as a substring: parse the interpreter path (including the env <prog> form, and trailing args/flags) and compare the basename for equality against the trigger list, longest-trigger-first or exact-match-only.

Note the constraint that rules out naive equality: some triggers are legitimately prefixes of real interpreter names — python must still match python3 and python3.12. A basename regex of the shape ^<trigger>[0-9.]*$ covers python3/python3.12 while correctly rejecting micropython and tclsh.

As defence in depth, also consider whether the Identity Conflict Trap should require both signals to be high-quality before dumping a file to Tier 5.

The golden crucible cannot catch this class

I swept the pinned language-crucible corpus for tclsh, wish, jimsh, ts-node, swift-sh, racketsh and micropython shebangs: zero files for all seven. No golden-master diff will ever fire on this, which is part of the argument for #3117. Seed a synthetic shebang matrix as the regression test instead.

Related

Sibling findings from the same review: #3117 (detection-accuracy harness — this defect class is invisible to every existing gate) and #3118 (registry-order arbitration, of which the shebang first-match-wins loop is one half). Detection-hardening sibling: #3110 (EQU-only .asm copybooks misclassified via ecosystem gravity) — same subsystem, opposite tier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginepriority: high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions