Skip to content

reqs: improve stathost detection#606

Merged
rofl0r merged 4 commits intotinyproxy:masterfrom
rofl0r:stathost_check
Apr 17, 2026
Merged

reqs: improve stathost detection#606
rofl0r merged 4 commits intotinyproxy:masterfrom
rofl0r:stathost_check

Conversation

@rofl0r
Copy link
Copy Markdown
Contributor

@rofl0r rofl0r commented Apr 17, 2026

  • check stathost before transparent proxy check, else stathost might be misdetected as a trans host request.
  • rather than just the URL in the request, check "Host" header first. if it exists, we only check it for a match, else we extract the host from the url and check that. for both cases we need to exclude trailing port specs, and a trailing path in case it's an URL.

this should make it easier to access the stathost, for example by injecting a host header into a curl command line with -H: $ curl -H "Host: tinyproxy.stats" 127.0.0.1:8080

the stathost can also be specified as an ip address, e.g. Stathost "127.0.0.10" + a separate Listen statement for that ip. in such a case e.g.
$ curl http://127.0.0.10:8080
would work too, even if curl didn't add a Host header (but it does anyway).

- check stathost before transparent proxy check, else stathost
  might be misdetected as a trans host request.
- rather than just the URL in the request, check "Host" header first.
  if it exists, we only check it for a match, else we extract the host
  from the url and check that.
  for both cases we need to exclude trailing port specs, and a trailing
  path in case it's an URL.

this should make it easier to access the stathost, for example by
injecting a host header into a curl command line with -H:
$ curl -H "Host: tinyproxy.stats" 127.0.0.1:8080

the stathost can also be specified as an ip address, e.g.
Stathost "127.0.0.10" + a separate Listen statement for that ip.
in such a case e.g.
$ curl http://127.0.0.10:8080
would work too, even if curl didn't add a Host header (but it does anyway).
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves Tinyproxy’s “stathost” (stats page) request detection by checking it earlier in request processing and by matching against the Host header (when present) instead of relying only on the parsed request host later in the flow.

Changes:

  • Adds helper functions to detect stathost requests from either the Host header or an absolute-form URL.
  • Moves stathost detection earlier in process_request() (before reverse/transparent-proxy handling).
  • Removes the later strcmp(config->stathost, request->host) stathost check.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/reqs.c Outdated
Comment thread src/reqs.c
Comment thread src/reqs.c Outdated
Comment on lines +334 to +338
static int check_stathost(char* hosts_header, char *url) {
/* if the Host: header was passed it's the only source of truth */
if (hosts_header) return is_stathost(hosts_header);
return !strncasecmp (url, "http://", 7) && is_stathost(url + 7);
}
Comment thread src/reqs.c Outdated
use an early stathost check for the host header, in case it exists,
and stick to comparing the hostname extracted from the url after
the transparent proxy logic executed.
this simplifies parsing in is_stathost and makes sure that a trans
request that resolves to the stathost still ends up there.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts Tinyproxy’s request parsing flow to more reliably detect requests intended for the configured StatHost, prioritizing the Host header and performing the stathost check earlier to avoid transparent-proxy misclassification.

Changes:

  • Added an is_stathost() helper to match a host value against config->stathost while tolerating an optional trailing :port.
  • Moved/added stathost detection to occur earlier in process_request() using the Host header.
  • Kept a secondary stathost check using the hostname extracted from the request URL.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/reqs.c Outdated
Comment thread src/reqs.c
Comment on lines +401 to +405
if (is_stathost (pseudomap_find (hashofheaders, "host"))) {
got_stathost:
log_message (LOG_NOTICE, "Request for the stathost.");
connptr->show_stats = TRUE;
goto fail;
Comment thread src/reqs.c
Comment on lines +401 to +406
if (is_stathost (pseudomap_find (hashofheaders, "host"))) {
got_stathost:
log_message (LOG_NOTICE, "Request for the stathost.");
connptr->show_stats = TRUE;
goto fail;
}
Comment thread src/reqs.c Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves Tinyproxy “stathost” detection by checking the Host header first (and earlier in request processing) to avoid misclassification as transparent-proxy traffic and to support accessing the stats page via injected Host headers.

Changes:

  • Add an is_stathost() helper to match stathost with an optional :port suffix.
  • Check for stathost via the Host header early in process_request() (before reverse/transparent proxy handling).
  • Reuse the updated stathost matching for basic-auth handling when selecting Authorization vs Proxy-Authorization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/reqs.c Outdated
Comment on lines +321 to +327
static int is_stathost(const char* host) {
const char *p = config->stathost, *q = host;
if (!p || !q) return 0;
while (*p && *(p++) == *(q++));
return *p == 0 && (*q == 0 || *q == ':');
}

Comment thread src/reqs.c
Comment on lines +396 to +404
/*
* Check to see if they're requesting the stat host
*/
if (is_stathost (pseudomap_find (hashofheaders, "host"))) {
got_stathost:
log_message (LOG_NOTICE, "Request for the stathost.");
connptr->show_stats = TRUE;
goto fail;
}
Comment thread src/reqs.c Outdated
Comment on lines +321 to +324
static int is_stathost(const char* host) {
const char *p = config->stathost, *q = host;
if (!p || !q) return 0;
while (*p && *(p++) == *(q++));
rofl0r added 2 commits April 17, 2026 21:33
by checking whether the entire stathost name matches the start
of the hostname passed, if we encounter a colon after the name we
can be sure it's for the port - by using strchr instead a colon
could be part of an ipv6 string.
@rofl0r rofl0r merged commit 09312a1 into tinyproxy:master Apr 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants