homebridge-lg-thinq-2 2.0.12 → 2.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Change Log
2
2
 
3
+ ## v2.0.14
4
+
5
+ ### Reliability
6
+
7
+ * The MQTT root certificate and client certificate received from LG are now validated before they are written to disk: they must be PEM certificates that parse as X.509, with no other content. If LG or the certificate host returns something else, such as an error page, the plugin logs a clear error, writes no file, and retries later, instead of saving it as a certificate and failing with a TLS error. Checked against the real AWS and LG root certificates.
8
+ * Known issue: the legacy root certificate URL (VeriSign G5, only used for MQTT hosts that are neither AWS IoT nor LG) now redirects to the DigiCert home page. It is now reported as an invalid certificate instead of being saved.
9
+
10
+ ## v2.0.13
11
+
12
+ ### Security
13
+
14
+ * Fixed the host name checks that pick the root certificate for the MQTT connection: the dots in the patterns were not escaped, so they matched any character and host names that only looked like AWS IoT or LG hosts. Found by CodeQL.
15
+ * When migrating cache files from older versions, each file is now checked and read through the same open file, so it cannot be replaced in between. Found by CodeQL.
16
+
17
+ ### Development
18
+
19
+ * Hardened the GitHub Actions workflows: the CI workflow now has a read-only token, actions are pinned to commit SHAs, dependencies are installed without running their install scripts (`npm ci --ignore-scripts`), jobs have time limits, and newer pushes to a pull request cancel older checks.
20
+ * Added Dependabot version updates for npm and GitHub Actions (weekly, development dependencies grouped), and CodeQL code scanning for JavaScript and TypeScript.
21
+
3
22
  ## v2.0.12
4
23
 
5
24
  ### Homebridge UI
@@ -2,6 +2,20 @@ import NodePersist from 'node-persist';
2
2
  import Fs from 'fs/promises';
3
3
  import Path from 'path';
4
4
  import Crypto from 'crypto';
5
+ /** Reads a file as UTF-8, or returns null if it is missing or not a regular file. */
6
+ async function readRegularFile(path) {
7
+ const handle = await Fs.open(path, 'r').catch(() => null);
8
+ if (!handle) {
9
+ return null;
10
+ }
11
+ try {
12
+ const stat = await handle.stat();
13
+ return stat.isFile() ? await handle.readFile({ encoding: 'utf8' }) : null;
14
+ }
15
+ finally {
16
+ await handle.close();
17
+ }
18
+ }
5
19
  export function isPersistCacheMiss(value) {
6
20
  return value === null || value === undefined;
7
21
  }
@@ -58,15 +72,15 @@ export default class Persist {
58
72
  continue;
59
73
  }
60
74
  const p = Path.join(this.dir, f);
61
- const stat = await Fs.stat(p).catch(() => null);
62
- if (!stat || !stat.isFile()) {
63
- continue;
64
- }
65
75
  if (/^[a-f0-9]{64}$/.test(f)) {
66
76
  continue;
67
77
  }
68
78
  try {
69
- const raw = await Fs.readFile(p, { encoding: 'utf8' });
79
+ // Check and read the same open file, so it cannot be replaced in between.
80
+ const raw = await readRegularFile(p);
81
+ if (raw === null) {
82
+ continue;
83
+ }
70
84
  const parsed = JSON.parse(raw);
71
85
  const key = parsed && typeof parsed === 'object' && parsed.key ? parsed.key : f;
72
86
  const datum = { key, value: parsed && parsed.key ? parsed.value ?? parsed : parsed, ttl: undefined };
@@ -1,3 +1,4 @@
1
+ import { X509Certificate } from 'crypto';
1
2
  import * as FS from 'fs';
2
3
  import * as Path from 'path';
3
4
  import forge from 'node-forge';
@@ -12,11 +13,14 @@ export const MQTT_CLIENT_PATH = 'service/users/client';
12
13
  export const MQTT_CERTIFICATE_PATH = 'service/users/client/certificate';
13
14
  export const MQTT_RETRY_ATTEMPTS = 5;
14
15
  export const MQTT_RETRY_DELAY_MS = 5000;
16
+ // Dots are escaped: an unescaped "." matches any character, so other hosts could match.
17
+ const AWS_ATS_MQTT_HOST = /^[^.]+-ats\.iot\.[^.]+\.amazonaws\.com$/;
18
+ const LG_MQTT_HOST = /^[^.]+\.iot\.ruic\.lgthinq\.com$/;
15
19
  export function rootCaUrlForMqttHost(hostname) {
16
- if (hostname.match(/^([^.]+)-ats.iot.([^.]+).amazonaws.com$/g)) {
20
+ if (AWS_ATS_MQTT_HOST.test(hostname)) {
17
21
  return ATS_ROOT_CA_URL;
18
22
  }
19
- if (hostname.match(/^([^.]+).iot.ruic.lgthinq.com$/g)) {
23
+ if (LG_MQTT_HOST.test(hostname)) {
20
24
  return LG_ROOT_CA_URL;
21
25
  }
22
26
  return LEGACY_ROOT_CA_URL;
@@ -110,8 +114,43 @@ export async function writeIfChanged(path, content, mode) {
110
114
  await FS.promises.chmod(path, mode);
111
115
  }
112
116
  }
117
+ const PEM_CERTIFICATE_BLOCK = /-----BEGIN CERTIFICATE-----\r?\n[\s\S]*?-----END CERTIFICATE-----/g;
118
+ /**
119
+ * Checks that certificate data received from LG is one or more PEM certificates,
120
+ * and returns it. Throws without including the data, which could be anything,
121
+ * such as an HTML error page.
122
+ *
123
+ * The data is written to disk for the MQTT client, so nothing else is accepted:
124
+ * no text outside the certificate blocks, and every block must parse as an X.509
125
+ * certificate.
126
+ */
127
+ export function validatePemCertificates(data, description) {
128
+ const invalid = (reason) => new Error(`LG returned an invalid ${description} (${reason}); skipping this connection attempt.`);
129
+ if (typeof data !== 'string') {
130
+ throw invalid('not text');
131
+ }
132
+ const blocks = data.match(PEM_CERTIFICATE_BLOCK) ?? [];
133
+ if (blocks.length === 0) {
134
+ throw invalid('no PEM certificate');
135
+ }
136
+ if (data.replace(PEM_CERTIFICATE_BLOCK, '').trim() !== '') {
137
+ throw invalid('unexpected content outside the PEM certificates');
138
+ }
139
+ for (const block of blocks) {
140
+ try {
141
+ new X509Certificate(block);
142
+ }
143
+ catch {
144
+ throw invalid('a PEM block is not a valid X.509 certificate');
145
+ }
146
+ }
147
+ return data;
148
+ }
113
149
  export async function writeMqttCertificateFiles(options) {
114
- const { mqttDir, rootCA, privateKey, certificatePem } = options;
150
+ const { mqttDir, privateKey } = options;
151
+ // Validate both certificates from LG before writing any file.
152
+ const rootCA = validatePemCertificates(options.rootCA, 'MQTT root certificate');
153
+ const certificatePem = validatePemCertificates(options.certificatePem, 'MQTT client certificate');
115
154
  await FS.promises.mkdir(mqttDir, { recursive: true, mode: MQTT_DIRECTORY_MODE });
116
155
  await FS.promises.chmod(mqttDir, MQTT_DIRECTORY_MODE);
117
156
  const paths = mqttCertificatePaths(mqttDir);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Homebridge LG ThinQ 2",
3
3
  "name": "homebridge-lg-thinq-2",
4
- "version": "2.0.12",
4
+ "version": "2.0.14",
5
5
  "description": "Community-maintained continuation of homebridge-lg-thinq: a Homebridge plugin for controlling/monitoring LG ThinQ devices via the LG ThinQ platform.",
6
6
  "license": "Apache-2.0",
7
7
  "type": "module",