Allowlist pubkey prefixes only; ignore public and hashtag channels
- is_allowed matches pubkey_prefix only; sender names are never trusted
(channel senders must be known contacts of the companion for the
MeshCore integration to include their pubkey_prefix)
- channel messages on channel index 0 (public) and hashtag channels
('#name') are ignored unconditionally, before the allowlist check
- config flow validates allowlist entries as 6-64 hex characters
(invalid_allowlist error) and UI strings/README updated
This commit is contained in:
@@ -28,6 +28,7 @@ from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import store as storage
|
||||
from .const import (
|
||||
ATTR_CHANNEL,
|
||||
ATTR_CHANNEL_IDX,
|
||||
ATTR_MESSAGE,
|
||||
ATTR_MESSAGE_TYPE,
|
||||
@@ -42,7 +43,9 @@ from .const import (
|
||||
DEFAULT_MAX_REPLY,
|
||||
DOMAIN,
|
||||
EVENT_MESHCORE_MESSAGE,
|
||||
HASHTAG_CHANNEL_PREFIX,
|
||||
MESH_CORE_DOMAIN,
|
||||
PUBLIC_CHANNEL_IDX,
|
||||
SERVICE_MESHCORE_SEND_CHANNEL_MESSAGE,
|
||||
SERVICE_MESHCORE_SEND_MESSAGE,
|
||||
SIGNAL_STORE_UPDATED,
|
||||
@@ -139,15 +142,26 @@ class MeshCoreAgent:
|
||||
# -- allowlist -----------------------------------------------------------
|
||||
|
||||
def is_allowed(self, event_data: dict) -> bool:
|
||||
"""Only pubkey prefixes count; sender names are never trusted.
|
||||
|
||||
Channel senders must therefore be known contacts of the companion
|
||||
(the MeshCore integration includes their pubkey_prefix).
|
||||
"""
|
||||
if self._allow_all:
|
||||
return True
|
||||
if not self._allowlist:
|
||||
return False
|
||||
prefix = (event_data.get(ATTR_PUBKEY_PREFIX) or "").lower()
|
||||
name = (event_data.get(ATTR_SENDER_NAME) or "").lower()
|
||||
return (
|
||||
bool(prefix) and prefix in self._allowlist
|
||||
) or (bool(name) and name in self._allowlist)
|
||||
prefix = (event_data.get(ATTR_PUBKEY_PREFIX) or "").strip().lower()
|
||||
return bool(prefix) and prefix in self._allowlist
|
||||
|
||||
def is_public_channel(self, event_data: dict) -> bool:
|
||||
"""True for the public channel (index 0) and hashtag channels."""
|
||||
if event_data.get(ATTR_MESSAGE_TYPE) != "channel":
|
||||
return False
|
||||
if event_data.get(ATTR_CHANNEL_IDX) == PUBLIC_CHANNEL_IDX:
|
||||
return True
|
||||
channel_name = str(event_data.get(ATTR_CHANNEL) or "")
|
||||
return channel_name.startswith(HASHTAG_CHANNEL_PREFIX)
|
||||
|
||||
# -- message handling ------------------------------------------------------
|
||||
|
||||
@@ -157,6 +171,13 @@ class MeshCoreAgent:
|
||||
return
|
||||
if data.get(ATTR_MESSAGE_TYPE) not in VALID_MESSAGE_TYPES:
|
||||
return
|
||||
if self.is_public_channel(data):
|
||||
_LOGGER.debug(
|
||||
"Ignoring public/hashtag channel message (idx=%s, channel=%r)",
|
||||
data.get(ATTR_CHANNEL_IDX),
|
||||
data.get(ATTR_CHANNEL),
|
||||
)
|
||||
return
|
||||
if not self.is_allowed(data):
|
||||
_LOGGER.debug(
|
||||
"Ignoring message from non-allowlisted sender %s",
|
||||
|
||||
@@ -36,9 +36,22 @@ def _validate(user_input: dict[str, Any]) -> str | None:
|
||||
int(user_input[CONF_MAX_REPLY])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return "invalid_max_reply"
|
||||
allowlist = str(user_input.get(CONF_ALLOWLIST, "")).replace("\n", ",")
|
||||
for part in allowlist.split(","):
|
||||
part = part.strip()
|
||||
if not part:
|
||||
continue
|
||||
if not is_hex_prefix(part):
|
||||
return "invalid_allowlist"
|
||||
return None
|
||||
|
||||
|
||||
def is_hex_prefix(value: str) -> bool:
|
||||
"""An allowlist entry must be 6-64 hex characters (a pubkey prefix)."""
|
||||
v = value.lower()
|
||||
return 6 <= len(v) <= 64 and all(c in "0123456789abcdef" for c in v)
|
||||
|
||||
|
||||
class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle the initial setup of the integration."""
|
||||
|
||||
|
||||
@@ -26,5 +26,11 @@ ATTR_OUTGOING = "outgoing"
|
||||
ATTR_MESSAGE_TYPE = "message_type"
|
||||
ATTR_SENDER_NAME = "sender_name"
|
||||
ATTR_PUBKEY_PREFIX = "pubkey_prefix"
|
||||
ATTR_CHANNEL = "channel"
|
||||
ATTR_CHANNEL_IDX = "channel_idx"
|
||||
ATTR_ENTRY_ID = "entry_id"
|
||||
|
||||
# Channel 0 is the public channel; hashtag channels ("#name") are the
|
||||
# other shared channels. Neither may command the agent.
|
||||
PUBLIC_CHANNEL_IDX = 0
|
||||
HASHTAG_CHANNEL_PREFIX = "#"
|
||||
|
||||
@@ -4,32 +4,34 @@
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "MeshCore Agent",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. The allowlist accepts public key prefixes or contact names, separated by commas.",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. Only public key prefixes are accepted (channel senders must be known contacts to appear with one).",
|
||||
"data": {
|
||||
"allowlist": "Allowed senders (pubkey prefixes or names, comma separated)",
|
||||
"allowlist": "Allowed senders (pubkey prefixes, comma separated)",
|
||||
"allow_all": "Allow all senders (not recommended)",
|
||||
"max_reply": "Maximum reply length (characters)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500."
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500.",
|
||||
"invalid_allowlist": "Allowlist entries must be public key prefixes (6-64 hex characters, comma separated)."
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"title": "MeshCore Agent options",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. The allowlist accepts public key prefixes or contact names, separated by commas.",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. Only public key prefixes are accepted (channel senders must be known contacts to appear with one).",
|
||||
"data": {
|
||||
"allowlist": "Allowed senders (pubkey prefixes or names, comma separated)",
|
||||
"allowlist": "Allowed senders (pubkey prefixes, comma separated)",
|
||||
"allow_all": "Allow all senders (not recommended)",
|
||||
"max_reply": "Maximum reply length (characters)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500."
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500.",
|
||||
"invalid_allowlist": "Allowlist entries must be public key prefixes (6-64 hex characters, comma separated)."
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
|
||||
@@ -4,32 +4,34 @@
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "MeshCore Agent",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. The allowlist accepts public key prefixes or contact names, separated by commas.",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. Only public key prefixes are accepted (channel senders must be known contacts to appear with one).",
|
||||
"data": {
|
||||
"allowlist": "Allowed senders (pubkey prefixes or names, comma separated)",
|
||||
"allowlist": "Allowed senders (pubkey prefixes, comma separated)",
|
||||
"allow_all": "Allow all senders (not recommended)",
|
||||
"max_reply": "Maximum reply length (characters)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500."
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500.",
|
||||
"invalid_allowlist": "Allowlist entries must be public key prefixes (6-64 hex characters, comma separated)."
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"title": "MeshCore Agent options",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. The allowlist accepts public key prefixes or contact names, separated by commas.",
|
||||
"description": "Allow MeshCore nodes to talk to the agent. Only public key prefixes are accepted (channel senders must be known contacts to appear with one).",
|
||||
"data": {
|
||||
"allowlist": "Allowed senders (pubkey prefixes or names, comma separated)",
|
||||
"allowlist": "Allowed senders (pubkey prefixes, comma separated)",
|
||||
"allow_all": "Allow all senders (not recommended)",
|
||||
"max_reply": "Maximum reply length (characters)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500."
|
||||
"invalid_max_reply": "Maximum reply length must be a number between 20 and 500.",
|
||||
"invalid_allowlist": "Allowlist entries must be public key prefixes (6-64 hex characters, comma separated)."
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
|
||||
Reference in New Issue
Block a user