Skip to content

Commit f9e07e6

Browse files
bwp91claude
andcommitted
test: cover SEQUENCE_NUM presence check in pair handlers
Adds two HAPServer integration tests verifying the guard from ebe2ec6: /pair-setup and /pair-verify must reject with 400 BAD_REQUEST + STATE=M2 + UNKNOWN error when the request TLV is missing TLVValues.SEQUENCE_NUM, rather than crashing on an indexed read of an undefined entry. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9d27bd2 commit f9e07e6

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All notable changes to `@homebridge/hap-nodejs` will be documented in this file.
1212
- test: cover encrypted data length validation in pair handlers
1313
- test: cover required TLV field validation in pairing handlers
1414
- test: cover RTP proxy setup rejection handling
15+
- test: cover SEQUENCE_NUM presence check in pair handlers
1516

1617
### Homebridge Dependencies
1718

src/lib/HAPServer.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,50 @@ describe("HAPServer", () => {
328328
});
329329
});
330330

331+
describe("SEQUENCE_NUM check in pair handlers (fix ebe2ec67)", () => {
332+
test("/pair-setup should reject when SEQUENCE_NUM TLV is missing", async () => {
333+
server = new HAPServer(accessoryInfoUnpaired);
334+
const [port] = await bindServer(server);
335+
336+
// a TLV containing only METHOD (no SEQUENCE_NUM/STATE) — without the
337+
// guard this would crash on `tlvData[SEQUENCE_NUM][0]` of `undefined`.
338+
try {
339+
await axios.post(
340+
`http://localhost:${port}/pair-setup`,
341+
tlv.encode(TLVValues.METHOD, PairMethods.PAIR_SETUP),
342+
{ httpAgent, responseType: "arraybuffer" },
343+
);
344+
fail("Expected BAD_REQUEST response");
345+
} catch (error) {
346+
expect(error).toBeInstanceOf(AxiosError);
347+
expect(error.response?.status).toBe(HAPHTTPCode.BAD_REQUEST);
348+
const objects = tlv.decode(error.response?.data);
349+
expect(objects[TLVValues.STATE].readUInt8(0)).toEqual(PairingStates.M2);
350+
expect(objects[TLVValues.ERROR_CODE].readUInt8(0)).toEqual(TLVErrorCode.UNKNOWN);
351+
}
352+
});
353+
354+
test("/pair-verify should reject when SEQUENCE_NUM TLV is missing", async () => {
355+
server = new HAPServer(accessoryInfoPaired);
356+
const [port] = await bindServer(server);
357+
358+
try {
359+
await axios.post(
360+
`http://localhost:${port}/pair-verify`,
361+
tlv.encode(TLVValues.PUBLIC_KEY, Buffer.alloc(32)),
362+
{ httpAgent, responseType: "arraybuffer" },
363+
);
364+
fail("Expected BAD_REQUEST response");
365+
} catch (error) {
366+
expect(error).toBeInstanceOf(AxiosError);
367+
expect(error.response?.status).toBe(HAPHTTPCode.BAD_REQUEST);
368+
const objects = tlv.decode(error.response?.data);
369+
expect(objects[TLVValues.STATE].readUInt8(0)).toEqual(PairingStates.M2);
370+
expect(objects[TLVValues.ERROR_CODE].readUInt8(0)).toEqual(TLVErrorCode.UNKNOWN);
371+
}
372+
});
373+
});
374+
331375
describe("required TLV fields validation in pairing handlers (fix 58c24b92)", () => {
332376
test("/pair-setup M5 should reject when decrypted payload is missing IDENTIFIER", async () => {
333377
server = new HAPServer(accessoryInfoUnpaired);

0 commit comments

Comments
 (0)