Files
Jamie Tanna c985ca29dd fix(util): don't log "Hidden Unicode" warning when reading a binary file (#41470)
In the case that we are reading binary files in repositories, we should
not log a WARN that there are "hidden" characters in it, as it's a
binary file.

We can use a common heuristic (at least according to Claude Sonnet 4.5)
to look for null bytes at the beginning of our files, and not log the
warning in that case.

Co-authored-by: Claude Sonnet 4.5 <jamie.tanna+github-copilot@mend.io>
2026-02-25 08:36:16 +00:00

56 lines
2.1 KiB
TypeScript

import { logger } from '../logger/index.ts';
import { hiddenUnicodeCharactersRegex, toUnicodeEscape } from './regex.ts';
function isBinaryContent(content: Buffer): boolean {
// Check for null bytes in the first 8KB - a common indicator of binary content
const sampleSize = Math.min(content.length, 8192);
for (let i = 0; i < sampleSize; i++) {
if (content[i] === 0) {
return true;
}
}
return false;
}
export function logWarningIfUnicodeHiddenCharactersInPackageFile(
file: string,
content: string | Buffer,
): void {
const buffer = Buffer.isBuffer(content) ? content : Buffer.from(content);
const hiddenCharacters = buffer
.toString('utf8')
.match(hiddenUnicodeCharactersRegex);
if (isBinaryContent(buffer) && hiddenCharacters) {
logger.trace(
{
file,
hiddenCharacters: toUnicodeEscape(hiddenCharacters.join('')),
},
`Hidden Unicode characters discovered in file \`${file}\`, but not logging higher than TRACE as it appears to be a binary file`,
);
return;
}
if (hiddenCharacters) {
if (hiddenCharacters.length === 1 && hiddenCharacters[0] === '\uFEFF') {
logger.once.trace(
{
file,
hiddenCharacters: toUnicodeEscape(hiddenCharacters.join('')),
},
`Hidden Byte Order Mark (BOM) Unicode characters has been discovered in the file \`${file}\`. This is likely safe, if you are using Microsoft Windows, but please confirm that they are intended to be there, as they could be an attempt to "smuggle" text into your codebase, or used to confuse tools like Renovate or Large Language Models (LLMs)`,
);
} else {
logger.once.warn(
{
file,
hiddenCharacters: toUnicodeEscape(hiddenCharacters.join('')),
},
`Hidden Unicode characters have been discovered in file(s) in your repository. See your Renovate logs for more details. Please confirm that they are intended to be there, as they could be an attempt to "smuggle" text into your codebase, or used to confuse tools like Renovate or Large Language Models (LLMs)`,
);
}
}
}