Skip to content

Commit

Permalink
cachemgr.cgi: Add validation for hostname parameter (squid-cache#504)
Browse files Browse the repository at this point in the history
Prevention of HTML/invalid chars in host param
  • Loading branch information
aaron-costello authored and yadij committed Nov 5, 2019
1 parent 9707113 commit 5a90b4c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/base/CharacterSet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include "squid.h"
#include "CharacterSet.h"
#include "base/CharacterSet.h"

#include <algorithm>
#include <iostream>
Expand Down
8 changes: 6 additions & 2 deletions tools/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ stub_debug.cc: $(top_srcdir)/src/tests/stub_debug.cc
Here.cc: $(top_srcdir)/src/base/Here.cc
cp $(top_srcdir)/src/base/Here.cc $@

CharacterSet.cc: $(top_srcdir)/src/base/CharacterSet.cc
cp $(top_srcdir)/src/base/CharacterSet.cc $@

MemBuf.cc: $(top_srcdir)/src/MemBuf.cc
cp $(top_srcdir)/src/MemBuf.cc $@

Expand All @@ -48,7 +51,7 @@ stub_cbdata.cc: $(top_srcdir)/src/tests/stub_cbdata.cc

stub_libmem.cc: $(top_srcdir)/src/tests/stub_libmem.cc STUB.h
cp $(top_srcdir)/src/tests/stub_libmem.cc $@

STUB.h: $(top_srcdir)/src/tests/STUB.h
cp $(top_srcdir)/src/tests/STUB.h $@

Expand All @@ -57,7 +60,7 @@ STUB.h: $(top_srcdir)/src/tests/STUB.h
# globals.cc is needed by test_tools.cc.
# Neither of these should be disted from here.
TESTSOURCES= test_tools.cc
CLEANFILES += test_tools.cc Here.cc MemBuf.cc stub_debug.cc time.cc stub_cbdata.cc stub_libmem.cc STUB.h
CLEANFILES += test_tools.cc Here.cc CharacterSet.cc MemBuf.cc stub_debug.cc time.cc stub_cbdata.cc stub_libmem.cc STUB.h

## Test Scripts
EXTRA_DIST += helper-ok-dying.pl helper-ok.pl
Expand All @@ -69,6 +72,7 @@ DEFAULT_CACHEMGR_CONFIG = $(sysconfdir)/cachemgr.conf
libexec_PROGRAMS = cachemgr$(CGIEXT)

cachemgr__CGIEXT__SOURCES = cachemgr.cc \
CharacterSet.cc \
Here.cc \
MemBuf.cc \
stub_cbdata.cc \
Expand Down
28 changes: 25 additions & 3 deletions tools/cachemgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "squid.h"
#include "base64.h"
#include "base/CharacterSet.h"
#include "getfullhostname.h"
#include "html_quote.h"
#include "ip/Address.h"
Expand Down Expand Up @@ -215,6 +216,21 @@ xstrtok(char **str, char del)
return "";
}

bool
hostname_check(const char *uri)
{
static CharacterSet hostChars = CharacterSet("host",".:[]_") +
CharacterSet::ALPHA + CharacterSet::DIGIT;

const auto limit = strlen(uri);
for (size_t i = 0; i < limit; i++) {
if (!hostChars[uri[i]]) {
return false;
}
}
return true;
}

static void
print_trailer(void)
{
Expand Down Expand Up @@ -807,9 +823,15 @@ process_request(cachemgr_request * req)
} else if ((S = req->hostname))
(void) 0;
else {
snprintf(buf, sizeof(buf), "Unknown host: %s\n", req->hostname);
error_html(buf);
return 1;
if (hostname_check(req->hostname)) {
snprintf(buf, sizeof(buf), "Unknown Host: %s\n", req->hostname);
error_html(buf);
return 1;
} else {
snprintf(buf, sizeof(buf), "%s\n", "Invalid Hostname");
error_html(buf);
return 1;
}
}

S.port(req->port);
Expand Down

0 comments on commit 5a90b4c

Please sign in to comment.