Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/__tests__/tcp-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,95 @@ describe('TCP Authentication with Firmware Version', () => {
expect(metrics2?.firmwareVersion).toBe('v2.1.0');
});
});

describe('Hex Dump Safety (opcode 0x09)', () => {
it('should structure 0x09 packet with version bytes after fixed-size prefix', () => {
const challenge = Buffer.from('1234567890abcdef1234567890abcdef', 'hex');
const firmwareVersion = 'test-v1.0';
const authMessage = buildAuthPacket(OPCODE_AUTH_WITH_VERSION, challenge, firmwareVersion);

// buildAuthPacket returns: addr(20) + sig(65) + versionLen(1) + versionBytes
// When sent on wire, opcode(1) is prepended: opcode + addr(20) + sig(65) + versionLen(1) + versionBytes
expect(authMessage.length).toBe(20 + 65 + 1 + firmwareVersion.length);

const versionLen = authMessage[85];
expect(versionLen).toBe(firmwareVersion.length);

const versionBytes = authMessage.subarray(86, 86 + versionLen);
expect(versionBytes.toString('utf-8')).toBe(firmwareVersion);
});

it('should build valid 0x09 packet with version containing non-ASCII characters', () => {
const challenge = Buffer.from('1234567890abcdef1234567890abcdef', 'hex');
// Version with non-ASCII/control characters that would garble terminal
const firmwareVersion = 'v1\x00\x01\x02test';
const authMessage = buildAuthPacket(OPCODE_AUTH_WITH_VERSION, challenge, firmwareVersion);

// The first 85 bytes (addr + sig) contain only hex-safe data
const safePrefix = authMessage.subarray(0, 85);
const safePrefixHex = safePrefix.toString('hex');
expect(safePrefixHex.length).toBe(170); // 85 bytes = 170 hex chars

// Version bytes are after byte 85 (versionLen at byte 85)
const versionLen = authMessage[85];
expect(versionLen).toBe(firmwareVersion.length);

// Verify the problematic bytes are only in the version section
const versionBytes = authMessage.subarray(86, 86 + versionLen);
expect(versionBytes.toString('utf-8')).toBe(firmwareVersion);
});

it('should verify TCPClient would dump only safe bytes for 0x09', () => {
const challenge = Buffer.from('1234567890abcdef1234567890abcdef', 'hex');
const firmwareVersion = 'version-with-\x00\x01-controls';
const authMessage = buildAuthPacket(OPCODE_AUTH_WITH_VERSION, challenge, firmwareVersion);

// On wire, full packet is: opcode(1) + authMessage
// TCPClient receives buffer with opcode prepended
const wirePacket = Buffer.concat([Buffer.from([OPCODE_AUTH_WITH_VERSION]), authMessage]);

// TCPClient dumps first 86 bytes for opcode 0x09 (opcode + addr + sig)
const dumpBytes = wirePacket.subarray(0, 86);
const hexDump = dumpBytes.toString('hex');

// Hex dump should be pure hex (no control characters in the string representation)
expect(hexDump).toMatch(/^[0-9a-f]+$/);
expect(hexDump.length).toBe(172); // 86 bytes * 2 hex chars

// Verify no control characters in the hex dump string
for (let i = 0; i < hexDump.length; i++) {
const charCode = hexDump.charCodeAt(i);
expect(charCode >= 48 && charCode <= 57 || charCode >= 97 && charCode <= 102).toBe(true); // 0-9 or a-f
}

// Verify version bytes with control chars exist but are NOT in the dumped portion
const versionBytes = wirePacket.subarray(87);
expect(versionBytes.includes(0x00)).toBe(true); // Version has control chars
expect(wirePacket.length).toBeGreaterThan(86); // Full packet is longer
expect(dumpBytes.length).toBe(86); // But we only dump first 86 bytes
});

it('should use ASCII-only characters in truncation suffix', () => {
const challenge = Buffer.from('1234567890abcdef1234567890abcdef', 'hex');
const longVersion = 'a'.repeat(100);
const authMessage = buildAuthPacket(OPCODE_AUTH_WITH_VERSION, challenge, longVersion);
const wirePacket = Buffer.concat([Buffer.from([OPCODE_AUTH_WITH_VERSION]), authMessage]);

// Simulate TCPClient truncation logic
const dumpBytes = wirePacket.subarray(0, 86);
const suffix = ` ...(+${wirePacket.length - 86}b version)`;
const logLine = dumpBytes.toString('hex') + suffix;

// Verify entire log line is ASCII-only (no Unicode ellipsis or em-dash)
for (let i = 0; i < logLine.length; i++) {
const charCode = logLine.charCodeAt(i);
expect(charCode).toBeGreaterThanOrEqual(32); // Printable ASCII starts at space (32)
expect(charCode).toBeLessThanOrEqual(126); // Printable ASCII ends at ~ (126)
}

// Verify it contains ASCII ellipsis, not Unicode
expect(logLine).toContain('...');
expect(logLine).not.toContain('…'); // Unicode ellipsis U+2026
});
});
});
6 changes: 3 additions & 3 deletions src/api/passkey-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ export class PasskeyStore {
private load(): StoreFile {
try {
if (!existsSync(this.path)) {
console.log(`[PasskeyStore] No store at ${this.path} — starting empty`);
console.log(`[PasskeyStore] No store at ${this.path} -- starting empty`);
return emptyStore();
}
const raw = readFileSync(this.path, 'utf-8');
const parsed = JSON.parse(raw) as StoreFile;
if (!parsed || parsed.version !== 1) {
console.warn('[PasskeyStore] Unknown store version — starting empty');
console.warn('[PasskeyStore] Unknown store version -- starting empty');
return emptyStore();
}
parsed.credentials = parsed.credentials || {};
Expand All @@ -110,7 +110,7 @@ export class PasskeyStore {
console.log(`[PasskeyStore] Loaded ${n} credential(s) from ${this.path}`);
return parsed;
} catch (err) {
console.error('[PasskeyStore] Failed to load — starting empty:', err);
console.error('[PasskeyStore] Failed to load -- starting empty:', err);
return emptyStore();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/service/ASyncTCPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class ASyncTCPService implements TCPCallback {

const liveIdx = this.liveIndexForAddress(addrLower);
if (liveIdx !== undefined && liveIdx !== index && !this.isQuiet(liveIdx)) {
console.log('Reject login #' + index, address, '— #' + liveIdx, 'still live');
console.log('Reject login #' + index, address, '-- #' + liveIdx, 'still live');
client.terminate();
return;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ export class ASyncTCPService implements TCPCallback {
const addrLower = recoveredAddr.toLowerCase();
const liveIdx = this.liveIndexForAddress(addrLower);
if (liveIdx !== undefined && liveIdx !== index && !this.isQuiet(liveIdx)) {
console.log('Reject signed login #' + index, '— #' + liveIdx, 'still live');
console.log('Reject signed login #' + index, '-- #' + liveIdx, 'still live');
client.terminate();
return;
}
Expand Down
18 changes: 17 additions & 1 deletion src/service/TCPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,23 @@ export class TCPClient {
const preview = buffer.subarray(0, 4).toString('hex');
const peer = this.getRemoteIp() || '?';
console.log('Receive Command:', preview, '#' + this.clientIndex, peer);
const dump = buffer.length <= 96 ? buffer.toString('hex') : buffer.subarray(0, 96).toString('hex') + `…(+${buffer.length - 96}b)`;

// For opcode 0x09 (auth with version), exclude version bytes from dump
// to avoid non-printable characters in terminal. Dump only: opcode(1) + addr(20) + sig(65) = 86 bytes
const opcode = buffer[0];
const OPCODE_AUTH_WITH_VERSION = 0x09;
let dumpBuffer = buffer;
let suffix = '';

if (opcode === OPCODE_AUTH_WITH_VERSION && buffer.length > 86) {
dumpBuffer = buffer.subarray(0, 86);
suffix = ` ...(+${buffer.length - 86}b version)`;
} else if (buffer.length > 96) {
dumpBuffer = buffer.subarray(0, 96);
suffix = ` ...(+${buffer.length - 96}b)`;
}

const dump = dumpBuffer.toString('hex') + suffix;
console.log(' -->', dump, '#' + this.clientIndex, peer);
this.isWaitingForResponse = false;
this.lastConnection = Date.now();
Expand Down
Loading