Skip to content

Commit

Permalink
fix: better sanitize topics (#981)
Browse files Browse the repository at this point in the history
* fix(hass): nodes sanitized discovery topic

* fix: lint issues

* fix: better sanitize regex

* fix: sanitize only nodeName
  • Loading branch information
robertsLando authored Apr 1, 2021
1 parent f2efd4e commit d8fa77d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,9 @@ function deviceInfo (node, nodeName) {
* @returns The topic string for this device discovery
*/
function getDiscoveryTopic (hassDevice, nodeName) {
return `${hassDevice.type}/${nodeName}/${hassDevice.object_id}/config`
return `${hassDevice.type}/${utils.sanitizeTopic(nodeName, true)}/${
hassDevice.object_id
}/config`
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function sanitizeTopic (str, sanitizeSlash) {
// replace spaces with '_'
str = str.replace(/\s/g, '_')
// remove special chars
return str.replace(/[+*#\\.'`!?^=(),"%[\]:;{}]+/g, '')
return str.replace(/[^A-Za-z0-9-_/]/g, '')
}

/**
Expand Down

0 comments on commit d8fa77d

Please sign in to comment.