Skip to content
Book a call

The regex that never matched because one backslash went missing

· 4-minute read · Engineering · by Direct Softworks

A regular expression compiled, ran, and matched nothing in a string that plainly contained its target. The source looked right. The file on disk did not say what the source said.

The regex was looking for a signature dictionary inside a PDF. Our contract module signs documents, and a signed PDF carries a marker that reads /Type /Sig. The pattern was /\/Type\s*\/Sig\b/, the string it ran against plainly contained /Type /Sig, and the match count was zero.

It compiled. It ran. It returned nothing. Every reading of the code said it should work, and every reading was correct, because the code we were reading was not the code on disk.

What was actually in the file

The file had been generated rather than typed: a script wrote it through a heredoc. Nothing about the line looked wrong until we printed it with cat -A, which renders control characters visibly:

/\/Type\s*\/Sig^H/

That ^H is byte 0x08, the backspace character. The two characters \b, which in a regex mean "word boundary", had arrived as one real backspace. The pattern now required a literal backspace after the word Sig, and no PDF has ever contained one there. It was a perfectly valid regex for a string that does not exist.

Why the shell was not the culprit

A quoted heredoc, <<'EOF', passes backslashes through untouched. That is what the quotes are for, and a POSIX shell honours it. The text went through more than one layer on its way to the shell, and one of those layers read it as a string literal and interpreted the escapes it recognised.

That last part is what makes this bug so good at hiding. Only the escapes the writer recognises are eaten. In a typical string-literal syntax, \b is backspace, \n is newline, \t is tab, \f and \v are form feed and vertical tab. But \s, \( and \/ mean nothing, so they survive with their backslash intact.

So in a single regex, \s and \/ were fine and \b was destroyed. The line looked half correct, and the surviving escapes argued that the transport was fine. You look at \s* sitting there exactly as written and conclude, reasonably and wrongly, that backslashes are getting through.

The same mechanism has produced the other classic symptom: a \n inside a JavaScript template literal arrived as a real line break and broke the parse. That one at least failed loudly.

The quiet version is worse

Later the same month it happened again, this time in a small check script. The regex silently matched nothing, and the test passed when it should have failed.

That is the version to be afraid of, and it is not bad luck. Any test that checks for the absence of something is satisfied by a regex that can never match: it finds nothing, so "nothing found" holds. The first incident cost some confusion. The second produced a green check that was not checking anything, and green checks are the ones nobody reads twice.

What we do now

Write files with a tool that writes bytes as given. For anything containing regexes, Windows paths or escape sequences, we write the file directly and edit it in place, rather than generating it through a shell command. If a file must be generated from a script, build backslashes explicitly (in Python, chr(92), or a raw string) instead of trusting a literal to pass through.

Sweep for control characters after generating source. Backspace, bell, vertical tab and form feed have no business in a .ts, .tsx or .mjs file. One line finds them:

grep -rlP '[\x07\x08\x0b\x0c]' --include='*.ts' --include='*.tsx' --include='*.mjs' .

Any output at all is a transport bug, not a style issue.

Give every regex test a string it must match. A pattern that is only ever tested against inputs it should reject cannot tell a correct regex from a broken one, because a broken regex rejects everything. One positive case turns the silent failure into a loud one.

When the code is right and the behaviour is wrong, look at the bytes. cat -A, xxd, or a hex view in the editor. The version of the signature check in the repository today spells its end boundary as a lookahead, (?![A-Za-z]), which also handles the fact that /Type /SigRef begins the same way. Whatever you write, the question is the same: does the file say what you think you wrote?

The wider point

Every layer that carries text can have opinions about it. Shells, string literals, JSON encoders, template engines, SQL clients. Each one is usually transparent, and the moment one of them is not, the failure looks like a logic bug in your code rather than a transport bug in the path to it.

It is the same lesson as a TEXT column that counts bytes rather than characters, and as the captcha key that was decided at build time: the thing you are reading is a representation, and the representation and the artefact can disagree. When the evidence and the source contradict each other, inspect the artefact before you doubt the logic.

More notes