Skip to content

Commit

Permalink
CSRF protection, refs #793
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 5, 2020
1 parent 0c064c5 commit e994b21
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import asgi_csrf
import collections
import datetime
import hashlib
Expand Down Expand Up @@ -884,7 +885,14 @@ async def setup_db():
await database.table_counts(limit=60 * 60 * 1000)

asgi = AsgiLifespan(
AsgiTracer(DatasetteRouter(self, routes)), on_startup=setup_db
AsgiTracer(
asgi_csrf.asgi_csrf(
DatasetteRouter(self, routes),
signing_secret=self._secret,
cookie_name="ds_csrftoken",
)
),
on_startup=setup_db,
)
for wrapper in pm.hook.asgi_wrapper(datasette=self):
asgi = wrapper(asgi)
Expand Down
3 changes: 2 additions & 1 deletion datasette/templates/messages_debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>Debug messages</h1>

<p>Set a message:</p>

<form action="/-/messages" method="POST">
<form action="/-/messages" method="post">
<div>
<input type="text" name="message" style="width: 40%">
<div class="select-wrapper">
Expand All @@ -19,6 +19,7 @@ <h1>Debug messages</h1>
<option>all</option>
</select>
</div>
<input type="hidden" name="csrftoken" value="{{ csrftoken }}">
<input type="submit" value="Add message">
</div>
</form>
Expand Down
1 change: 1 addition & 0 deletions datasette/templates/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ <h3>Query parameters</h3>
{% endif %}
<p>
<button id="sql-format" type="button" hidden>Format SQL</button>
{% if canned_query %}<input type="hidden" name="csrftoken" value="{{ csrftoken }}">{% endif %}
<input type="submit" value="Run SQL">
</p>
</form>
Expand Down
1 change: 1 addition & 0 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async def render(self, templates, request, context=None):
**context,
**{
"database_url": self.database_url,
"csrftoken": request.scope["csrftoken"],
"database_color": self.database_color,
"show_messages": lambda: self.ds._show_messages(request),
"select_templates": [
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def get_version():
"uvicorn~=0.11",
"aiofiles>=0.4,<0.6",
"janus>=0.4,<0.6",
"asgi-csrf>=0.3.1",
"PyYAML~=5.3",
"mergedeep>=1.1.1,<1.4.0",
"itsdangerous~=1.1",
Expand Down

0 comments on commit e994b21

Please sign in to comment.