-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CORS requests when Origin is invalid or null
In some situations (such as calling posthog from within an iframe) the Origin header can be "null" or otherwise invalid In these situations we can't echo back the requested origin so we need to send * instead
- Loading branch information
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from collections import namedtuple | ||
from django.test import TestCase | ||
|
||
from posthog.utils_cors import cors_response | ||
|
||
|
||
class TestCorsResponse(TestCase): | ||
def test_origin(self) -> None: | ||
valid_origin_test_cases = [ | ||
("https://my-amazing.site", "https://my-amazing.site"), | ||
("https://my-amazing.site/", "https://my-amazing.site"), | ||
("https://my-amazing.site/my/path", "https://my-amazing.site"), | ||
("http://my-amazing.site/my/path", "http://my-amazing.site"), | ||
("https://us.posthog.com/decide", "https://us.posthog.com"), | ||
("my-amazing.site", "*"), | ||
("my-amazing.site/path", "*"), | ||
("null", "*"), | ||
("", "*"), | ||
] | ||
|
||
FakeRequest = namedtuple("Request", "META") | ||
for origin, expected in valid_origin_test_cases: | ||
with self.subTest(): | ||
request = FakeRequest(META={"HTTP_ORIGIN": origin}) | ||
self.assertEqual( | ||
expected, | ||
cors_response(request, {})["Access-Control-Allow-Origin"], | ||
msg=f"with origin='{origin}', actual did not equal {expected}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters