Skip to content

Commit

Permalink
Make additional_headers propery of the embedded webserver CivetWeb ed…
Browse files Browse the repository at this point in the history
…itable by users via the new config option webserver.headers

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Feb 21, 2025
1 parent 03cc1d3 commit d9fd060
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
3 changes: 3 additions & 0 deletions src/api/docs/content/specs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ components:
type: string
threads:
type: integer
headers:
type: string
session:
type: object
properties:
Expand Down Expand Up @@ -744,6 +746,7 @@ components:
acl: "+0.0.0.0/0,::/0"
port: 80,[::]:80
threads: 0
headers: "Content-Security-Policy: default-src 'self' 'unsafe-inline';\r\nX-Frame-Options: DENY\r\nX-XSS-Protection: 0\r\nX-Content-Type-Options: nosniff\r\nReferrer-Policy: strict-origin-when-cross-origin"
session:
timeout: 300
restore: true
Expand Down
6 changes: 6 additions & 0 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,12 @@ static void initConfig(struct config *conf)
conf->webserver.threads.d.ui = 0;
conf->webserver.threads.c = validate_stub; // Only type-based checking

conf->webserver.headers.k = "webserver.headers";
conf->webserver.headers.h = "Additional HTTP headers added to the web server responses.\n The headers are separated by a carriage return and a line feed (\\r\\n). The headers are added to all responses, including those for the API.\n Note about the default additional headers:\n - Content-Security-Policy: [...] 'unsafe-inline' is both required by Chart.js styling some elements directly, and index.html containing some inlined Javascript code.\n - X-Frame-Options: DENY: The page can not be displayed in a frame, regardless of the site attempting to do so.\n - X-Xss-Protection: 0: Disables XSS filtering in browsers that support it. This header is usually enabled by default in browsers, and is not recommended as it can hurt the security of the site. (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection).\n - X-Content-Type-Options: nosniff: Marker used by the server to indicate that the MIME types advertised in the Content-Type headers should not be changed and be followed. This allows to opt-out of MIME type sniffing, or, in other words, it is a way to say that the webmasters knew what they were doing. Site security testers usually expect this header to be set.\n - Referrer-Policy: strict-origin-when-cross-origin: A referrer will be sent for same-site origins, but cross-origin requests will send no referrer information.\n The latter four headers are set as expected by https://securityheaders.io";
conf->webserver.headers.t = CONF_STRING;
conf->webserver.headers.d.s = (char*)"Content-Security-Policy: default-src 'self' 'unsafe-inline';\r\nX-Frame-Options: DENY\r\nX-XSS-Protection: 0\r\nX-Content-Type-Options: nosniff\r\nReferrer-Policy: strict-origin-when-cross-origin";
conf->webserver.headers.c = validate_stub; // Only type-based checking

conf->webserver.tls.cert.k = "webserver.tls.cert";
conf->webserver.tls.cert.h = "Path to the TLS (SSL) certificate file. All directories along the path must be readable and accessible by the user running FTL (typically 'pihole'). This option is only required when at least one of webserver.port is TLS. The file must be in PEM format, and it must have both, private key and certificate (the *.pem file created must contain a 'CERTIFICATE' section as well as a 'RSA PRIVATE KEY' section).\n The *.pem file can be created using\n cp server.crt server.pem\n cat server.key >> server.pem\n if you have these files instead";
conf->webserver.tls.cert.a = cJSON_CreateStringReference("<valid TLS certificate file (*.pem)>");
Expand Down
1 change: 1 addition & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ struct config {
struct conf_item acl;
struct conf_item port;
struct conf_item threads;
struct conf_item headers;
struct {
struct conf_item timeout;
struct conf_item restore;
Expand Down
27 changes: 1 addition & 26 deletions src/webserver/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,27 +424,6 @@ void http_init(void)
}

// Prepare options for HTTP server (NULL-terminated list)
// Note about the additional headers:
// - "Content-Security-Policy: [...]"
// 'unsafe-inline' is both required by Chart.js styling some elements directly, and
// index.html containing some inlined Javascript code.
// - "X-Frame-Options: DENY"
// The page can not be displayed in a frame, regardless of the site attempting to do
// so.
// - "X-Xss-Protection: 0"
// Disables XSS filtering in browsers that support it. This header is usually
// enabled by default in browsers, and is not recommended as it can hurt the
// security of the site. (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection)
// - "X-Content-Type-Options: nosniff"
// Marker used by the server to indicate that the MIME types advertised in the
// Content-Type headers should not be changed and be followed. This allows to
// opt-out of MIME type sniffing, or, in other words, it is a way to say that the
// webmasters knew what they were doing. Site security testers usually expect this
// header to be set.
// - "Referrer-Policy: strict-origin-when-cross-origin"
// A referrer will be sent for same-site origins, but cross-origin requests will
// send no referrer information.
// The latter four headers are set as expected by https://securityheaders.io
const char *options[] = {
"document_root", config.webserver.paths.webroot.v.s,
"error_pages", error_pages,
Expand All @@ -453,11 +432,7 @@ void http_init(void)
"enable_directory_listing", "no",
"num_threads", num_threads,
"authentication_domain", config.webserver.domain.v.s,
"additional_header", "Content-Security-Policy: default-src 'self' 'unsafe-inline';\r\n"
"X-Frame-Options: DENY\r\n"
"X-XSS-Protection: 0\r\n"
"X-Content-Type-Options: nosniff\r\n"
"Referrer-Policy: strict-origin-when-cross-origin",
"additional_header", config.webserver.headers.v.s,
"index_files", "index.html,index.htm,index.lp",
NULL, NULL,
NULL, NULL, // Leave slots for access control list (ACL) and TLS configuration at the end
Expand Down
29 changes: 26 additions & 3 deletions test/pihole.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Pi-hole configuration file (v5.25.2-2556-g3cf56990-dirty)
# Pi-hole configuration file (v6.0.1-3-g03cc1d34-dirty)
# Encoding: UTF-8
# This file is managed by pihole-FTL
# Last updated on 2025-01-25 17:33:51 UTC
# Last updated on 2025-02-21 10:13:14 UTC

[dns]
# Array of upstream DNS servers used by Pi-hole
Expand Down Expand Up @@ -672,6 +672,29 @@
# threads are only created when needed due to incoming connections.
threads = 0

# Additional HTTP headers added to the web server responses.
# The headers are separated by a carriage return and a line feed (\r\n). The headers
# are added to all responses, including those for the API.
# Note about the default additional headers:
# - Content-Security-Policy: [...] 'unsafe-inline' is both required by Chart.js
# styling some elements directly, and index.html containing some inlined Javascript
# code.
# - X-Frame-Options: DENY: The page can not be displayed in a frame, regardless of the
# site attempting to do so.
# - X-Xss-Protection: 0: Disables XSS filtering in browsers that support it. This
# header is usually enabled by default in browsers, and is not recommended as it can
# hurt the security of the site.
# (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection).
# - X-Content-Type-Options: nosniff: Marker used by the server to indicate that the
# MIME types advertised in the Content-Type headers should not be changed and be
# followed. This allows to opt-out of MIME type sniffing, or, in other words, it is a
# way to say that the webmasters knew what they were doing. Site security testers
# usually expect this header to be set.
# - Referrer-Policy: strict-origin-when-cross-origin: A referrer will be sent for
# same-site origins, but cross-origin requests will send no referrer information.
# The latter four headers are set as expected by https://securityheaders.io
headers = "Content-Security-Policy: default-src 'self' 'unsafe-inline';\r\nX-Frame-Options: DENY\r\nX-XSS-Protection: 0\r\nX-Content-Type-Options: nosniff\r\nReferrer-Policy: strict-origin-when-cross-origin"

[webserver.session]
# Session timeout in seconds. If a session is inactive for more than this time, it will
# be terminated. Sessions are continuously refreshed by the web interface, preventing
Expand Down Expand Up @@ -1140,7 +1163,7 @@
all = true ### CHANGED, default = false

# Configuration statistics:
# 152 total entries out of which 95 entries are default
# 153 total entries out of which 96 entries are default
# --> 57 entries are modified
# 2 entries are forced through environment:
# - misc.nice
Expand Down

0 comments on commit d9fd060

Please sign in to comment.