Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for #1575 to allow OPTIONS method to work with x-api-key #1576

Merged
merged 3 commits into from
Dec 22, 2021
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
10 changes: 9 additions & 1 deletion aries_cloudagent/admin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,15 @@ async def check_token(request: web.Request, handler):
header_admin_api_key = request.headers.get("x-api-key")
valid_key = const_compare(self.admin_api_key, header_admin_api_key)

if valid_key or is_unprotected_path(request.path):
# We have to allow OPTIONS method access to paths without a key since
# browsers performing CORS requests will never include the original
# x-api-key header from the method that triggered the preflight
# OPTIONS check.
if (
valid_key
or is_unprotected_path(request.path)
or (request.method == "OPTIONS")
):
return await handler(request)
else:
raise web.HTTPUnauthorized()
Expand Down
25 changes: 25 additions & 0 deletions aries_cloudagent/admin/tests/test_admin_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,31 @@ async def test_visit_secure_mode(self):
) as response:
assert response.status == 200

# Make sure that OPTIONS requests used by browsers for CORS
# are allowed without a x-api-key even when x-api-key security is enabled
async with self.client_session.options(
f"http://127.0.0.1:{self.port}/status",
headers={
"Access-Control-Request-Headers": "x-api-key",
"Access-Control-Request-Method": "GET",
"Connection": "keep-alive",
"Host": f"http://127.0.0.1:{self.port}/status",
"Origin": "http://localhost:3000",
"Referer": "http://localhost:3000/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
},
) as response:
assert response.status == 200
assert response.headers["Access-Control-Allow-Credentials"] == "true"
assert response.headers["Access-Control-Allow-Headers"] == "X-API-KEY"
assert response.headers["Access-Control-Allow-Methods"] == "GET"
assert (
response.headers["Access-Control-Allow-Origin"]
== "http://localhost:3000"
)

async with self.client_session.ws_connect(
f"http://127.0.0.1:{self.port}/ws", headers={"x-api-key": "test-api-key"}
) as ws:
Expand Down