Skip to content

Commit

Permalink
--cors Access-Control-Allow-Headers: Authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 14, 2021
1 parent 0fdbf00 commit 8584993
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from .utils import (
PrefixedUrlString,
StartupError,
add_cors_headers,
async_call_with_supported_arguments,
await_me_maybe,
call_with_supported_arguments,
Expand Down Expand Up @@ -1321,7 +1322,7 @@ async def handle_500(self, request, send, exception):
)
headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(headers)
if request.path.split("?")[0].endswith(".json"):
await asgi_send_json(send, info, status=status, headers=headers)
else:
Expand Down
5 changes: 5 additions & 0 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,3 +1089,8 @@ async def derive_named_parameters(db, sql):
return [row["p4"].lstrip(":") for row in results if row["opcode"] == "Variable"]
except sqlite3.DatabaseError:
return possible_params


def add_cors_headers(headers):
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Headers"] = "Authorization"
9 changes: 5 additions & 4 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datasette import __version__
from datasette.database import QueryInterrupted
from datasette.utils import (
add_cors_headers,
await_me_maybe,
EscapeHtmlWriter,
InvalidSql,
Expand Down Expand Up @@ -163,7 +164,7 @@ class DataView(BaseView):
async def options(self, request, *args, **kwargs):
r = Response.text("ok")
if self.ds.cors:
r.headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(r.headers)
return r

def redirect(self, request, path, forward_querystring=True, remove_args=None):
Expand All @@ -174,7 +175,7 @@ def redirect(self, request, path, forward_querystring=True, remove_args=None):
r = Response.redirect(path)
r.headers["Link"] = f"<{path}>; rel=preload"
if self.ds.cors:
r.headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(r.headers)
return r

async def data(self, request, database, hash, **kwargs):
Expand Down Expand Up @@ -417,7 +418,7 @@ async def stream_fn(r):

headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(headers)
if request.args.get("_dl", None):
if not trace:
content_type = "text/csv; charset=utf-8"
Expand Down Expand Up @@ -643,5 +644,5 @@ def set_response_headers(self, response, ttl):
response.headers["Cache-Control"] = ttl_header
response.headers["Referrer-Policy"] = "no-referrer"
if self.ds.cors:
response.headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(response.headers)
return response
3 changes: 2 additions & 1 deletion datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import markupsafe

from datasette.utils import (
add_cors_headers,
await_me_maybe,
check_visibility,
derive_named_parameters,
Expand Down Expand Up @@ -176,7 +177,7 @@ async def view_get(self, request, database, hash, correct_hash_present, **kwargs
filepath = db.path
headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(headers)
headers["Transfer-Encoding"] = "chunked"
return AsgiFileDownload(
filepath,
Expand Down
4 changes: 2 additions & 2 deletions datasette/views/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hashlib
import json

from datasette.utils import check_visibility, CustomJSONEncoder
from datasette.utils import add_cors_headers, check_visibility, CustomJSONEncoder
from datasette.utils.asgi import Response
from datasette.version import __version__

Expand Down Expand Up @@ -129,7 +129,7 @@ async def get(self, request, as_format):
if as_format:
headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(headers)
return Response(
json.dumps({db["name"]: db for db in databases}, cls=CustomJSONEncoder),
content_type="application/json; charset=utf-8",
Expand Down
4 changes: 2 additions & 2 deletions datasette/views/special.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from datasette.utils.asgi import Response, Forbidden
from datasette.utils import actor_matches_allow
from datasette.utils import actor_matches_allow, add_cors_headers
from .base import BaseView
import secrets

Expand All @@ -23,7 +23,7 @@ async def get(self, request, as_format):
if as_format:
headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
add_cors_headers(headers)
return Response(
json.dumps(data),
content_type="application/json; charset=utf-8",
Expand Down
3 changes: 2 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,8 @@ def test_trace(trace_debug):
def test_cors(app_client_with_cors, path, status_code):
response = app_client_with_cors.get(path)
assert response.status == status_code
assert "*" == response.headers["Access-Control-Allow-Origin"]
assert response.headers["Access-Control-Allow-Origin"] == "*"
assert response.headers["Access-Control-Allow-Headers"] == "Authorization"


@pytest.mark.parametrize(
Expand Down

0 comments on commit 8584993

Please sign in to comment.