Skip to content

Commit

Permalink
feat: do not print user codes or network keys in logfile (#4383)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Mar 16, 2022
1 parent 7a8c70f commit ad882f3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 76 deletions.
6 changes: 0 additions & 6 deletions packages/zwave-js/src/lib/commandclass/CommandClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
NODE_ID_BROADCAST,
parseCCId,
serializeCacheValue,
stripUndefined,
ValueDB,
ValueID,
valueIdToString,
Expand Down Expand Up @@ -433,11 +432,6 @@ export class CommandClass {
return ret;
}

protected toJSONInherited(props: JSONObject): JSONObject {
const { payload, ...ret } = this.toJSONInternal();
return stripUndefined({ ...ret, ...props });
}

protected throwMissingCriticalInterviewResponse(): never {
throw new ZWaveError(
`The node did not respond to a critical interview query in time.`,
Expand Down
10 changes: 5 additions & 5 deletions packages/zwave-js/src/lib/commandclass/DoorLockLoggingCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ZWaveError,
ZWaveErrorCodes,
} from "@zwave-js/core";
import { buffer2hex, isPrintableASCII, num2hex } from "@zwave-js/shared";
import { isPrintableASCII, num2hex } from "@zwave-js/shared";
import type { Driver } from "../driver/Driver";
import { MessagePriority } from "../message/Constants";
import { PhysicalCCAPI } from "./API";
Expand All @@ -23,6 +23,7 @@ import {
gotDeserializationOptions,
implementedVersion,
} from "./CommandClass";
import { userCodeToLogString } from "./UserCodeCC";

interface DateSegments {
year: number;
Expand Down Expand Up @@ -358,10 +359,9 @@ export class DoorLockLoggingCCRecordReport extends DoorLockLoggingCC {
message["user ID"] = this.record.userId;
}
if (this.record.userCode) {
message["user code"] =
typeof this.record.userCode === "string"
? this.record.userCode
: buffer2hex(this.record.userCode);
message["user code"] = userCodeToLogString(
this.record.userCode,
);
}
}
return {
Expand Down
5 changes: 0 additions & 5 deletions packages/zwave-js/src/lib/commandclass/NodeNamingCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ZWaveError,
ZWaveErrorCodes,
} from "@zwave-js/core";
import type { JSONObject } from "@zwave-js/shared";
import type { Driver } from "../driver/Driver";
import { MessagePriority } from "../message/Constants";
import {
Expand Down Expand Up @@ -400,10 +399,6 @@ export class NodeNamingAndLocationCCLocationReport extends NodeNamingAndLocation
message: { location: this.location },
};
}

public toJSON(): JSONObject {
return super.toJSONInherited({ location: this.location });
}
}

@CCCommand(NodeNamingAndLocationCommand.LocationGet)
Expand Down
20 changes: 1 addition & 19 deletions packages/zwave-js/src/lib/commandclass/NotificationCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ZWaveError,
ZWaveErrorCodes,
} from "@zwave-js/core";
import { buffer2hex, JSONObject, num2hex, pick } from "@zwave-js/shared";
import { buffer2hex, num2hex, pick } from "@zwave-js/shared";
import { isArray } from "alcalzone-shared/typeguards";
import type { Driver } from "../driver/Driver";
import { MessagePriority } from "../message/Constants";
Expand Down Expand Up @@ -994,24 +994,6 @@ export class NotificationCCReport extends NotificationCC {
};
}

public toJSON(): JSONObject {
return super.toJSONInherited({
alarmType: this.alarmType,
notificationType:
this.notificationType != undefined
? this.driver.configManager.lookupNotification(
this.notificationType,
)?.name
: this.notificationType,
notificationStatus: this.notificationStatus,
notificationEvent: this.notificationEvent,
alarmLevel: this.alarmLevel,
zensorNetSourceNodeId: this.zensorNetSourceNodeId,
eventParameters: this.eventParameters,
sequenceNumber: this.sequenceNumber,
});
}

private parseEventParameters(): void {
if (
this.notificationType == undefined ||
Expand Down
3 changes: 2 additions & 1 deletion packages/zwave-js/src/lib/commandclass/Security2CC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,8 @@ export class Security2CCNetworkKeyReport extends Security2CC {
SecurityClass,
this.grantedKey,
),
"network key": buffer2hex(this.networkKey),
// This shouldn't be logged, so users can safely post their logs online
// "network key": buffer2hex(this.networkKey),
},
};
}
Expand Down
13 changes: 7 additions & 6 deletions packages/zwave-js/src/lib/commandclass/SecurityCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,13 @@ export class SecurityCCNetworkKeySet extends SecurityCC {
return super.serialize();
}

public toLogEntry(): MessageOrCCLogEntry {
return {
...super.toLogEntry(),
message: { "network key": buffer2hex(this.networkKey) },
};
}
// The network key shouldn't be logged, so users can safely post their logs online
// public toLogEntry(): MessageOrCCLogEntry {
// return {
// ...super.toLogEntry(),
// message: { "network key": buffer2hex(this.networkKey) },
// };
// }
}

@CCCommand(SecurityCommand.CommandsSupportedReport)
Expand Down
50 changes: 16 additions & 34 deletions packages/zwave-js/src/lib/commandclass/UserCodeCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import {
ZWaveErrorCodes,
} from "@zwave-js/core";
import {
buffer2hex,
getEnumMemberName,
isPrintableASCII,
isPrintableASCIIWithNewlines,
JSONObject,
num2hex,
pick,
} from "@zwave-js/shared";
Expand Down Expand Up @@ -312,6 +310,12 @@ function persistUserCode(
return true;
}

/** Formats a user code in a way that's safe to print in public logs */
export function userCodeToLogString(userCode: string | Buffer): string {
if (userCode === "") return "(empty)";
return "*".repeat(userCode.length);
}

@API(CommandClasses["User Code"])
export class UserCodeCCAPI extends PhysicalCCAPI {
public supportsCommand(cmd: UserCodeCommand): Maybe<boolean> {
Expand Down Expand Up @@ -966,23 +970,10 @@ export class UserCodeCCSet extends UserCodeCC {
message: {
"user id": this.userId,
"id status": getEnumMemberName(UserIDStatus, this.userIdStatus),
"user code":
typeof this.userCode === "string"
? this.userCode
: buffer2hex(this.userCode),
"user code": userCodeToLogString(this.userCode),
},
};
}

public toJSON(): JSONObject {
return super.toJSONInherited({
userId: this.userId,
userCode:
typeof this.userCode === "string"
? this.userCode
: buffer2hex(this.userCode),
});
}
}

@CCCommand(UserCodeCommand.Report)
Expand Down Expand Up @@ -1056,10 +1047,7 @@ export class UserCodeCCReport
message: {
"user id": this.userId,
"id status": getEnumMemberName(UserIDStatus, this.userIdStatus),
"user code":
typeof this.userCode === "string"
? this.userCode
: buffer2hex(this.userCode),
"user code": userCodeToLogString(this.userCode),
},
};
}
Expand Down Expand Up @@ -1428,7 +1416,7 @@ export class UserCodeCCMasterCodeSet extends UserCodeCC {
public toLogEntry(): MessageOrCCLogEntry {
return {
...super.toLogEntry(),
message: { "master code": this.masterCode },
message: { "master code": userCodeToLogString(this.masterCode) },
};
}
}
Expand Down Expand Up @@ -1461,7 +1449,7 @@ export class UserCodeCCMasterCodeReport extends UserCodeCC {
public toLogEntry(): MessageOrCCLogEntry {
return {
...super.toLogEntry(),
message: { "master code": this.masterCode },
message: { "master code": userCodeToLogString(this.masterCode) },
};
}
}
Expand Down Expand Up @@ -1637,12 +1625,9 @@ export class UserCodeCCExtendedUserCodeSet extends UserCodeCC {
public toLogEntry(): MessageOrCCLogEntry {
const message: MessageRecord = {};
for (const { userId, userIdStatus, userCode } of this.userCodes) {
message[
`code #${userId}`
] = `${userCode} (status: ${getEnumMemberName(
UserIDStatus,
userIdStatus,
)})`;
message[`code #${userId}`] = `${userCodeToLogString(
userCode,
)} (status: ${getEnumMemberName(UserIDStatus, userIdStatus)})`;
}
return {
...super.toLogEntry(),
Expand Down Expand Up @@ -1691,12 +1676,9 @@ export class UserCodeCCExtendedUserCodeReport extends UserCodeCC {
public toLogEntry(): MessageOrCCLogEntry {
const message: MessageRecord = {};
for (const { userId, userIdStatus, userCode } of this.userCodes) {
message[
`code #${userId}`
] = `${userCode} (status: ${getEnumMemberName(
UserIDStatus,
userIdStatus,
)})`;
message[`code #${userId}`] = `${userCodeToLogString(
userCode,
)} (status: ${getEnumMemberName(UserIDStatus, userIdStatus)})`;
}
message["next user id"] = this.nextUserId;
return {
Expand Down

0 comments on commit ad882f3

Please sign in to comment.