Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 94 additions & 2 deletions docs/guides/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,98 @@ it, where it ends the dispatch of a request that is not allowed.
:language: python
:caption: myapp.py

Security headers
~~~~~~~~~~~~~~~~

A browser protects the pages of an application from the pages of other sites
as far as the headers of their responses ask it to.
:mod:`Pyjoyment.Plugin.SecurityHeaders` adds them to every response, of the
actions, the static files and the pages of errors alike, with defaults that
restrict nothing that a page usually needs, so it can be enabled before any
of them is tuned.

.. code-block:: python

plugin('SecurityHeaders')

``Strict-Transport-Security`` makes the browser use HTTPS only for the site,
for as long as its ``max-age`` says, a year by default, so that an attacker on
the network cannot strip the encryption of the next visit. It is sent only
with the responses of HTTPS, also of a request that a `Reverse proxy`_ has
received with HTTPS, and never for ``localhost``. A site that starts to use it
starts with a few minutes, as a browser keeps it for that long and the site
cannot take it back, and adds ``include_subdomains`` and ``preload`` only once
every subdomain has HTTPS.

``Content-Security-Policy`` says where the scripts, the styles, the images and
the frames of a page may come from, so that a script that an attacker injects
into a page does not run. The default one only keeps other sites from framing
the pages and from changing their base URL, as a policy of the scripts depends
on the pages. A policy of its own is tried out first with ``csp_report_only``,
which the browser reports the violations of without enforcing it, with the
route of ``reports``, which logs them, and moves to ``csp`` once the reports
are only of what the pages should not do.

.. literalinclude:: cookbook/security_headers.py
:language: python
:caption: myapp.py

An inline script or style needs a nonce, a random value of the response that
the policy names and that the element has. The helpers ``javascript`` and
``stylesheet`` give it to their inline scripts and styles, as the helper
``csp_nonce`` gives it to a template, and the pages of errors of Pyjoyment have
it too. A response with a nonce gets ``Cache-Control: private``, as a CDN
would give the same nonce to every client, so a page that a CDN keeps loads
its scripts and styles from files instead, which the sources of the policy
allow.

The hook ``csp_report`` gets each violation, to store it or to send it to a
service of monitoring, as the route is public and its reports come from
anyone.

.. code-block:: python

@hook('csp_report')
def csp_report(c, report):
c.app.log.info(f'{report["effectiveDirective"]} blocked {report["blockedURL"]}')

``Permissions-Policy`` turns off the features of the browser that the pages do
not use, the camera, the geolocation and the microphone by default, also for
the frames of other sites in them, and ``X-Content-Type-Options``,
``Referrer-Policy`` and ``Cross-Origin-Opener-Policy`` keep the browser from
guessing the type of a response, from sending the whole URL of a page to
other sites, and from giving a page of another site a reference to the window
of the page.

Cross-origin resource sharing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A page of another site can request the application, but the browser gives it
the response only if the response says that its origin may read it, with the
headers of CORS. :mod:`Pyjoyment.Plugin.CORS` adds them for the origins of its
options, every origin by default, and answers the preflight request that a
browser sends before a request that a page could not send without CORS, as a
``PUT`` or a request of JSON, before the routes, so that it reaches no action.

.. code-block:: python

plugin('CORS')

A frontend on a site of its own, which reads an API with the cookies of the
session of its user, names its origins, as a browser gives a page the
response of a request with cookies only when it names the origin of the page,
never with ``*``. With
:mod:`Pyjoyment.Plugin.AllowedHosts`, which rejects the requests of the other
origins, the origins are in both.

.. literalinclude:: cookbook/cors.py
:language: python
:caption: myapp.py

CORS protects what a page of another site may read, not what it may request,
as a form of another site can still post to the application, which the CSRF
protection of forms is for.

.. _cookbook-deployment:

Deployment
Expand Down Expand Up @@ -2715,8 +2807,8 @@ JSONP

A page of another site reads the JSON of your application with CORS, which a
new API should use: the header ``Access-Control-Allow-Origin`` names the
origins that may read the response, and the recipe `Hosts and origins`_ says
how to check them.
origins that may read the response, which the recipe `Cross-origin resource
sharing`_ adds to every response, and an action to its own.

.. code-block:: python

Expand Down
15 changes: 15 additions & 0 deletions docs/guides/cookbook/cors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from Pyjoyment.Lite import *


# The pages of the frontend, also of its development server, read the API with the cookies of the session
ORIGINS = ['https://app.example.com', 'http://localhost:5173']
plugin('AllowedHosts', hosts=['api.example.com', 'localhost'], origins=ORIGINS)
plugin('CORS', origins=ORIGINS, credentials=True, paths=['/api'])


@get('/api/user')
def user(c):
c.render(json={'name': c.session('name')})


app.start()
39 changes: 39 additions & 0 deletions docs/guides/cookbook/security_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
__DATA__

@@ index.html.ep
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
%= stylesheet('/app.css')
%= javascript(begin
document.addEventListener('DOMContentLoaded', () => console.log('Welcome!'));
%= end)
</head>
<body><h1>Welcome</h1></body>
</html>
"""

from Pyjoyment.Lite import *


# A strict policy, tried out with the reports of the browsers while the default one is enforced, and a short HSTS
plugin(
'SecurityHeaders',
hsts={'max_age': 300},
csp_report_only={
'default-src': ["'self'"],
'img-src': ["'self'", 'data:'],
'frame-ancestors': ["'none'"],
},
reports='/csp-reports',
)


@get('/')
def index(c):
c.render('index')


app.start()
47 changes: 47 additions & 0 deletions t/docs/guides_cookbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,53 @@ def start_response(s, h, exc_info=None):
t.post_ok('/mcp', {'Host': 'evil.com', 'Origin': 'http://evil.com'}).status_is(421)
t.post_ok('/mcp', {'Origin': 'http://evil.com'}).status_is(403)

with subtest('Security headers'):
with cookbook('security_headers') as t:
t.get_ok('/').status_is(200).header_is('X-Content-Type-Options', 'nosniff').header_is(
'Cache-Control', 'private'
)
(
t.header_is(
'Content-Security-Policy',
"base-uri 'self'; frame-ancestors 'self'; object-src 'none'; report-uri /csp-reports; report-to csp",
)
.header_like(
'Content-Security-Policy-Report-Only',
r"^default-src 'self' 'nonce-([^']+)'; img-src 'self' data:; frame-ancestors 'none'; "
r'report-uri /csp-reports; report-to csp$',
)
.header_is('Reporting-Endpoints', 'csp="/csp-reports"')
.header_exists_not('Strict-Transport-Security')
)
nonce = re.search(r"'nonce-([^']+)'", t.tx.res.headers.content_security_policy_report_only)[1]
t.element_exists(f'script[nonce="{nonce}"]').element_exists('link[href="/app.css"]:not([nonce])')
report = {'csp-report': {'blocked-uri': 'inline', 'effective-directive': 'script-src-elem'}}
with t.app.log.capture('warn') as logs:
t.post_ok('/csp-reports', json=report).status_is(204)
like_ok(str(logs), r'CSP violation of script-src-elem: inline', 'report')
reports = []
t.app.hook('csp_report', lambda c, report: reports.append(report))
t.post_ok('/csp-reports', json=report).status_is(204)
is_deeply_ok(reports, [{'blockedURL': 'inline', 'effectiveDirective': 'script-src-elem'}], 'hook')

with subtest('Cross-origin resource sharing'):
with cookbook('cors') as t:
page = {'Host': 'api.example.com', 'Origin': 'https://app.example.com'}
(
t.get_ok('/api/user', page)
.status_is(200)
.header_is('Access-Control-Allow-Origin', 'https://app.example.com')
.header_is('Access-Control-Allow-Credentials', 'true')
.header_is('Vary', 'Origin')
)
preflight = {**page, 'Origin': 'http://localhost:5173', 'Access-Control-Request-Method': 'PUT'}
t.options_ok('/api/user', preflight).status_is(204).header_is(
'Access-Control-Allow-Origin', 'http://localhost:5173'
)
t.get_ok('/api/user', {**page, 'Origin': 'https://evil.com'}).status_is(403)
t.get_ok('/', {'Host': 'api.example.com', 'Origin': 'https://app.example.com'}).status_is(404)
t.header_exists_not('Access-Control-Allow-Origin')

with subtest('Pre-forking'):
with cookbook('worker') as t, t.app.log.capture('info') as logs:
t.get_ok('/').status_is(200).content_is('Hello World!')
Expand Down
Loading