Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: trim surrounding whitespace from QR codes #5295

Merged
merged 1 commit into from
Jan 4, 2023
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
25 changes: 25 additions & 0 deletions packages/core/src/security/QR.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,28 @@ test("QR code parsing -> Example case: Acme Light Dimmer w/o SmartStart", (t) =>
productId: 0x0003,
});
});

test("QR code parsing -> ignore surrounding whitespace", (t) => {
const result = parseQRCodeString(
" 900032782003515253545541424344453132333435212223242500100435301537022065520001000000300578 ",
);
t.deepEqual(result, {
version: QRCodeVersion.S2,
securityClasses: [
SecurityClass.S2_Unauthenticated,
SecurityClass.S2_Authenticated,
],
requestedSecurityClasses: [
SecurityClass.S2_Unauthenticated,
SecurityClass.S2_Authenticated,
],
dsk: "51525-35455-41424-34445-31323-33435-21222-32425",
applicationVersion: "2.66",
genericDeviceClass: 0x11,
specificDeviceClass: 0x01,
installerIconType: 0x0601,
manufacturerId: 0xfff0,
productType: 0x0064,
productId: 0x0003,
});
});
7 changes: 5 additions & 2 deletions packages/core/src/security/QR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ function parseTLV(qr: string): {

/** Parses a string that has been decoded from a Z-Wave (S2 or SmartStart) QR code */
export function parseQRCodeString(qr: string): QRProvisioningInformation {
// Trim off whitespace that might have been copied by accident
qr = qr.trim();
// Validate the QR code
if (!qr.startsWith("90")) fail("must start with 90");
if (qr.length < minQRCodeLength) fail("too short");
if (!onlyDigitsRegex.test(qr)) fail("contains invalid characters");
Expand All @@ -229,7 +232,7 @@ export function parseQRCodeString(qr: string): QRProvisioningInformation {
const checksum = readUInt16(qr, 4);
// The checksum covers the remaining data
const hash = createHash("sha1");
hash.update(Buffer.from(qr.substr(9), "ascii"));
hash.update(Buffer.from(qr.slice(9), "ascii"));
const expectedChecksum = hash.digest().readUInt16BE(0);
if (checksum !== expectedChecksum) fail("invalid checksum");

Expand Down Expand Up @@ -265,7 +268,7 @@ export function parseQRCodeString(qr: string): QRProvisioningInformation {
const {
entry: { type, ...data },
charsRead,
} = parseTLV(qr.substr(offset));
} = parseTLV(qr.slice(offset));
offset += charsRead;

if (type === ProvisioningInformationType.ProductId) {
Expand Down