homebridge-lg-thinq-2 2.0.13 → 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,12 @@
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
+
3
10
  ## v2.0.13
4
11
 
5
12
  ### Security
@@ -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';
@@ -113,8 +114,43 @@ export async function writeIfChanged(path, content, mode) {
113
114
  await FS.promises.chmod(path, mode);
114
115
  }
115
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
+ }
116
149
  export async function writeMqttCertificateFiles(options) {
117
- 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');
118
154
  await FS.promises.mkdir(mqttDir, { recursive: true, mode: MQTT_DIRECTORY_MODE });
119
155
  await FS.promises.chmod(mqttDir, MQTT_DIRECTORY_MODE);
120
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.13",
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",