Skip to content

Commit 6095e9e

Browse files
committed
Added SecurePolicies documentation
1 parent 1c676dc commit 6095e9e

File tree

1 file changed

+205
-9
lines changed

1 file changed

+205
-9
lines changed

Diff for: README.md

+205-9
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ Pragma: no-cache
100100

101101
You can toggle the setting of headers with default values by passing `True` or `False` and override default values by passing a string to the following options:
102102

103-
- `server` - set the Server header, e.g. `Server=“Secure”` *(string / bool, default=False)*
104-
- `hsts` - set the Strict-Transport-Security header *(string / bool, default=True)*
105-
- `xfo` - set the X-Frame-Options header *(string / bool, default=True)*
106-
- `xss` - set the X-XSS-Protection header *(string / bool, default=True)*
107-
- `content` - set the X-Content-Type-Options header *(string / bool, default=True)*
108-
- `csp` - set the Content-Security-Policy *(string / bool, default=False)* *
109-
- `referrer` - set the Referrer-Policy header *(string / bool, default=True)*
110-
- `cache` - set the Cache-control and Pragma headers *(string / bool, default=True)*
111-
- `feature` - set the Feature-Policy header *(string / bool, default=False)*
103+
- `server` - set the Server header, e.g. `Server=“Secure”` *(string / bool / SecurePolicies, default=False)*
104+
- `hsts` - set the Strict-Transport-Security header *(string / bool / SecurePolicies, default=True)*
105+
- `xfo` - set the X-Frame-Options header *(string / bool / SecurePolicies, default=True)*
106+
- `xxp` - set the X-XSS-Protection header *(string / bool / SecurePolicies, default=True)*
107+
- `content` - set the X-Content-Type-Options header *(string / bool / SecurePolicies, default=True)*
108+
- `csp` - set the Content-Security-Policy *(string / bool / SecurePolicies, default=False)* *
109+
- `referrer` - set the Referrer-Policy header *(string / bool / SecurePolicies, default=True)*
110+
- `cache` - set the Cache-control and Pragma headers *(string / bool / SecurePolicies, default=True)*
111+
- `feature` - set the Feature-Policy header *(SecurePolicies / string / bool / SecurePolicies, default=False)*
112112

113113
#### Example
114114

@@ -123,6 +123,202 @@ secure_headers.framework(response)
123123

124124
```
125125

126+
## SecurePolicies - Header Policy Builder
127+
128+
### Methods
129+
130+
#### CSP()
131+
**Directives:** `base_uri(sources)`, `block_all_mixed_content()`, `connect_src(sources)`, `default_src(sources)`, `font_src(sources)`, `form_action(sources)`, `frame_ancestors(sources)`, `frame_src(sources)`, `img_src(sources)`, `manifest_src(sources)`, `media_src(sources)`, `object_src(sources)`, `plugin_types(types)`, `report_to(json_object)`, `report_uri(uri)`, `require_sri_for(values)`, `sandbox(values)`, `script_src(sources)`, `style_src(sources)`, `upgrade_insecure_requests()`, `worker_src(sources)`
132+
133+
**Values()**: `self_`, `none`, `unsafe_inline`, `unsafe_eval`, `strict_dynamic`, `nonce(nonce_value)`, `all` = "*"
134+
135+
**Example:**
136+
137+
```Python
138+
csp_value = (
139+
SecurePolicies.CSP()
140+
.default_src(SecurePolicies.CSP().Values.none)
141+
.base_uri(SecurePolicies.CSP().Values.self_)
142+
.block_all_mixed_content()
143+
.connect_src(SecurePolicies.CSP().Values.self_, "api.spam.com")
144+
.frame_src(SecurePolicies.CSP().Values.none)
145+
.img_src(SecurePolicies.CSP().Values.self_, "static.spam.com")
146+
)
147+
148+
# default-src 'none'; base-uri 'self'; block-all-mixed-content; connect-src 'self' api.spam.com; frame-src 'none'; img-src 'self' static.spam.com
149+
```
150+
151+
*You can check the effectiveness of your CSP Policy at the [CSP Evaluator](https://csp-evaluator.withgoogle.com)*
152+
153+
154+
#### HSTS()
155+
**Directives:** `include_subDomains()`, `max_age(seconds)`, `preload()`
156+
157+
**Example:**
158+
159+
```Python
160+
hsts_value = (
161+
SecurePolicies.HSTS()
162+
.include_subdomains()
163+
.preload()
164+
.max_age(SecurePolicies.Seconds.one_month)
165+
)
166+
167+
# includeSubDomains; preload; max-age=2592000
168+
```
169+
170+
#### XXP()
171+
**Directives:** `disabled()` = 0, `enabled()` = 1, `enabled_block()` = 1; mode=block, `enabled_report(uri)` = 1; report=uri
172+
173+
**Example:**
174+
175+
```Python
176+
xxp_value = SecurePolicies.XXP().enabled_block()
177+
178+
# 1; mode=block
179+
```
180+
181+
182+
#### XFO()
183+
**Directives:** `allow_from(uri)`, `deny()`, `sameorigin()`
184+
185+
**Example:**
186+
187+
```Python
188+
xfo_value = SecurePolicies.XFO().deny()
189+
190+
# deny
191+
```
192+
193+
194+
#### Referrer()
195+
**Directives:** `no_referrer()`, `no_referrer_when_downgrade()`, `origin()`, `origin_when_cross_origin()`, `same_origin()`, `strict_origin()`, `strict_origin_when_cross_origin()`, `unsafe_url()`
196+
197+
**Example:**
198+
199+
```Python
200+
referrer_value = SecurePolicies.Referrer().no_referrer()
201+
202+
# no-referrer
203+
```
204+
205+
206+
#### Feature()
207+
**Directives:** `accelerometer(allowlist)`, `ambient_light_sensor(allowlist)`, `autoplay(allowlist)`, `camera(allowlist)`, `document_domain(allowlist)`, `encrypted_media(allowlist)`, `fullscreen(allowlist)`, `geolocation(allowlist)`, `gyroscope(allowlist)`, `magnetometer(allowlist)`, `microphone(allowlist)`, `midi(allowlist)`, `payment(allowlist)`, `picture_in_picture(allowlist)`, `speaker(allowlist)`, `sync_xhr(allowlist)`, `usb(allowlist)`, `Values(allowlist)`, `vr(allowlist)`
208+
209+
**Values()**: `self_`, `none`, `src`, `all` = "*"
210+
211+
**Example:**
212+
213+
```Python
214+
feature_value = (
215+
SecurePolicies.Feature()
216+
.geolocation(SecurePolicies.Feature.Values.self_, "spam.com")
217+
.vibrate(SecurePolicies.Feature.Values.none)
218+
)
219+
220+
# geolocation 'self' spam.com; vibrate 'none'
221+
```
222+
223+
224+
#### Cache
225+
**Directives:** `immutable()`, `max_age(seconds)`, `max_stale(seconds)`, `min_fresh(seconds)`, `must_revalidate()`, `no_cache()`, `no_store()`, `no_transform()`, `only_if_cached()`, `private()`, `proxy_revalidate()`, `public()`, `s_maxage(seconds)`, `stale_if_error(seconds)`, `stale_while_revalidate(seconds)`,
226+
227+
**Example:**
228+
229+
```Python
230+
cache_value = SecurePolicies.Cache().no_store().must_revalidate().proxy_revalidate()
231+
232+
# no-store, must-revalidate, proxy-revalidate
233+
```
234+
235+
236+
### Seconds
237+
**Values:** `five_minutes` = "300", `one_week` = "604800", `one_month` = "2592000", `one_year` = "31536000", `two_years` = "63072000"
238+
239+
## Usage
240+
##### Example
241+
```Python
242+
from sanic import Sanic
243+
from secure import SecureHeaders, SecurePolicies
244+
245+
csp_value = (
246+
SecurePolicies.CSP()
247+
.default_src(SecurePolicies.CSP().Values.none)
248+
.base_uri(SecurePolicies.CSP().Values.self_)
249+
.block_all_mixed_content()
250+
.connect_src(SecurePolicies.CSP().Values.self_, "api.spam.com")
251+
.frame_src(SecurePolicies.CSP().Values.none)
252+
.img_src(SecurePolicies.CSP().Values.self_, "static.spam.com")
253+
)
254+
255+
hsts_value = (
256+
SecurePolicies.HSTS()
257+
.include_subdomains()
258+
.preload()
259+
.max_age(SecurePolicies.Seconds.one_month)
260+
)
261+
262+
xxp_value = SecurePolicies.XXP().enabled_block()
263+
264+
xfo_value = SecurePolicies.XFO().deny()
265+
266+
referrer_value = SecurePolicies.Referrer().no_referrer()
267+
268+
feature_value = (
269+
SecurePolicies.Feature()
270+
.geolocation(SecurePolicies.Feature.Values.self_, "spam.com")
271+
.vibrate(SecurePolicies.Feature.Values.none)
272+
)
273+
274+
cache_value = SecurePolicies.Cache().no_store().must_revalidate().proxy_revalidate()
275+
276+
secure_headers = SecureHeaders(
277+
csp=csp_value,
278+
hsts=hsts_value,
279+
xfo=xfo_value,
280+
xxp=xxp_value,
281+
referrer=referrer_value,
282+
feature=feature_value,
283+
cache=cache_value,
284+
)
285+
secure_cookie = SecureCookie()
286+
287+
app = Sanic()
288+
289+
. . .
290+
291+
@app.middleware("response")
292+
async def set_secure_headers(request, response):
293+
secure_headers.sanic(response)
294+
295+
. . .
296+
```
297+
298+
Response Headers:
299+
300+
```HTTP
301+
Strict-Transport-Security: includeSubDomains; preload; max-age=2592000
302+
X-Frame-Options: deny
303+
X-XSS-Protection: 1; mode=block
304+
X-Content-Type-Options: nosniff
305+
Content-Security-Policy: default-src 'none'; base-uri 'self'; block-all-mixed-content; connect-src 'self' api.spam.com; frame-src 'none'; img-src 'self' static.spam.com
306+
Referrer-Policy: no-referrer
307+
Cache-control: no-store, must-revalidate, proxy-revalidate
308+
Feature-Policy: geolocation 'self' spam.com; vibrate 'none'
309+
```
310+
311+
312+
## Resources:
313+
* **CSP:** [CSP Cheat Sheet | Scott Helme](https://scotthelme.co.uk/csp-cheat-sheet/), [Content-Security-Policy | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy), [Content Security Policy Cheat Sheet | OWASP](https://www.owasp.org/index.php/Content_Security_Policy_Cheat_Sheet), [Content Security Policy CSP Reference & Examples](https://content-security-policy.com)
314+
* **XXP:** [X-XSS-Protection | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection)
315+
* **XFO:** [X-Frame-Options | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options)
316+
* **HSTS:** [Strict-Transport-Security | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security),
317+
[HTTP Strict Transport Security Cheat Sheet | OWASP](https://www.owasp.org/index.php/HTTP_Strict_Transport_Security_Cheat_Sheet)
318+
* **Referrer:** [A new security header: Referrer Policy | Scott Helme](https://scotthelme.co.uk/a-new-security-header-referrer-policy/), [Referrer-Policy | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy)
319+
* **Feature:** [A new security header: Feature Policy | Scott Helme](https://scotthelme.co.uk/a-new-security-header-feature-policy/), [Feature-Policy | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Feature-Policy), [Introduction to Feature Policy | Google Developers](https://developers.google.com/web/updates/2018/06/feature-policy)
320+
* **Cache:** [Cache-Control | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)
321+
126322
## Cookies
127323
#### Path
128324
The Path directive instructs the browser to only send the cookie if provided path exists in the URL.

0 commit comments

Comments
 (0)