From 35fb9d88bca7c4633d6093bd90a985f1dc3ed4c9 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Sun, 15 Nov 2026 11:26:06 -0600 Subject: [PATCH 1/3] Fetch feeds through an optional HTTP or SOCKS proxy Feeds that are blocked on the user's network or in their country could not be read at all: the only way to route requests through a proxy was the HTTP(S)_PROXY environment variables, which are hard to set on Home Assistant OS and apply to every feed. Each feed now has an optional proxy URL (http, https, socks4, socks4a, socks5 or socks5h; PySocks is added for the socks schemes). It is used for the sensor's polls and for the validation fetch during setup and reconfigure, and is available in the setup and options forms and in YAML. Malformed URLs are rejected on their field (UI) or by config validation (YAML). A password in the proxy URL is masked in the reader's repr and in fetch error messages. file:// feeds ignore the proxy, and an empty value keeps the previous behavior, environment variables included. Closes #27 Co-Authored-By: Claude Opus 5.5 --- README.md | 8 + custom_components/feedparser/config_flow.py | 37 +- custom_components/feedparser/const.py | 2 + custom_components/feedparser/coordinator.py | 8 +- custom_components/feedparser/fetch.py | 51 +- custom_components/feedparser/manifest.json | 1 + custom_components/feedparser/reader.py | 13 +- custom_components/feedparser/sensor.py | 13 +- custom_components/feedparser/strings.json | 18 +- .../feedparser/translations/de.json | 18 +- .../feedparser/translations/el.json | 18 +- .../feedparser/translations/en.json | 18 +- pyproject.toml | 1 + tests/test_config_flow.py | 2 +- tests/test_proxy.py | 437 ++++++++++++++++++ 15 files changed, 603 insertions(+), 42 deletions(-) create mode 100644 tests/test_proxy.py diff --git a/README.md b/README.md index 8ff59347..3af7b89f 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,12 @@ sensor: name: Leicester Weather Warnings feed_url: http://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/em match: Leicester + + # A feed that is blocked on this network, fetched through a SOCKS proxy + - platform: feedparser + name: Blocked Feed + feed_url: https://blocked.example.org/rss + proxy: socks5h://192.168.1.10:1080 ``` If you wish the integration to look for enclosures in the feed entries, add `image` to the `inclusions` list. Do not use `enclosure`. @@ -165,6 +171,7 @@ The integration supports both UI and YAML setup. Most options are the same in bo | `match` | No | regular expression | none | `Leicester` or `rain\|snow` | Only entries whose title or summary contains a match are kept. Ignores case. | | `exclude_match` | No | regular expression | none | `sponsored\|advert` | Entries whose title or summary contains a match are removed. Ignores case. | | `user_agent` | No | string | `Home Assistant Feed-parser Integration ` | `Mozilla/5.0 (compatible; MyFeedReader/1.0)` | User-Agent header sent when fetching the feed (and when validating it during UI setup). Leave empty in the UI to use the default. | +| `proxy` | No | URL | none | `socks5h://192.168.1.10:1080` or `http://user:password@proxy.local:3128` | Proxy used to fetch the feed (and to validate it during UI setup). Schemes: `http`, `https`, `socks4`, `socks4a`, `socks5`, `socks5h`. Leave empty to connect directly. | ### Notes and behavior details @@ -173,6 +180,7 @@ The integration supports both UI and YAML setup. Most options are the same in bo - **Filtering entries with `match` and `exclude_match`**: these choose which *entries* are kept, while `inclusions` and `exclusions` choose which *fields* each entry keeps. They search the entry's `title` and `summary` (the RSS ``), even if those fields are not in `inclusions`. Filtering runs before `show_topn`, so `show_topn` counts matching entries, and the sensor's state is the number of matching entries. To get several filtered sensors from one feed, add the feed more than once with different names and patterns. Patterns are [Python regular expressions](https://docs.python.org/3/library/re.html#regular-expression-syntax) and are found anywhere in the text, so `Leicester` matches "Leicester, Leicestershire". Use a template sensor only if you need to reshape entries, not just choose them. - **`image` extraction**: adding `image` to `inclusions` enables image URL extraction from image enclosures, Media RSS (`media:content`, `media:thumbnail`), the first image in the summary HTML, or the first image in the full content (`content:encoded`, used by WordPress feeds). - **`user_agent`**: some servers reject or never answer requests from unknown clients. If a feed works in a browser but fails in Home Assistant, try a browser User-Agent. +- **`proxy`**: use it to reach feeds that are blocked on your network or in your country. Prefer `socks5h://` over `socks5://` for SOCKS proxies: it also looks up the feed's host name through the proxy, which matters when DNS is what blocks the site. A password in the proxy URL is hidden in logs and errors, but it is stored in plain text in Home Assistant's configuration like other settings. Only the feed itself goes through the proxy: images and links in the entries are opened by your browser, which does not use it. Without this option the integration still honors the standard `HTTP_PROXY`, `HTTPS_PROXY` and `ALL_PROXY` environment variables if they are set for Home Assistant. - **UI vs YAML input format**: - UI uses comma-separated text for `inclusions` and `exclusions`. - YAML uses proper lists. diff --git a/custom_components/feedparser/config_flow.py b/custom_components/feedparser/config_flow.py index 84625dbe..4988830a 100644 --- a/custom_components/feedparser/config_flow.py +++ b/custom_components/feedparser/config_flow.py @@ -31,6 +31,7 @@ CONF_LOCAL_TIME, CONF_MANAGED_BY_YAML, CONF_MATCH, + CONF_PROXY, CONF_REMOVE_SUMMARY_IMAGE, CONF_SCAN_INTERVAL, CONF_SHOW_TOPN, @@ -44,7 +45,7 @@ DOMAIN, ENTRY_VERSION, ) -from .fetch import USER_AGENT, fetch_feed +from .fetch import USER_AGENT, fetch_feed, is_valid_proxy from .reader import compile_filter from .yaml_import import ( REASON_MANAGED_IN_UI, @@ -174,6 +175,7 @@ def _options_from_input(user_input: Mapping[str, object]) -> dict[str, object]: CONF_USER_AGENT: _user_agent_from_input(user_input), CONF_MATCH: _pattern(user_input.get(CONF_MATCH)), CONF_EXCLUDE_MATCH: _pattern(user_input.get(CONF_EXCLUDE_MATCH)), + CONF_PROXY: str(user_input.get(CONF_PROXY) or "").strip(), } @@ -196,6 +198,15 @@ def _pattern_errors(options: Mapping[str, object]) -> dict[str, str]: return errors +def _option_errors(options: Mapping[str, object]) -> dict[str, str]: + """Return form errors for the free-text options (patterns, proxy).""" + errors = _pattern_errors(options) + proxy = str(options.get(CONF_PROXY) or "") + if proxy and not is_valid_proxy(proxy): + errors[CONF_PROXY] = "invalid_proxy" + return errors + + def _options_from_import(import_data: Mapping[str, Any]) -> dict[str, object]: """Build stored options from a validated YAML sensor configuration.""" user_agent = str(import_data.get(CONF_USER_AGENT) or "").strip() @@ -215,6 +226,7 @@ def _options_from_import(import_data: Mapping[str, Any]) -> dict[str, object]: CONF_USER_AGENT: "" if user_agent == USER_AGENT else user_agent, CONF_MATCH: _pattern(import_data.get(CONF_MATCH)), CONF_EXCLUDE_MATCH: _pattern(import_data.get(CONF_EXCLUDE_MATCH)), + CONF_PROXY: str(import_data.get(CONF_PROXY) or "").strip(), } @@ -237,6 +249,7 @@ def _schema_with_defaults( # noqa: PLR0913 - one keyword per form field user_agent: str = "", match: str = "", exclude_match: str = "", + proxy: str = "", include_feed_identity: bool, ) -> vol.Schema: """Build schema for user/options forms.""" @@ -275,6 +288,7 @@ def _schema_with_defaults( # noqa: PLR0913 - one keyword per form field vol.Optional(CONF_EXCLUDE_MATCH, default=exclude_match): str, vol.Optional(CONF_EXCLUSIONS, default=exclusions): str, vol.Optional(CONF_USER_AGENT, default=user_agent): str, + vol.Optional(CONF_PROXY, default=proxy): str, } if include_feed_identity: @@ -314,7 +328,7 @@ async def async_step_user( except vol.Invalid: errors[CONF_SCAN_INTERVAL] = "scan_interval_too_short" else: - errors.update(_pattern_errors(options)) + errors.update(_option_errors(options)) if not _is_supported_url(feed_url): errors[CONF_FEED_URL] = "invalid_url" elif self._is_configured(feed_url, name): @@ -326,6 +340,7 @@ async def async_step_user( errors = await self._async_validate_feed( feed_url, str(options[CONF_USER_AGENT]) or USER_AGENT, + str(options[CONF_PROXY]), ) if not errors: return cast( @@ -433,6 +448,7 @@ async def async_step_reconfigure( errors = await self._async_validate_feed( feed_url, str(entry.options.get(CONF_USER_AGENT) or "") or USER_AGENT, + str(entry.options.get(CONF_PROXY) or ""), ) if not errors: # The sensor's unique ID is the entry ID, so the entity ID and @@ -508,6 +524,7 @@ async def _async_validate_feed( self, feed_url: str, user_agent: str, + proxy: str = "", ) -> dict[str, str]: """Fetch the feed and return form errors (empty if it is usable).""" try: @@ -515,6 +532,7 @@ async def _async_validate_feed( self._validate_feed_url, feed_url, user_agent, + proxy, ) except requests.RequestException: return {"base": "cannot_connect"} @@ -523,13 +541,17 @@ async def _async_validate_feed( return {} @staticmethod - def _validate_feed_url(feed_url: str, user_agent: str = USER_AGENT) -> None: + def _validate_feed_url( + feed_url: str, + user_agent: str = USER_AGENT, + proxy: str = "", + ) -> None: """Validate that the URL can be fetched and parses as a feed. - Uses the same request (User-Agent, timeouts) as the sensor so a feed that - passes here also works at runtime. + Uses the same request (User-Agent, proxy, timeouts) as the sensor so a feed + that passes here also works at runtime. """ - parsed = feedparser.parse(fetch_feed(feed_url, user_agent)) + parsed = feedparser.parse(fetch_feed(feed_url, user_agent, proxy)) # A real feed exposes a version (rss20, atom10, ...) even when it currently # has no entries; an HTML page or random document does not. if not parsed.get("version") and not parsed.get("entries"): @@ -555,7 +577,7 @@ async def async_step_init( except vol.Invalid: errors[CONF_SCAN_INTERVAL] = "scan_interval_too_short" else: - errors = _pattern_errors(options) + errors = _option_errors(options) if not errors: return cast( "ConfigFlowResult", @@ -582,6 +604,7 @@ async def async_step_init( user_agent=str(merged.get(CONF_USER_AGENT) or ""), match=str(merged.get(CONF_MATCH) or ""), exclude_match=str(merged.get(CONF_EXCLUDE_MATCH) or ""), + proxy=str(merged.get(CONF_PROXY) or ""), ) if user_input is not None: # keep what the user typed when showing errors diff --git a/custom_components/feedparser/const.py b/custom_components/feedparser/const.py index 4e0127f0..35d36700 100644 --- a/custom_components/feedparser/const.py +++ b/custom_components/feedparser/const.py @@ -19,6 +19,7 @@ CONF_USER_AGENT = "user_agent" CONF_MATCH = "match" CONF_EXCLUDE_MATCH = "exclude_match" +CONF_PROXY = "proxy" # Entry data flag: True while the entry follows configuration.yaml, False once the # user moved it to the UI. Missing on UI entries and on entries imported by 1.0.0. @@ -45,4 +46,5 @@ CONF_USER_AGENT, CONF_MATCH, CONF_EXCLUDE_MATCH, + CONF_PROXY, ] diff --git a/custom_components/feedparser/coordinator.py b/custom_components/feedparser/coordinator.py index b54ec0b7..79dc3044 100644 --- a/custom_components/feedparser/coordinator.py +++ b/custom_components/feedparser/coordinator.py @@ -20,6 +20,7 @@ CONF_INCLUSIONS, CONF_LOCAL_TIME, CONF_MATCH, + CONF_PROXY, CONF_REMOVE_SUMMARY_IMAGE, CONF_SCAN_INTERVAL, CONF_SHOW_TOPN, @@ -31,7 +32,7 @@ DEFAULT_TOPN, DOMAIN, ) -from .fetch import USER_AGENT +from .fetch import USER_AGENT, redact_text from .reader import FeedReader if TYPE_CHECKING: @@ -81,6 +82,7 @@ def __init__(self, hass: HomeAssistant, entry: FeedparserConfigEntry) -> None: user_agent=config.get(CONF_USER_AGENT) or USER_AGENT, match=config.get(CONF_MATCH) or "", exclude_match=config.get(CONF_EXCLUDE_MATCH) or "", + proxy=config.get(CONF_PROXY) or "", ) super().__init__( hass, @@ -97,5 +99,7 @@ async def _async_update_data(self) -> FeedEntries: try: return await self.hass.async_add_executor_job(self.reader.read) except requests.RequestException as err: - msg = f"Unable to fetch {self.reader.feed}: {err}" + # the error may quote the proxy URL; never log its password + reason = redact_text(str(err), self.reader.proxy) + msg = f"Unable to fetch {self.reader.feed}: {reason}" raise UpdateFailed(msg) from err diff --git a/custom_components/feedparser/fetch.py b/custom_components/feedparser/fetch.py index d3b9350b..b5042c8a 100644 --- a/custom_components/feedparser/fetch.py +++ b/custom_components/feedparser/fetch.py @@ -2,6 +2,8 @@ from __future__ import annotations +from urllib.parse import urlsplit + import requests from requests_file import FileAdapter @@ -13,18 +15,63 @@ # connection but never answers blocks an executor thread indefinitely. REQUEST_TIMEOUT = (10, 30) +# Proxy URL schemes requests understands. The socks* schemes need PySocks; +# socks5h / socks4a also resolve the feed's host name through the proxy. +PROXY_SCHEMES = ("http", "https", "socks4", "socks4a", "socks5", "socks5h") + + +def is_valid_proxy(proxy: str) -> bool: + """Return True if `proxy` looks like scheme://[user:pass@]host:port.""" + try: + parts = urlsplit(proxy) + port = parts.port + except ValueError: + return False + return ( + parts.scheme.lower() in PROXY_SCHEMES + and bool(parts.hostname) + and port is not None + and parts.path in ("", "/") + and not parts.query + and not parts.fragment + ) + + +def redact_proxy(proxy: str) -> str: + """Return `proxy` with any password replaced, safe to log.""" + try: + parts = urlsplit(proxy) + password = parts.password + except ValueError: + return "" + if password is None: + return proxy + netloc = parts.netloc.replace(f":{password}@", ":***@", 1) + return parts._replace(netloc=netloc).geturl() -def fetch_feed(url: str, user_agent: str = USER_AGENT) -> bytes: + +def redact_text(text: str, proxy: str) -> str: + """Remove the proxy password from `text` (e.g. an exception message).""" + if not proxy: + return text + return text.replace(proxy, redact_proxy(proxy)) + + +def fetch_feed(url: str, user_agent: str = USER_AGENT, proxy: str = "") -> bytes: """Download a feed and return its raw bytes. Raw bytes (not ``Response.text``) are returned so that feedparser can detect the document encoding from the XML prolog instead of relying on requests' guess. + When `proxy` is set, http(s) requests go through it instead of any proxy set + in the environment. ``file://`` feeds never use a proxy. + Raises ``requests.RequestException`` on network or HTTP errors. """ with requests.Session() as session: session.mount("file://", FileAdapter()) session.headers.update({"User-Agent": user_agent}) - response = session.get(url, timeout=REQUEST_TIMEOUT) + proxies = {"http": proxy, "https": proxy} if proxy else None + response = session.get(url, timeout=REQUEST_TIMEOUT, proxies=proxies) response.raise_for_status() return response.content diff --git a/custom_components/feedparser/manifest.json b/custom_components/feedparser/manifest.json index 721699d5..fe35fe26 100644 --- a/custom_components/feedparser/manifest.json +++ b/custom_components/feedparser/manifest.json @@ -12,6 +12,7 @@ "issue_tracker": "https://github.com/custom-components/feedparser/issues", "requirements": [ "feedparser>=6.0.12", + "PySocks>=1.7.1", "python-dateutil", "requests-file" ], diff --git a/custom_components/feedparser/reader.py b/custom_components/feedparser/reader.py index 6fa3ac5e..209f221f 100644 --- a/custom_components/feedparser/reader.py +++ b/custom_components/feedparser/reader.py @@ -21,7 +21,7 @@ from dateutil import parser from homeassistant.util import dt -from .fetch import USER_AGENT, fetch_feed +from .fetch import USER_AGENT, fetch_feed, redact_proxy _LOGGER: logging.Logger = logging.getLogger(__name__) @@ -93,7 +93,7 @@ def _looks_like_image(url: str) -> bool: class FeedReader: """Download one feed and build its entries according to the sensor options.""" - def __init__( + def __init__( # noqa: PLR0913 - one keyword per sensor option self: FeedReader, *, feed: str, @@ -107,6 +107,7 @@ def __init__( user_agent: str = USER_AGENT, match: str = "", exclude_match: str = "", + proxy: str = "", ) -> None: """Initialize the reader. @@ -122,6 +123,7 @@ def __init__( self._inclusions = inclusions self._exclusions = exclusions self._user_agent = user_agent + self.proxy = proxy self._match = compile_filter(match) self._exclude_match = compile_filter(exclude_match) @@ -136,7 +138,8 @@ def __repr__(self: FeedReader) -> str: f'user_agent="{self._user_agent}", ' f"match={self._match.pattern if self._match else None!r}, " f"exclude_match=" - f"{self._exclude_match.pattern if self._exclude_match else None!r})" + f"{self._exclude_match.pattern if self._exclude_match else None!r}, " + f"proxy={redact_proxy(self.proxy) if self.proxy else None!r})" ) def read(self: FeedReader) -> list[dict[str, object]]: @@ -150,7 +153,7 @@ def read(self: FeedReader) -> list[dict[str, object]]: _LOGGER.debug("Feed %s: Polling feed data from %s", self.name, self.feed) parsed_feed = cast( "ParsedFeed", - feedparser.parse(fetch_feed(self.feed, self._user_agent)), + feedparser.parse(fetch_feed(self.feed, self._user_agent, self.proxy)), ) if not parsed_feed.entries: _LOGGER.warning("Feed %s: No data received.", self.name) @@ -319,7 +322,7 @@ def _parse_date_string(date: str) -> datetime | None: warnings.simplefilter("ignore") try: fallback = parser.parse(date) - except (ValueError, OverflowError): + except ValueError, OverflowError: return None return fallback if fallback.tzinfo is not None else None diff --git a/custom_components/feedparser/sensor.py b/custom_components/feedparser/sensor.py index 76cab9cd..0a557eba 100644 --- a/custom_components/feedparser/sensor.py +++ b/custom_components/feedparser/sensor.py @@ -19,6 +19,7 @@ CONF_INCLUSIONS, CONF_LOCAL_TIME, CONF_MATCH, + CONF_PROXY, CONF_REMOVE_SUMMARY_IMAGE, CONF_SCAN_INTERVAL, CONF_SHOW_TOPN, @@ -30,7 +31,7 @@ DEFAULT_TOPN, ) from .coordinator import FeedEntries, FeedparserConfigEntry, FeedparserCoordinator -from .fetch import USER_AGENT +from .fetch import USER_AGENT, is_valid_proxy from .reader import compile_filter from .yaml_import import async_import_yaml_sensor @@ -55,6 +56,15 @@ def _valid_pattern(value: str) -> str: return value +def _valid_proxy(value: str) -> str: + """Validate a proxy URL from YAML ("" means no proxy).""" + value = value.strip() + if value and not is_valid_proxy(value): + msg = "invalid proxy URL, expected scheme://[user:password@]host:port" + raise vol.Invalid(msg) + return value + + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_NAME): cv.string, @@ -75,6 +85,7 @@ def _valid_pattern(value: str) -> str: cv.string, _valid_pattern, ), + vol.Optional(CONF_PROXY, default=""): vol.All(cv.string, _valid_proxy), }, ) diff --git a/custom_components/feedparser/strings.json b/custom_components/feedparser/strings.json index 6062bcb5..7be6480c 100644 --- a/custom_components/feedparser/strings.json +++ b/custom_components/feedparser/strings.json @@ -16,7 +16,8 @@ "exclude_match": "Skip entries matching", "inclusions": "Included fields", "exclusions": "Excluded fields", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "Proxy URL" }, "data_description": { "name": "Friendly name for the sensor.", @@ -30,7 +31,8 @@ "exclude_match": "Optional regular expression. Entries whose title or summary contains a match are removed, ignoring case. Applied together with the filter above.", "inclusions": "Optional comma-separated allowlist, for example `title, link, published`. Leave empty to keep all fields except exclusions.", "exclusions": "Optional comma-separated fields to remove. Exclusions are applied after inclusions.", - "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default." + "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default.", + "proxy": "Optional proxy for reaching feeds that are blocked on your network, e.g. socks5h://192.168.1.10:1080 or http://user:password@proxy.local:3128. Supported schemes: http, https, socks4, socks4a, socks5, socks5h (socks5h also resolves the feed's host name through the proxy). Leave empty to connect directly." } }, "reconfigure": { @@ -52,7 +54,8 @@ "invalid_url": "URL is invalid.", "invalid_feed": "The URL was downloaded but does not look like an RSS or Atom feed.", "scan_interval_too_short": "Refresh interval must be at least 1 minute.", - "invalid_pattern": "Not a valid regular expression." + "invalid_pattern": "Not a valid regular expression.", + "invalid_proxy": "Not a valid proxy URL. Use scheme://host:port, e.g. socks5h://192.168.1.10:1080." }, "abort": { "already_configured": "This feed is already configured.", @@ -76,7 +79,8 @@ "exclude_match": "Skip entries matching", "inclusions": "Included fields", "exclusions": "Excluded fields", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "Proxy URL" }, "data_description": { "date_format": "Python strftime format used for feed dates, for example `%a, %d %b %Y %H:%M:%S %Z`.", @@ -88,7 +92,8 @@ "exclude_match": "Optional regular expression. Entries whose title or summary contains a match are removed, ignoring case. Applied together with the filter above.", "inclusions": "Optional comma-separated allowlist, for example `title, link, published`. Leave empty to keep all fields except exclusions.", "exclusions": "Optional comma-separated fields to remove. Exclusions are applied after inclusions.", - "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default." + "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default.", + "proxy": "Optional proxy for reaching feeds that are blocked on your network, e.g. socks5h://192.168.1.10:1080 or http://user:password@proxy.local:3128. Supported schemes: http, https, socks4, socks4a, socks5, socks5h (socks5h also resolves the feed's host name through the proxy). Leave empty to connect directly." } }, "yaml": { @@ -105,7 +110,8 @@ }, "error": { "scan_interval_too_short": "Refresh interval must be at least 1 minute.", - "invalid_pattern": "Not a valid regular expression." + "invalid_pattern": "Not a valid regular expression.", + "invalid_proxy": "Not a valid proxy URL. Use scheme://host:port, e.g. socks5h://192.168.1.10:1080." } }, "issues": { diff --git a/custom_components/feedparser/translations/de.json b/custom_components/feedparser/translations/de.json index 71308efa..7f4cb917 100644 --- a/custom_components/feedparser/translations/de.json +++ b/custom_components/feedparser/translations/de.json @@ -16,7 +16,8 @@ "exclude_match": "Passende Einträge überspringen", "inclusions": "Einbezogene Felder", "exclusions": "Ausgeschlossene Felder", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "Proxy-URL" }, "data_description": { "name": "Anzeigename für den Sensor.", @@ -30,7 +31,8 @@ "exclude_match": "Optionaler regulärer Ausdruck. Einträge, deren Titel oder Zusammenfassung einen Treffer enthält, werden entfernt (Groß-/Kleinschreibung egal). Wird zusammen mit dem Filter oben angewendet.", "inclusions": "Optionale, durch Kommas getrennte Positivliste, zum Beispiel `title, link, published`. Leer lassen, um alle Felder außer den ausgeschlossenen beizubehalten.", "exclusions": "Optionale, durch Kommas getrennte Felder, die entfernt werden sollen. Ausschlüsse werden nach den Einschlüssen angewendet.", - "user_agent": "Optionaler HTTP-User-Agent, der beim Abrufen des Feeds gesendet wird. Manche Server blockieren unbekannte Clients oder antworten nicht; verwende dafür einen Browser-User-Agent. Leer lassen für den Standardwert." + "user_agent": "Optionaler HTTP-User-Agent, der beim Abrufen des Feeds gesendet wird. Manche Server blockieren unbekannte Clients oder antworten nicht; verwende dafür einen Browser-User-Agent. Leer lassen für den Standardwert.", + "proxy": "Optionaler Proxy, um Feeds zu erreichen, die in deinem Netzwerk blockiert sind, z. B. socks5h://192.168.1.10:1080 oder http://benutzer:passwort@proxy.local:3128. Unterstützte Schemata: http, https, socks4, socks4a, socks5, socks5h (socks5h löst auch den Hostnamen des Feeds über den Proxy auf). Leer lassen für eine direkte Verbindung." } }, "reconfigure": { @@ -52,7 +54,8 @@ "invalid_url": "Die URL ist ungültig.", "invalid_feed": "Die URL wurde geladen, scheint aber kein RSS- oder Atom-Feed zu sein.", "scan_interval_too_short": "Das Aktualisierungsintervall muss mindestens 1 Minute betragen.", - "invalid_pattern": "Kein gültiger regulärer Ausdruck." + "invalid_pattern": "Kein gültiger regulärer Ausdruck.", + "invalid_proxy": "Keine gültige Proxy-URL. Verwende schema://host:port, z. B. socks5h://192.168.1.10:1080." }, "abort": { "already_configured": "Dieser Feed ist bereits konfiguriert.", @@ -76,7 +79,8 @@ "exclude_match": "Passende Einträge überspringen", "inclusions": "Einbezogene Felder", "exclusions": "Ausgeschlossene Felder", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "Proxy-URL" }, "data_description": { "date_format": "Python-strftime-Format für Feed-Datumswerte, zum Beispiel `%a, %d %b %Y %H:%M:%S %Z`.", @@ -88,7 +92,8 @@ "exclude_match": "Optionaler regulärer Ausdruck. Einträge, deren Titel oder Zusammenfassung einen Treffer enthält, werden entfernt (Groß-/Kleinschreibung egal). Wird zusammen mit dem Filter oben angewendet.", "inclusions": "Optionale, durch Kommas getrennte Positivliste, zum Beispiel `title, link, published`. Leer lassen, um alle Felder außer den ausgeschlossenen beizubehalten.", "exclusions": "Optionale, durch Kommas getrennte Felder, die entfernt werden sollen. Ausschlüsse werden nach den Einschlüssen angewendet.", - "user_agent": "Optionaler HTTP-User-Agent, der beim Abrufen des Feeds gesendet wird. Manche Server blockieren unbekannte Clients oder antworten nicht; verwende dafür einen Browser-User-Agent. Leer lassen für den Standardwert." + "user_agent": "Optionaler HTTP-User-Agent, der beim Abrufen des Feeds gesendet wird. Manche Server blockieren unbekannte Clients oder antworten nicht; verwende dafür einen Browser-User-Agent. Leer lassen für den Standardwert.", + "proxy": "Optionaler Proxy, um Feeds zu erreichen, die in deinem Netzwerk blockiert sind, z. B. socks5h://192.168.1.10:1080 oder http://benutzer:passwort@proxy.local:3128. Unterstützte Schemata: http, https, socks4, socks4a, socks5, socks5h (socks5h löst auch den Hostnamen des Feeds über den Proxy auf). Leer lassen für eine direkte Verbindung." } }, "yaml": { @@ -105,7 +110,8 @@ }, "error": { "scan_interval_too_short": "Das Aktualisierungsintervall muss mindestens 1 Minute betragen.", - "invalid_pattern": "Kein gültiger regulärer Ausdruck." + "invalid_pattern": "Kein gültiger regulärer Ausdruck.", + "invalid_proxy": "Keine gültige Proxy-URL. Verwende schema://host:port, z. B. socks5h://192.168.1.10:1080." } }, "issues": { diff --git a/custom_components/feedparser/translations/el.json b/custom_components/feedparser/translations/el.json index 553ef796..4fa26a04 100644 --- a/custom_components/feedparser/translations/el.json +++ b/custom_components/feedparser/translations/el.json @@ -16,7 +16,8 @@ "exclude_match": "Παράλειψη καταχωρήσεων που ταιριάζουν", "inclusions": "Συμπεριλαμβανόμενα πεδία", "exclusions": "Εξαιρούμενα πεδία", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "URL διακομιστή μεσολάβησης" }, "data_description": { "name": "Φιλικό όνομα για τον αισθητήρα.", @@ -30,7 +31,8 @@ "exclude_match": "Προαιρετική κανονική έκφραση. Οι καταχωρήσεις των οποίων ο τίτλος ή η περίληψη περιέχει ταίριασμα αφαιρούνται, χωρίς διάκριση πεζών-κεφαλαίων. Εφαρμόζεται μαζί με το παραπάνω φίλτρο.", "inclusions": "Προαιρετική λίστα επιτρεπόμενων πεδίων χωρισμένων με κόμμα, για παράδειγμα `title, link, published`. Αφήστε το κενό για να διατηρηθούν όλα τα πεδία εκτός από όσα εξαιρούνται.", "exclusions": "Προαιρετικά πεδία προς αφαίρεση, χωρισμένα με κόμμα. Οι εξαιρέσεις εφαρμόζονται μετά τις συμπεριλήψεις.", - "user_agent": "Προαιρετικό HTTP User-Agent που αποστέλλεται κατά τη λήψη της ροής. Ορισμένοι διακομιστές αποκλείουν ή δεν απαντούν σε άγνωστους πελάτες· ορίστε ένα User-Agent προγράμματος περιήγησης για αυτούς. Αφήστε το κενό για την προεπιλογή." + "user_agent": "Προαιρετικό HTTP User-Agent που αποστέλλεται κατά τη λήψη της ροής. Ορισμένοι διακομιστές αποκλείουν ή δεν απαντούν σε άγνωστους πελάτες· ορίστε ένα User-Agent προγράμματος περιήγησης για αυτούς. Αφήστε το κενό για την προεπιλογή.", + "proxy": "Προαιρετικός διακομιστής μεσολάβησης (proxy) για πρόσβαση σε ροές που είναι αποκλεισμένες στο δίκτυό σας, π.χ. socks5h://192.168.1.10:1080 ή http://user:password@proxy.local:3128. Υποστηριζόμενα σχήματα: http, https, socks4, socks4a, socks5, socks5h (το socks5h επιλύει επίσης το όνομα του διακομιστή της ροής μέσω του proxy). Αφήστε το κενό για απευθείας σύνδεση." } }, "reconfigure": { @@ -52,7 +54,8 @@ "invalid_url": "Το URL δεν είναι έγκυρο.", "invalid_feed": "Η διεύθυνση URL φορτώθηκε, αλλά δεν φαίνεται να είναι ροή RSS ή Atom.", "scan_interval_too_short": "Το διάστημα ανανέωσης πρέπει να είναι τουλάχιστον 1 λεπτό.", - "invalid_pattern": "Δεν είναι έγκυρη κανονική έκφραση." + "invalid_pattern": "Δεν είναι έγκυρη κανονική έκφραση.", + "invalid_proxy": "Μη έγκυρο URL διακομιστή μεσολάβησης. Χρησιμοποιήστε scheme://host:port, π.χ. socks5h://192.168.1.10:1080." }, "abort": { "already_configured": "Αυτή η ροή έχει ήδη ρυθμιστεί.", @@ -76,7 +79,8 @@ "exclude_match": "Παράλειψη καταχωρήσεων που ταιριάζουν", "inclusions": "Συμπεριλαμβανόμενα πεδία", "exclusions": "Εξαιρούμενα πεδία", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "URL διακομιστή μεσολάβησης" }, "data_description": { "date_format": "Μορφή Python strftime που χρησιμοποιείται για τις ημερομηνίες της ροής, για παράδειγμα `%a, %d %b %Y %H:%M:%S %Z`.", @@ -88,7 +92,8 @@ "exclude_match": "Προαιρετική κανονική έκφραση. Οι καταχωρήσεις των οποίων ο τίτλος ή η περίληψη περιέχει ταίριασμα αφαιρούνται, χωρίς διάκριση πεζών-κεφαλαίων. Εφαρμόζεται μαζί με το παραπάνω φίλτρο.", "inclusions": "Προαιρετική λίστα επιτρεπόμενων πεδίων χωρισμένων με κόμμα, για παράδειγμα `title, link, published`. Αφήστε το κενό για να διατηρηθούν όλα τα πεδία εκτός από όσα εξαιρούνται.", "exclusions": "Προαιρετικά πεδία προς αφαίρεση, χωρισμένα με κόμμα. Οι εξαιρέσεις εφαρμόζονται μετά τις συμπεριλήψεις.", - "user_agent": "Προαιρετικό HTTP User-Agent που αποστέλλεται κατά τη λήψη της ροής. Ορισμένοι διακομιστές αποκλείουν ή δεν απαντούν σε άγνωστους πελάτες· ορίστε ένα User-Agent προγράμματος περιήγησης για αυτούς. Αφήστε το κενό για την προεπιλογή." + "user_agent": "Προαιρετικό HTTP User-Agent που αποστέλλεται κατά τη λήψη της ροής. Ορισμένοι διακομιστές αποκλείουν ή δεν απαντούν σε άγνωστους πελάτες· ορίστε ένα User-Agent προγράμματος περιήγησης για αυτούς. Αφήστε το κενό για την προεπιλογή.", + "proxy": "Προαιρετικός διακομιστής μεσολάβησης (proxy) για πρόσβαση σε ροές που είναι αποκλεισμένες στο δίκτυό σας, π.χ. socks5h://192.168.1.10:1080 ή http://user:password@proxy.local:3128. Υποστηριζόμενα σχήματα: http, https, socks4, socks4a, socks5, socks5h (το socks5h επιλύει επίσης το όνομα του διακομιστή της ροής μέσω του proxy). Αφήστε το κενό για απευθείας σύνδεση." } }, "yaml": { @@ -105,7 +110,8 @@ }, "error": { "scan_interval_too_short": "Το διάστημα ανανέωσης πρέπει να είναι τουλάχιστον 1 λεπτό.", - "invalid_pattern": "Δεν είναι έγκυρη κανονική έκφραση." + "invalid_pattern": "Δεν είναι έγκυρη κανονική έκφραση.", + "invalid_proxy": "Μη έγκυρο URL διακομιστή μεσολάβησης. Χρησιμοποιήστε scheme://host:port, π.χ. socks5h://192.168.1.10:1080." } }, "issues": { diff --git a/custom_components/feedparser/translations/en.json b/custom_components/feedparser/translations/en.json index 6062bcb5..7be6480c 100644 --- a/custom_components/feedparser/translations/en.json +++ b/custom_components/feedparser/translations/en.json @@ -16,7 +16,8 @@ "exclude_match": "Skip entries matching", "inclusions": "Included fields", "exclusions": "Excluded fields", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "Proxy URL" }, "data_description": { "name": "Friendly name for the sensor.", @@ -30,7 +31,8 @@ "exclude_match": "Optional regular expression. Entries whose title or summary contains a match are removed, ignoring case. Applied together with the filter above.", "inclusions": "Optional comma-separated allowlist, for example `title, link, published`. Leave empty to keep all fields except exclusions.", "exclusions": "Optional comma-separated fields to remove. Exclusions are applied after inclusions.", - "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default." + "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default.", + "proxy": "Optional proxy for reaching feeds that are blocked on your network, e.g. socks5h://192.168.1.10:1080 or http://user:password@proxy.local:3128. Supported schemes: http, https, socks4, socks4a, socks5, socks5h (socks5h also resolves the feed's host name through the proxy). Leave empty to connect directly." } }, "reconfigure": { @@ -52,7 +54,8 @@ "invalid_url": "URL is invalid.", "invalid_feed": "The URL was downloaded but does not look like an RSS or Atom feed.", "scan_interval_too_short": "Refresh interval must be at least 1 minute.", - "invalid_pattern": "Not a valid regular expression." + "invalid_pattern": "Not a valid regular expression.", + "invalid_proxy": "Not a valid proxy URL. Use scheme://host:port, e.g. socks5h://192.168.1.10:1080." }, "abort": { "already_configured": "This feed is already configured.", @@ -76,7 +79,8 @@ "exclude_match": "Skip entries matching", "inclusions": "Included fields", "exclusions": "Excluded fields", - "user_agent": "User-Agent" + "user_agent": "User-Agent", + "proxy": "Proxy URL" }, "data_description": { "date_format": "Python strftime format used for feed dates, for example `%a, %d %b %Y %H:%M:%S %Z`.", @@ -88,7 +92,8 @@ "exclude_match": "Optional regular expression. Entries whose title or summary contains a match are removed, ignoring case. Applied together with the filter above.", "inclusions": "Optional comma-separated allowlist, for example `title, link, published`. Leave empty to keep all fields except exclusions.", "exclusions": "Optional comma-separated fields to remove. Exclusions are applied after inclusions.", - "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default." + "user_agent": "Optional HTTP User-Agent sent when fetching the feed. Some servers block or stall unknown clients; set a browser User-Agent for those. Leave empty to use the default.", + "proxy": "Optional proxy for reaching feeds that are blocked on your network, e.g. socks5h://192.168.1.10:1080 or http://user:password@proxy.local:3128. Supported schemes: http, https, socks4, socks4a, socks5, socks5h (socks5h also resolves the feed's host name through the proxy). Leave empty to connect directly." } }, "yaml": { @@ -105,7 +110,8 @@ }, "error": { "scan_interval_too_short": "Refresh interval must be at least 1 minute.", - "invalid_pattern": "Not a valid regular expression." + "invalid_pattern": "Not a valid regular expression.", + "invalid_proxy": "Not a valid proxy URL. Use scheme://host:port, e.g. socks5h://192.168.1.10:1080." } }, "issues": { diff --git a/pyproject.toml b/pyproject.toml index 8e270c76..d0dd8ccb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ requires-python = ">=3.14" dependencies = [ "feedparser>=6.0.12", "homeassistant", + "PySocks>=1.7.1", "python-dateutil", "requests-file", "requests" diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index a6c8831c..e8468661 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -92,4 +92,4 @@ def test_validate_feed_url_sends_user_agent( return_value=feed.read_bytes(), ) as fetch: FeedparserConfigFlow._validate_feed_url(*args) # noqa: SLF001 - fetch.assert_called_once_with(feed.as_uri(), expected) + fetch.assert_called_once_with(feed.as_uri(), expected, "") diff --git a/tests/test_proxy.py b/tests/test_proxy.py new file mode 100644 index 00000000..1c98dc7d --- /dev/null +++ b/tests/test_proxy.py @@ -0,0 +1,437 @@ +"""Tests for fetching feeds through an HTTP or SOCKS proxy (issue #27).""" + +from __future__ import annotations + +import socket +import socketserver +import struct +import threading +from collections.abc import Iterator +from pathlib import Path +from typing import TYPE_CHECKING, Any +from unittest.mock import patch + +import pytest +import requests +from homeassistant import config_entries +from homeassistant.const import CONF_NAME +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.setup import async_setup_component +from pytest_homeassistant_custom_component.common import MockConfigEntry + +from custom_components.feedparser import fetch +from custom_components.feedparser.const import ( + CONF_DATE_FORMAT, + CONF_EXCLUSIONS, + CONF_FEED_URL, + CONF_INCLUSIONS, + CONF_LOCAL_TIME, + CONF_PROXY, + CONF_REMOVE_SUMMARY_IMAGE, + CONF_SCAN_INTERVAL, + CONF_SHOW_TOPN, + CONF_USER_AGENT, + DOMAIN, + ENTRY_VERSION, +) +from custom_components.feedparser.reader import FeedReader + +if TYPE_CHECKING: + from homeassistant.core import HomeAssistant + +FEED = ( + b'' + b"Thttps://e.xT" + b"through the proxyhttps://e.x/1" + b"" +) +# .invalid never resolves (RFC 6761), so these feeds are only reachable when the +# proxy, not this machine, handles the host name. +BLOCKED_URL = "http://blocked.invalid/rss" + + +# --- tiny local proxies ------------------------------------------------------- + + +class _Server(socketserver.ThreadingTCPServer): + # handler threads are joined on close so none outlive the test + daemon_threads = False + block_on_close = True + allow_reuse_address = True + seen: list[str] + + +def _serve_feed(conn: socket.socket) -> None: + """Answer one HTTP request on `conn` with the test feed.""" + data = b"" + while b"\r\n\r\n" not in data: + chunk = conn.recv(4096) + if not chunk: + return + data += chunk + conn.sendall( + b"HTTP/1.1 200 OK\r\nContent-Type: application/rss+xml\r\n" + b"Content-Length: " + str(len(FEED)).encode() + b"\r\n" + b"Connection: close\r\n\r\n" + FEED, + ) + + +class _HttpProxyHandler(socketserver.BaseRequestHandler): + """Forward-proxy stand-in: records the request line and serves the feed.""" + + def handle(self) -> None: + conn: socket.socket = self.request + first = conn.recv(4096, socket.MSG_PEEK).split(b"\r\n", 1)[0] + self.server.seen.append(first.decode()) # type: ignore[attr-defined] + _serve_feed(conn) + + +class _Socks5Handler(socketserver.BaseRequestHandler): + """Minimal SOCKS5 server (no auth, CONNECT only) that serves the feed itself. + + It records the requested destination instead of connecting to it. + """ + + def handle(self) -> None: + conn: socket.socket = self.request + _, n_methods = conn.recv(2) + conn.recv(n_methods) + conn.sendall(b"\x05\x00") # no authentication + _, cmd, _, atyp = conn.recv(4) + if atyp == 3: # domain name: resolved by the proxy (socks5h) # noqa: PLR2004 + host = conn.recv(conn.recv(1)[0]).decode() + else: # IPv4: already resolved by the client (socks5) + host = socket.inet_ntoa(conn.recv(4)) + (port,) = struct.unpack(">H", conn.recv(2)) + self.server.seen.append(f"{cmd} {host}:{port}") # type: ignore[attr-defined] + conn.sendall(b"\x05\x00\x00\x01" + socket.inet_aton("127.0.0.1") + b"\0\0") + _serve_feed(conn) + + +def _start(handler: type[socketserver.BaseRequestHandler]) -> Iterator[_Server]: + server = _Server(("127.0.0.1", 0), handler) + server.seen = [] + thread = threading.Thread(target=server.serve_forever) + thread.start() + try: + yield server + finally: + server.shutdown() + server.server_close() + thread.join() + + +# The Home Assistant test plugin blocks sockets; these fixtures need real ones on +# 127.0.0.1 to act as the proxy. +@pytest.fixture +def http_proxy(socket_enabled: None) -> Iterator[_Server]: # noqa: ARG001 + """Run a local HTTP forward proxy.""" + yield from _start(_HttpProxyHandler) + + +@pytest.fixture +def socks_proxy(socket_enabled: None) -> Iterator[_Server]: # noqa: ARG001 + """Run a local SOCKS5 proxy.""" + yield from _start(_Socks5Handler) + + +def _url(scheme: str, server: _Server) -> str: + host, port = server.server_address[:2] + return f"{scheme}://{host}:{port}" + + +# --- proxy URL helpers -------------------------------------------------------- + + +@pytest.mark.parametrize( + "proxy", + [ + "http://proxy.local:3128", + "https://proxy.local:443", + "socks4://10.0.0.1:1080", + "socks4a://10.0.0.1:1080", + "socks5://10.0.0.1:1080", + "SOCKS5H://10.0.0.1:1080/", + "http://user:p%40ss@proxy.local:3128", + "socks5h://[::1]:1080", + ], +) +def test_valid_proxies(proxy: str) -> None: + """Supported schemes with a host and port are accepted.""" + assert fetch.is_valid_proxy(proxy) + + +@pytest.mark.parametrize( + "proxy", + [ + "proxy.local:3128", # no scheme + "ftp://proxy.local:21", + "socks5://10.0.0.1", # no port + "http://:3128", # no host + "http://proxy.local:99999", + "http://proxy.local:3128/path", + "http://proxy.local:3128?x=1", + "http://[::1:3128", + ], +) +def test_invalid_proxies(proxy: str) -> None: + """Anything requests could not use as a proxy is rejected.""" + assert not fetch.is_valid_proxy(proxy) + + +def test_redact_proxy_hides_password() -> None: + """Passwords are masked; URLs without one are unchanged.""" + assert ( + fetch.redact_proxy("http://bob:s3cret@proxy.local:3128") + == "http://bob:***@proxy.local:3128" + ) + assert fetch.redact_proxy("socks5h://h:1080") == "socks5h://h:1080" + assert fetch.redact_text( + "failed via http://bob:s3cret@p:1 ok", + "http://bob:s3cret@p:1", + ) == ("failed via http://bob:***@p:1 ok") + assert fetch.redact_text("unchanged", "") == "unchanged" + + +def test_reader_repr_hides_proxy_password() -> None: + """The reader's repr (used in debug logs) does not show the password.""" + reader = FeedReader( + feed="https://e.x/rss", + name="n", + date_format="%Y", + show_topn=1, + remove_summary_image=False, + inclusions=[], + exclusions=[], + local_time=False, + proxy="http://bob:s3cret@proxy.local:3128", + ) + assert "s3cret" not in repr(reader) + assert "http://bob:***@proxy.local:3128" in repr(reader) + + +# --- fetching ----------------------------------------------------------------- + + +def test_fetch_without_proxy_passes_none() -> None: + """No proxy configured keeps requests' default (environment) behavior.""" + with patch.object(requests.Session, "get") as get: + get.return_value.content = FEED + fetch.fetch_feed("https://example.com/feed") + assert get.call_args.kwargs["proxies"] is None + + +def test_fetch_passes_proxy_for_both_schemes() -> None: + """The proxy is used for http and https feeds.""" + with patch.object(requests.Session, "get") as get: + get.return_value.content = FEED + fetch.fetch_feed("https://example.com/feed", proxy="socks5h://h:1080") + assert get.call_args.kwargs["proxies"] == { + "http": "socks5h://h:1080", + "https": "socks5h://h:1080", + } + + +def test_fetch_through_http_proxy(http_proxy: _Server) -> None: + """A feed this machine cannot resolve is fetched through an HTTP proxy.""" + content = fetch.fetch_feed(BLOCKED_URL, proxy=_url("http", http_proxy)) + assert content == FEED + assert http_proxy.seen == [f"GET {BLOCKED_URL} HTTP/1.1"] + + +def test_fetch_through_socks5h_proxy(socks_proxy: _Server) -> None: + """socks5h hands the host name to the proxy, so blocked DNS does not matter.""" + content = fetch.fetch_feed(BLOCKED_URL, proxy=_url("socks5h", socks_proxy)) + assert content == FEED + assert socks_proxy.seen == ["1 blocked.invalid:80"] + + +def test_file_feed_ignores_proxy(tmp_path: Path) -> None: + """Local file:// feeds never go through the proxy.""" + feed = tmp_path / "feed.xml" + feed.write_bytes(FEED) + # an unusable proxy: the fetch would fail if it were used + content = fetch.fetch_feed(feed.as_uri(), proxy="http://127.0.0.1:9") + assert content == FEED + + +# --- Home Assistant flows ----------------------------------------------------- + +pytestmark = pytest.mark.usefixtures("enable_custom_integrations") + + +def _options(**overrides: Any) -> dict[str, Any]: # noqa: ANN401 + return { + CONF_DATE_FORMAT: "%Y-%m-%d", + CONF_LOCAL_TIME: False, + CONF_SCAN_INTERVAL: {"hours": 1, "minutes": 0}, + CONF_SHOW_TOPN: 10, + CONF_REMOVE_SUMMARY_IMAGE: False, + CONF_INCLUSIONS: "title", + CONF_EXCLUSIONS: "", + CONF_USER_AGENT: "", + } | overrides + + +async def _start_user_flow(hass: HomeAssistant, **overrides: Any) -> Any: # noqa: ANN401 + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_USER}, + ) + return await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_NAME: "Blocked Feed", CONF_FEED_URL: BLOCKED_URL} | _options(**overrides), + ) + + +async def test_user_flow_through_proxy( + hass: HomeAssistant, + http_proxy: _Server, +) -> None: + """Setup validates through the proxy, and the sensor then polls through it.""" + proxy = _url("http", http_proxy) + result = await _start_user_flow(hass, **{CONF_PROXY: f" {proxy} "}) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["options"][CONF_PROXY] == proxy + await hass.async_block_till_done() + + state = hass.states.get("sensor.blocked_feed") + assert state is not None + assert state.state == "1" + assert state.attributes["entries"][0]["title"] == "through the proxy" + # one request to validate, one for the first refresh + assert len(http_proxy.seen) == 2 # noqa: PLR2004 + + +async def test_user_flow_rejects_invalid_proxy(hass: HomeAssistant) -> None: + """A malformed proxy is reported on its field before anything is fetched.""" + with patch( + "custom_components.feedparser.config_flow.fetch_feed", + ) as fetch_feed: + result = await _start_user_flow(hass, **{CONF_PROXY: "proxy.local:3128"}) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {CONF_PROXY: "invalid_proxy"} + fetch_feed.assert_not_called() + + +async def test_user_flow_unreachable_proxy(hass: HomeAssistant) -> None: + """A proxy that cannot be reached shows the usual cannot_connect error.""" + proxy = "http://127.0.0.1:9" + with patch( + "custom_components.feedparser.config_flow.fetch_feed", + side_effect=requests.exceptions.ProxyError("unreachable"), + ) as fetch_feed: + result = await _start_user_flow(hass, **{CONF_PROXY: proxy}) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "cannot_connect"} + fetch_feed.assert_called_once_with(BLOCKED_URL, fetch.USER_AGENT, proxy) + + +async def test_options_set_and_validate_proxy( + hass: HomeAssistant, + tmp_path: Path, +) -> None: + """Options reject a malformed proxy and store a valid one.""" + feed = tmp_path / "feed.xml" + feed.write_bytes(FEED) + entry = MockConfigEntry( + domain=DOMAIN, + version=ENTRY_VERSION, + title="Test Feed", + data={CONF_NAME: "Test Feed", CONF_FEED_URL: feed.as_uri()}, + options={**_options(), CONF_INCLUSIONS: ["title"], CONF_EXCLUSIONS: []}, + ) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(entry.entry_id) + result = await hass.config_entries.options.async_configure( + result["flow_id"], + _options(**{CONF_PROXY: "socks5://no-port"}), + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {CONF_PROXY: "invalid_proxy"} + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + _options(**{CONF_PROXY: "socks5h://10.0.0.1:1080"}), + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + await hass.async_block_till_done() + assert entry.options[CONF_PROXY] == "socks5h://10.0.0.1:1080" + + +async def test_yaml_proxy_is_imported( + hass: HomeAssistant, + socks_proxy: _Server, +) -> None: + """`proxy` from YAML is stored and used by the sensor.""" + proxy = _url("socks5h", socks_proxy) + assert await async_setup_component( + hass, + "sensor", + { + "sensor": [ + { + "platform": DOMAIN, + "name": "Yaml Feed", + "feed_url": BLOCKED_URL, + "proxy": proxy, + }, + ], + }, + ) + await hass.async_block_till_done() + + entry = hass.config_entries.async_entries(DOMAIN)[0] + assert entry.options[CONF_PROXY] == proxy + state = hass.states.get("sensor.yaml_feed") + assert state is not None + assert state.state == "1" + assert socks_proxy.seen == ["1 blocked.invalid:80"] + + +async def test_fetch_error_does_not_log_proxy_password( + hass: HomeAssistant, + tmp_path: Path, + caplog: pytest.LogCaptureFixture, +) -> None: + """A failing poll's message has the proxy password masked.""" + proxy = "http://bob:s3cret@127.0.0.1:9" + feed = tmp_path / "feed.xml" + feed.write_bytes(FEED) + entry = MockConfigEntry( + domain=DOMAIN, + version=ENTRY_VERSION, + title="Test Feed", + data={CONF_NAME: "Test Feed", CONF_FEED_URL: feed.as_uri()}, + options={ + **_options(), + CONF_INCLUSIONS: ["title"], + CONF_EXCLUSIONS: [], + CONF_PROXY: proxy, + }, + ) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + with patch( + "custom_components.feedparser.reader.fetch_feed", + side_effect=requests.exceptions.ProxyError(f"Cannot connect to {proxy}"), + ): + await entry.runtime_data.async_refresh() + await hass.async_block_till_done() + + assert entry.runtime_data.last_update_success is False + # the failure Home Assistant reports (log and entity) has the password masked + reported = [ + r.getMessage() for r in caplog.records if "Unable to fetch" in r.getMessage() + ] + assert reported + assert all("s3cret" not in m for m in reported) + assert "http://bob:***@127.0.0.1:9" in reported[-1] + error = entry.runtime_data.last_exception + assert error is not None + assert "s3cret" not in str(error) From 73341f90348b47a5b838ae121fef5137d8c7216a Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Sun, 15 Nov 2026 11:32:12 -0600 Subject: [PATCH 2/3] Type the proxy test flow helper instead of using Any The pre-commit ruff hook in CI reformats the old signature and then flags ANN401 on **overrides. Co-Authored-By: Claude Opus 5.5 --- tests/test_proxy.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 1c98dc7d..0e1760e4 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -37,6 +37,7 @@ from custom_components.feedparser.reader import FeedReader if TYPE_CHECKING: + from homeassistant.config_entries import ConfigFlowResult from homeassistant.core import HomeAssistant FEED = ( @@ -273,7 +274,7 @@ def _options(**overrides: Any) -> dict[str, Any]: # noqa: ANN401 } | overrides -async def _start_user_flow(hass: HomeAssistant, **overrides: Any) -> Any: # noqa: ANN401 +async def _start_user_flow(hass: HomeAssistant, **overrides: str) -> ConfigFlowResult: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, From d50188b8c19bf12e1c0eb9097226449177dce02d Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Sun, 15 Nov 2026 11:37:08 -0600 Subject: [PATCH 3/3] Build the test proxy URL from its port only mypy in CI types server_address as str | bytes, so formatting the host tripped str-bytes-safe. The test servers always bind to 127.0.0.1. Co-Authored-By: Claude Opus 5.5 --- tests/test_proxy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 0e1760e4..dda017b8 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -137,8 +137,9 @@ def socks_proxy(socket_enabled: None) -> Iterator[_Server]: # noqa: ARG001 def _url(scheme: str, server: _Server) -> str: - host, port = server.server_address[:2] - return f"{scheme}://{host}:{port}" + # bound to 127.0.0.1 in _start, so only the port varies + port = server.server_address[1] + return f"{scheme}://127.0.0.1:{port}" # --- proxy URL helpers --------------------------------------------------------