Skip to content

Commit

Permalink
Add handling for CORS preflight checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjb committed Jan 16, 2024
1 parent 105ee0a commit ddb1c9b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
34 changes: 33 additions & 1 deletion canarytokens/channel_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ def render_GET(self, request: Request):
request.setHeader("Server", "Apache")
return resp

def render_OPTIONS(self, request):
def render_OPTIONS(self, request: Request):
"""
Alert as if it is a normal GET request, but return the expected content and headers.
"""
_ = self.render_GET(request)
_check_and_add_cors_preflight_headers(request)
request.setHeader("Allow", "OPTIONS, GET, POST")
request.setResponseCode(200)
request.responseHeaders.removeHeader("Content-Type")
Expand Down Expand Up @@ -206,6 +207,37 @@ def render_POST(self, request: Request):
return self.render_GET(request)


def _check_and_add_cors_preflight_headers(request):
"""
According to https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request, we
should check for `Access-Control-Request-Method` and `Origin` and optionally,
`Access-Control-Request-Headers` headers to determine its a preflight request; and
respond with `Access-Control-Allow-Origin` and `Access-Control-Allow-Methods`.
"""
if (
request.getHeader("Access-Control-Request-Method") is None
or request.getHeader("Origin") is None
):
return

acr_headers = request.getHeader("Access-Control-Request-Headers")
if acr_headers is not None:
request.setHeader("Access-Control-Allow-Headers", acr_headers)

request.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"))
request.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST")


# Request
# Access-Control-Request-Method: DELETE
# Access-Control-Request-Headers: origin, x-requested-with
# Origin: https://foo.bar.org

# Response
# Access-Control-Allow-Origin: https://foo.bar.org
# Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE


class ChannelHTTP:
def __init__(
self,
Expand Down
6 changes: 3 additions & 3 deletions frontend/frontend.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ CANARY_DOMAINS=127.0.0.1
CANARY_NXDOMAINS=nx.127.0.0.1
#CANARY_SWITCHBOARD_SETTINGS_PATH=

CANARY_SENTRY_DSN="https://06a5bffddffd4d0f8a9b675ea82a5fb5@o1177763.ingest.sentry.io/6312922"
CANARY_SENTRY_ENVIRONMENT=local
CANARY_SENTRY_ENABLE=False
#CANARY_SENTRY_DSN=
#CANARY_SENTRY_ENVIRONMENT=local
#CANARY_SENTRY_ENABLE=False

# template configurations
#CANARY_TEMPLATES_PATH=
Expand Down

0 comments on commit ddb1c9b

Please sign in to comment.