Skip to content

Commit

Permalink
Add fix for openwallet-foundation#1575 to allow OPTIONS method to wor…
Browse files Browse the repository at this point in the history
…k when x-api-key is configured

Signed-off-by: john court <[email protected]>
  • Loading branch information
jcourt562 committed Dec 22, 2021
1 parent cd5588f commit 0254c9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion aries_cloudagent/admin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ 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
20 changes: 20 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,26 @@ 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

0 comments on commit 0254c9d

Please sign in to comment.