Repository navigation
fix(input): sniff UTF-8/UTF-16LE/UTF-16BE BOMs in plan/steps/code/password readers #628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Herbicide7665
wants to merge
2
commits into
TestSprite:main
Choose a base branch
from
Herbicide7665:fix/463-bom-sniffing-reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { mkdtempSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { runCreateFromPlan, runLint } from './test.js'; | ||
| import { ApiError } from '../lib/errors.js'; | ||
|
|
||
| /** | ||
| * Integration tests for issue #463: the CLI's own docs suggest creating plan | ||
| * files with shell redirection, and Windows PowerShell 5.1's `>` writes | ||
| * UTF-16LE with a BOM. Every encoding the default Windows shell can produce | ||
| * must lint (and therefore create) cleanly. | ||
| */ | ||
| const PLAN = { | ||
| projectId: 'project_alice', | ||
| type: 'frontend', | ||
| name: 'checkout', | ||
| description: 'BOM encoding matrix', | ||
| planSteps: [{ type: 'action', description: 'navigate to home' }], | ||
| }; | ||
|
|
||
| type BomEncoding = 'utf8' | 'utf8-bom' | 'utf16le' | 'utf16be'; | ||
|
|
||
| function writeEncoded(dir: string, name: string, payload: string, enc: BomEncoding): string { | ||
| const path = join(dir, name); | ||
| let bytes: Buffer; | ||
| if (enc === 'utf8') { | ||
| bytes = Buffer.from(payload, 'utf8'); | ||
| } else if (enc === 'utf8-bom') { | ||
| bytes = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(payload, 'utf8')]); | ||
| } else if (enc === 'utf16le') { | ||
| bytes = Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(payload, 'utf16le')]); | ||
| } else { | ||
| const body = Buffer.from(payload, 'utf16le'); | ||
| body.swap16(); | ||
| bytes = Buffer.concat([Buffer.from([0xfe, 0xff]), body]); | ||
| } | ||
| writeFileSync(path, bytes); | ||
| return path; | ||
| } | ||
|
|
||
| describe('plan files saved with a BOM (PowerShell 5.1 `>`) lint cleanly', () => { | ||
| for (const enc of ['utf16le', 'utf16be', 'utf8-bom', 'utf8'] as const) { | ||
| it(`lints a ${enc} plan file with zero issues`, async () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'bom-plan-')); | ||
| const path = writeEncoded(dir, `plan-${enc}.json`, JSON.stringify(PLAN, null, 2), enc); | ||
| const report = await runLint( | ||
| { profile: 'default', output: 'json', debug: false, planFrom: path }, | ||
| ); | ||
| expect(report.checked).toBe(1); | ||
| expect(report.issues).toEqual([]); | ||
| }); | ||
| } | ||
|
|
||
| it('an odd-byte UTF-16BE plan file fails create with the typed encoding error, not a crash', async () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'bom-plan-')); | ||
| const body = Buffer.from(JSON.stringify(PLAN, null, 2), 'utf16le'); | ||
| body.swap16(); | ||
| const path = join(dir, 'plan-utf16be-odd.json'); | ||
| writeFileSync(path, Buffer.concat([Buffer.from([0xfe, 0xff]), body, Buffer.from([0x00])])); | ||
| writeFileSync( | ||
| join(dir, 'credentials'), | ||
| '[default]\napi_url = http://localhost:13502\napi_key = sk-user-test\n', | ||
| ); | ||
| const deps = { | ||
| credentialsPath: join(dir, 'credentials'), | ||
| fetchImpl: (() => { | ||
| throw new Error('unexpected network'); | ||
| }) as typeof fetch, | ||
| stdout: () => {}, | ||
| stderr: () => {}, | ||
| }; | ||
| // Plan parsing fires before any network; the typed localValidationError | ||
| // (not a raw ERR_INVALID_BUFFER_SIZE) is what reaches the caller. The | ||
| // human-readable text rides on nextAction (envelope message is generic). | ||
| const err: unknown = await runCreateFromPlan( | ||
| { profile: 'default', output: 'json', debug: false, planFrom: path }, | ||
| deps as never, | ||
| ).then( | ||
| () => null, | ||
| (e: unknown) => e, | ||
| ); | ||
| expect(err).toBeInstanceOf(ApiError); | ||
| expect(String((err as { nextAction?: string }).nextAction)).toContain('incomplete code unit'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { mkdtempSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { decodeWithBom, readTextFileBomAware } from './text-file.js'; | ||
|
|
||
| const PAYLOAD = '{"projectId":"project_alice","type":"frontend","name":"checkout"}'; | ||
|
|
||
| function encoded(payload: string, enc: 'utf8' | 'utf8-bom' | 'utf16le' | 'utf16be'): Buffer { | ||
| if (enc === 'utf8') return Buffer.from(payload, 'utf8'); | ||
| if (enc === 'utf8-bom') return Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(payload, 'utf8')]); | ||
| if (enc === 'utf16le') return Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(payload, 'utf16le')]); | ||
| const body = Buffer.from(payload, 'utf16le'); | ||
| body.swap16(); | ||
| return Buffer.concat([Buffer.from([0xfe, 0xff]), body]); | ||
| } | ||
|
|
||
| describe('decodeWithBom', () => { | ||
| for (const enc of ['utf8', 'utf8-bom', 'utf16le', 'utf16be'] as const) { | ||
| it(`decodes ${enc}`, () => { | ||
| expect(decodeWithBom(encoded(PAYLOAD, enc))).toBe(PAYLOAD); | ||
| }); | ||
| } | ||
|
|
||
| it('passes BOM-less UTF-8 through unchanged', () => { | ||
| const raw = Buffer.from(PAYLOAD, 'utf8'); | ||
| expect(decodeWithBom(raw)).toBe(PAYLOAD); | ||
| expect(decodeWithBom(raw)).toBe(raw.toString('utf8')); | ||
| }); | ||
|
|
||
| it('handles an empty buffer', () => { | ||
| expect(decodeWithBom(Buffer.alloc(0))).toBe(''); | ||
| }); | ||
| }); | ||
|
|
||
| describe('readTextFileBomAware', () => { | ||
| it('reads a UTF-16LE file written by PowerShell 5.1 `>` back intact', () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'text-file-')); | ||
| const path = join(dir, 'plan.json'); | ||
| writeFileSync(path, encoded(PAYLOAD, 'utf16le')); | ||
| expect(readTextFileBomAware(path)).toBe(PAYLOAD); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { readFileSync } from 'node:fs'; | ||
|
|
||
| /** | ||
| * Decode a buffer that may start with a Unicode BOM: UTF-8 (EF BB BF), | ||
| * UTF-16LE (FF FE) or UTF-16BE (FE FF). BOM-less input decodes as UTF-8. | ||
| * | ||
| * Why: Windows PowerShell 5.1's `>` redirection (the default shell on | ||
| * Windows 10/11) writes UTF-16LE with a BOM, and this CLI's own documentation | ||
| * suggests exactly that redirection for creating plan files. Readers that | ||
| * decode UTF-8 unconditionally turn the CLI's own template into garbage. | ||
| */ | ||
| export function decodeWithBom(bytes: Buffer): string { | ||
| if (bytes.length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf) { | ||
| return bytes.subarray(3).toString('utf8'); | ||
| } | ||
| if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xfe) { | ||
| return bytes.subarray(2).toString('utf16le'); | ||
| } | ||
| if (bytes.length >= 2 && bytes[0] === 0xfe && bytes[1] === 0xff) { | ||
| // Buffers have no 'utf16be' decoder: swap the byte pairs, then decode LE. | ||
| // An odd trailing byte is an incomplete code unit — reject it here instead | ||
| // of letting swap16() throw an untyped ERR_INVALID_BUFFER_SIZE. | ||
| const body = bytes.subarray(2); | ||
| if (body.length % 2 !== 0) { | ||
| throw new Error('UTF-16BE input ends with an incomplete code unit'); | ||
| } | ||
| const be = Buffer.from(body); | ||
| be.swap16(); | ||
| return be.toString('utf16le'); | ||
| } | ||
| return bytes.toString('utf8'); | ||
| } | ||
|
|
||
| /** | ||
| * Read a small text file, sniffing and stripping UTF-8 / UTF-16LE / UTF-16BE | ||
| * BOMs. BOM-less files decode as UTF-8, byte-for-byte unchanged from | ||
| * `readFileSync(path, 'utf8')`. | ||
| */ | ||
| export function readTextFileBomAware(path: string): string { | ||
| return decodeWithBom(readFileSync(path)); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.