Skip to content

Commit

Permalink
Add openhab server uuid validation (#443)
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <jeremy.setton@gmail.com>
  • Loading branch information
jsetton authored Dec 13, 2021
1 parent a59e1eb commit 52e6b88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lambda/openhab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const fs = require('fs');
const request = require('request-promise-native');
const Agent = require('agentkeepalive');
const { sprintf } = require('sprintf-js');
const { validate: uuidValidate } = require('uuid');

/**
* Defines openHAB class
Expand Down Expand Up @@ -80,7 +81,7 @@ class OpenHAB {
const [properties, uuid] = await Promise.all([this.getRootResource(), this.getUUID().catch(() => undefined)]);
const { locale, measurementSystem, runtimeInfo, version } = properties || {};
const apiVersion = parseFloat(version);
const settings = { regional: {}, runtime: { ...(uuid && { uuid }) } };
const settings = { regional: {}, runtime: { ...(uuidValidate(uuid) && { uuid }) } };

if (apiVersion >= 4) {
// Use root resource properties for OH 3.0 and later [API Version >= 4]
Expand Down
17 changes: 17 additions & 0 deletions lambda/test/openhab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ describe('OpenHAB Tests', () => {
expect(nock.isDone()).to.be.true;
});

it('oh3.x with invalid uuid', async () => {
// set environment
nock(baseURL)
// root resource
.get('/rest/')
.reply(200, { version: '4', locale, measurementSystem, runtimeInfo: { version: '3.0.0' } })
// uuid
.get('/rest/uuid')
.reply(200, 'invalid');
// run test
expect(await openhab.getServerSettings()).to.deep.equal({
regional: { language, measurementSystem, region },
runtime: { version: '3.0.0' }
});
expect(nock.isDone()).to.be.true;
});

it('oh3.x with unauthorized uuid', async () => {
// set environment
nock(baseURL)
Expand Down

0 comments on commit 52e6b88

Please sign in to comment.