8
8
A basic Content Security Policy processor running in docker
9
9
10
10
## WIP Warning
11
+
11
12
This is still work in progress
12
13
13
14
## What does it do?
15
+
14
16
It logs to STDOUT (LOGLEVEL ` INFO ` ) and, optionally, to a GELF capable host, the received CSP violation.
15
17
16
18
The request must go to the path ` /csp ` (default) or to the path set in the environment variable ` CSP_PATH ` .
17
19
18
20
Just add the header:
19
- ```
21
+
22
+ ``` txt
20
23
Content-Security-Policy-Report-Only: upgrade-insecure-requests; default-src 'self'; report-uri https://example.com/csp;
21
24
```
22
25
23
26
### Invalid requests
27
+
24
28
The following requests are not logged at all, instead a warning is logged:
29
+
25
30
* Zero-length requests (a HTTP POST containing no payload)
26
31
* Requests larger than ` MAX_CONTENT_LENGTH `
27
32
28
33
All other requests log the underlying WSGI environment to log level ` DEBUG ` . Non-JSON requests are also logged to the same level.
29
34
30
35
## Healthcheck
36
+
31
37
To enable a healthcheck, just point it to ` /healthz ` (default) or to the value set for the environment variable ` HEALTHZ_PATH ` . You can use ` ENABLE_HEALTHZ_VERSION ` to also have CSP display the version and build information (disabled by default).
32
38
33
39
## Usage Examples
34
40
35
41
### CLI
42
+
36
43
``` sh
37
44
docker run --rm -it \
38
45
-p 9999:80 \
39
46
-e PORT=80 \
40
47
-e GELF_HOST=graylog \
41
48
--name csp \
42
- registry.gitlab.com /ix. ai/csp:latest
49
+ ghcr.io /ix- ai/csp:latest
43
50
```
44
51
45
52
### docker-compose
53
+
46
54
``` yml
47
55
version : " 3.7"
48
56
49
57
services :
50
58
csp :
51
- image : registry.gitlab.com /ix. ai/csp:latest
59
+ image : ghcr.io /ix- ai/csp:latest
52
60
environment :
53
61
PORT : ' 80'
54
62
MAX_CONTENT_LENGTH : ' 512'
@@ -57,12 +65,13 @@ services:
57
65
` ` `
58
66
59
67
### docker stack with traefik
68
+
60
69
` ` ` yml
61
70
version : " 3.7"
62
71
63
72
services :
64
73
csp :
65
- image : registry.gitlab.com /ix. ai/csp:latest
74
+ image : ghcr.io /ix- ai/csp:latest
66
75
deploy :
67
76
labels :
68
77
traefik.enable : ' true'
@@ -89,17 +98,21 @@ services:
89
98
```
90
99
91
100
## Output example
101
+
92
102
Firefox browser and ` LOGLEVEL: INFO `
93
- ```
103
+
104
+ ``` txt
94
105
2020-12-06 14:25:42.853 WARNING [__main__.<module>] Starting **csp refactor-225909200**. Listening on *:9180
95
106
2020-12-06 14:28:15.442 INFO [csp.log_csp] {"csp-report": {"blocked-uri": "inline", "document-uri": "https://xxxREDACTEDxxx/", "original-policy": "upgrade-insecure-requests; default-src 'self' https://cdnjs.cloudflare.com; script-src 'self' https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp", "referrer": "", "source-file": "https://xxxREDACTEDxxx/", "violated-directive": "default-src"}}
96
107
2020-12-06 14:28:15.711 INFO [csp.log_csp] {"csp-report": {"blocked-uri": "inline", "column-number": 1, "document-uri": "https://xxxREDACTEDxxx/", "line-number": 925, "original-policy": "upgrade-insecure-requests; default-src 'self' https://cdnjs.cloudflare.com; script-src 'self' https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp", "referrer": "", "source-file": "https://xxxREDACTEDxxx/", "violated-directive": "script-src"}}
97
108
2020-12-06 14:28:15.724 INFO [csp.log_csp] {"csp-report": {"blocked-uri": "inline", "column-number": 3975, "document-uri": "https://xxxREDACTEDxxx/", "line-number": 3, "original-policy": "upgrade-insecure-requests; default-src 'self' https://cdnjs.cloudflare.com; script-src 'self' https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp", "referrer": "", "source-file": "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js", "violated-directive": "default-src"}}
98
109
2020-12-06 14:28:15.735 INFO [csp.log_csp] {"csp-report": {"blocked-uri": "inline", "column-number": 3975, "document-uri": "https://xxxREDACTEDxxx/", "line-number": 3, "original-policy": "upgrade-insecure-requests; default-src 'self' https://cdnjs.cloudflare.com; script-src 'self' https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp", "referrer": "", "source-file": "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js", "violated-directive": "default-src"}}
99
110
2020-12-06 14:28:15.738 INFO [csp.log_csp] {"csp-report": {"blocked-uri": "inline", "column-number": 14648, "document-uri": "https://xxxREDACTEDxxx/", "line-number": 3, "original-policy": "upgrade-insecure-requests; default-src 'self' https://cdnjs.cloudflare.com; script-src 'self' https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp", "referrer": "", "source-file": "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js", "violated-directive": "default-src"}}
100
111
```
112
+
101
113
Google Chrome browser and ` LOGLEVEL: DEBUG `
102
- ```
114
+
115
+ ``` txt
103
116
2020-12-06 14:38:27.132 DEBUG [csp.log_csp] {'REMOTE_ADDR': '10.0.14.14', 'REMOTE_HOST': '10.0.14.14', 'REMOTE_PORT': '56224', 'REQUEST_METHOD': 'POST', 'SERVER_PORT': '9180', 'SERVER_NAME': '9f02bb970b0b', 'SERVER_SOFTWARE': None, 'SERVER_PROTOCOL': 'HTTP/1.1', 'SCRIPT_NAME': '', 'PATH_INFO': '/csp', 'QUERY_STRING': '', 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0), 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'wsgi.input': <_io.BytesIO object at 0x7fb398c89720>, 'wsgi.file_wrapper': <class 'waitress.buffers.ReadOnlyFileBasedBuffer'>, 'wsgi.input_terminated': True, 'HTTP_HOST': 'csp.example.com', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', 'CONTENT_LENGTH': '548', 'HTTP_ACCEPT': '*/*', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_ACCEPT_LANGUAGE': 'en-DE,en-GB;q=0.9,en;q=0.8,de-DE;q=0.7,de;q=0.6,ro-RO;q=0.5,ro;q=0.4,en-US;q=0.3', 'CONTENT_TYPE': 'application/csp-report', 'HTTP_DNT': '1', 'HTTP_ORIGIN': 'https://xxxREDACTEDxxx', 'HTTP_REFERER': 'https://xxxREDACTEDxxx/', 'HTTP_SEC_FETCH_DEST': 'report', 'HTTP_SEC_FETCH_MODE': 'no-cors', 'HTTP_SEC_FETCH_SITE': 'cross-site', 'HTTP_X_FORWARDED_FOR': '2001:0DB8::1', 'HTTP_X_FORWARDED_HOST': 'csp.example.com', 'HTTP_X_FORWARDED_PORT': '443', 'HTTP_X_FORWARDED_PROTO': 'https', 'HTTP_X_FORWARDED_SERVER': '2319d1b2d5bf', 'HTTP_X_REAL_IP': '2001:0DB8::1', 'werkzeug.request': <Request 'http://csp.example.com/csp' [POST]>}
104
117
2020-12-06 14:38:27.132 INFO [csp.log_csp] {"csp-report": {"document-uri": "https://xxxREDACTEDxxx/", "referrer": "", "violated-directive": "script-src-elem", "effective-directive": "script-src-elem", "original-policy": "upgrade-insecure-requests; default-src 'self' https://cdnjs.cloudflare.com; script-src 'self' https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp;", "disposition": "report", "blocked-uri": "inline", "line-number": 925, "source-file": "https://xxxREDACTEDxxx/", "status-code": 0, "script-sample": ""}}
105
118
2020-12-06 14:38:27.134 DEBUG [csp.log_csp] {'REMOTE_ADDR': '10.0.14.14', 'REMOTE_HOST': '10.0.14.14', 'REMOTE_PORT': '56220', 'REQUEST_METHOD': 'POST', 'SERVER_PORT': '9180', 'SERVER_NAME': '9f02bb970b0b', 'SERVER_SOFTWARE': None, 'SERVER_PROTOCOL': 'HTTP/1.1', 'SCRIPT_NAME': '', 'PATH_INFO': '/csp', 'QUERY_STRING': '', 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0), 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'wsgi.input': <_io.BytesIO object at 0x7fb398c89720>, 'wsgi.file_wrapper': <class 'waitress.buffers.ReadOnlyFileBasedBuffer'>, 'wsgi.input_terminated': True, 'HTTP_HOST': 'csp.example.com', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', 'CONTENT_LENGTH': '609', 'HTTP_ACCEPT': '*/*', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_ACCEPT_LANGUAGE': 'en-DE,en-GB;q=0.9,en;q=0.8,de-DE;q=0.7,de;q=0.6,ro-RO;q=0.5,ro;q=0.4,en-US;q=0.3', 'CONTENT_TYPE': 'application/csp-report', 'HTTP_DNT': '1', 'HTTP_ORIGIN': 'https://xxxREDACTEDxxx', 'HTTP_REFERER': 'https://xxxREDACTEDxxx/', 'HTTP_SEC_FETCH_DEST': 'report', 'HTTP_SEC_FETCH_MODE': 'no-cors', 'HTTP_SEC_FETCH_SITE': 'cross-site', 'HTTP_X_FORWARDED_FOR': '2001:0DB8::1', 'HTTP_X_FORWARDED_HOST': 'csp.example.com', 'HTTP_X_FORWARDED_PORT': '443', 'HTTP_X_FORWARDED_PROTO': 'https', 'HTTP_X_FORWARDED_SERVER': '2319d1b2d5bf', 'HTTP_X_REAL_IP': '2001:0DB8::1', 'werkzeug.request': <Request 'http://csp.example.com/csp' [POST]>}
@@ -111,7 +124,8 @@ Google Chrome browser and `LOGLEVEL: DEBUG`
111
124
```
112
125
113
126
Various errors (with ` LOGLEVEL:DEBUG ` ):
114
- ```
127
+
128
+ ``` txt
115
129
2020-12-06 14:28:15.448 WARNING [csp.log_csp] Content too large (523445). Dropping.
116
130
2020-12-06 14:52:47.747 WARNING [csp.log_csp] Empty content received
117
131
2020-12-06 14:54:07.615 DEBUG [csp.log_csp] {'REMOTE_ADDR': '10.0.14.16', 'REMOTE_HOST': '10.0.14.16', 'REMOTE_PORT': '32772', 'REQUEST_METHOD': 'POST', 'SERVER_PORT': '9180', 'SERVER_NAME': '45d8708af6ab', 'SERVER_SOFTWARE': None, 'SERVER_PROTOCOL': 'HTTP/1.1', 'SCRIPT_NAME': '', 'PATH_INFO': '/csp', 'QUERY_STRING': '', 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0), 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'wsgi.input': <_io.BytesIO object at 0x7f8cbd65c0e0>, 'wsgi.file_wrapper': <class 'waitress.buffers.ReadOnlyFileBasedBuffer'>, 'wsgi.input_terminated': True, 'HTTP_HOST': 'csp.example.com', 'HTTP_USER_AGENT': 'curl/7.64.1', 'CONTENT_LENGTH': '10', 'HTTP_ACCEPT': '*/*', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'HTTP_X_FORWARDED_FOR': '2001:0DB8::1', 'HTTP_X_FORWARDED_HOST': 'csp.example.com', 'HTTP_X_FORWARDED_PORT': '443', 'HTTP_X_FORWARDED_PROTO': 'https', 'HTTP_X_FORWARDED_SERVER': 'de9e6f88b502', 'HTTP_X_REAL_IP': '2001:0DB8::1', 'HTTP_ACCEPT_ENCODING': 'gzip', 'werkzeug.request': <Request 'http://csp.example.com/csp' [POST]>}
@@ -121,7 +135,8 @@ Various errors (with `LOGLEVEL:DEBUG`):
121
135
## Metrics
122
136
123
137
When setting ` ENABLE_METRICS=yes ` , the following metrics are exposed:
124
- ```
138
+
139
+ ``` txt
125
140
# HELP csp_valid_violation_reports_total Counts the number of valid violation reports
126
141
# TYPE csp_valid_violation_reports_total counter
127
142
csp_valid_violation_reports_total{blocked_uri="inline",document_uri="https://xxxREDACTEDxxx/",line_number="925",original_policy="upgrade-insecure-requests; default-src self https://cdnjs.cloudflare.com; script-src self https://cdnjs.cloudflare.com https://s.ytimg.com; font-src https://fonts.gstatic.com https://cdnjs.cloudflare.com; report-uri https://csp.example.com/csp;",violated_directive="script-src-elem"} 3.0
@@ -150,6 +165,7 @@ csp_version_info{version="0.2.0-225909200"} 1.0
150
165
| ** Variable** | ** Default** | ** Description** |
151
166
| :-------------------------| :-----------:| :-----------------------------------------------------------------------|
152
167
| ` MAX_CONTENT_LENGTH ` | ` 32768 ` | The maximum content length (in bytes) of the HTTP POST content |
168
+ | ` ENABLE_USER_AGENT ` | ` no ` | Enable the labels ` user_agent_platform ` , ` user_agent_browser ` and ` user_agent_version ` |
153
169
| ` ENABLE_HEALTHZ_VERSION ` | ` no ` | Set this to ` yes ` to show the version on the ` HEALTHZ_PATH ` endpoint |
154
170
| ` ENABLE_METRICS ` | ` no ` | Set this to ` yes ` to enable the Prometheus metrics |
155
171
| ` CSP_PATH ` | ` /csp ` | The path used for the CSP reporting |
@@ -162,29 +178,43 @@ csp_version_info{version="0.2.0-225909200"} 1.0
162
178
| ` ADDRESS ` | ` * ` | The IP address to bind to |
163
179
164
180
## Breaking Changes
181
+
165
182
Starting with version ` v0.1.0 ` , the log format has changed!
166
183
167
184
CSP will now parse and format any JSON received (smaller than ` MAX_CONTENT_LENGTH ` ) and log it in form:
168
- ```
185
+
186
+ ``` txt
169
187
2020-12-06 14:59:13.855 INFO [csp.log_csp] {"ab": 2}
170
188
```
171
189
172
190
Non-JSON content will be logged as follows:
173
- ```
191
+
192
+ ``` txt
174
193
2020-12-06 15:15:58.497 DEBUG [csp.log_csp] Content is not JSON: `{"ab": e2}`
175
194
```
176
195
196
+ ## Contributors
197
+
198
+ Thank you to the contributors:
199
+
200
+ * @bgi : ix.ai/csp!13
201
+
202
+ ## Deprecations
203
+
204
+ ** WARNING** : Due to the [ introduction of storage usage quotas by GitLab] ( https://docs.gitlab.com/ee/user/usage_quotas.html ) , the ` registry.gitlab.com ` images will ** not** be updated anymore and will soon be removed. Please switch to either Docker Hub or GitHub (see below).
205
+
177
206
## Tags and Arch
178
207
179
208
Starting with version ` v0.1.0 ` , the images are multi-arch, with builds for i386, amd64, arm64, armv7 and armv6.
209
+
180
210
* ` vN.N.N ` - for example v0.1.0
181
211
* ` latest ` - always pointing to the latest version
182
212
* ` dev-branch ` - the last build on a feature/development branch
183
213
* ` dev-master ` - the last build on the master branch
184
214
185
- ## Resources:
186
- * GitLab: https://gitlab.com/ix.ai/csp
187
- * GitHub: https://github .com/ix- ai/csp
188
- * GitLab Registry: https://gitlab .com/ix. ai/csp/container_registry
189
- * GitHub Registry: https://ghcr.io/ix-ai/csp
190
- * Docker Hub: https://hub.docker.com/r/ixdotai/csp
215
+ ## Resources
216
+
217
+ * GitLab: [ gitlab.com/ix.ai/csp ] ( https://gitlab .com/ix. ai/csp )
218
+ * GitHub: [ github.com/ix-ai/csp ] ( https://github .com/ix- ai/csp )
219
+ * GitHub Registry: ` ghcr.io/ix-ai/csp ` - [ ghcr.io/ix-ai/csp ] ( https://ghcr.io/ix-ai/csp )
220
+ * Docker Hub: ` ixdotai/csp ` - [ hub.docker.com/r/ixdotai/csp ] ( https://hub.docker.com/r/ixdotai/csp )
0 commit comments