Skip to content

Commit

Permalink
Proxyto: Allow CORS on commercial on public docs pages (#10762)
Browse files Browse the repository at this point in the history
Allow CORS on commercial

After testing this more thoroughly,
allowing cross-origin requests on commercial should be safe
for documentation pages.

Using `*` doesn't allow credentials, and we are also not
allowing cross-origin requests for private versions.
Basically we have a double protection :D.
  • Loading branch information
stsewd authored Sep 21, 2023
1 parent 20af793 commit ae4791c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
6 changes: 0 additions & 6 deletions readthedocs/proxito/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,6 @@ def add_cors_headers(self, request, response):
See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin.
"""

# Disable CORS on "Read the Docs for Business" for now.
# We want to be pretty sure this logic is OK before enabling it there.
if settings.ALLOW_PRIVATE_REPOS:
return

# TODO: se should add these headers to files from docs only,
# proxied APIs and other endpoints should not have CORS headers.
# These attributes aren't currently set for proxied APIs, but we shuold
Expand Down
31 changes: 31 additions & 0 deletions readthedocs/proxito/tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,37 @@ def test_cors_headers_public_version(self):
self.assertNotIn(ACCESS_CONTROL_ALLOW_CREDENTIALS, r.headers)
self.assertEqual(r[ACCESS_CONTROL_ALLOW_METHODS], "HEAD, OPTIONS, GET")

@override_settings(ALLOW_PRIVATE_REPOS=True, RTD_ALLOW_ORGANIZATIONS=True)
def test_cors_headers_public_version_with_organizations(self):
get(Organization, owners=[self.eric], projects=[self.project])

self.client.force_login(self.eric)

# Normal request
r = self.client.get(
"/en/latest/",
secure=True,
headers={"host": "project.dev.readthedocs.io"},
)
self.assertEqual(r.status_code, 200)
self.assertEqual(r[ACCESS_CONTROL_ALLOW_ORIGIN], "*")
self.assertNotIn(ACCESS_CONTROL_ALLOW_CREDENTIALS, r.headers)
self.assertEqual(r[ACCESS_CONTROL_ALLOW_METHODS], "HEAD, OPTIONS, GET")

# Cross-origin request
r = self.client.get(
"/en/latest/",
secure=True,
headers={
"host": "project.dev.readthedocs.io",
"origin": "https://example.com",
},
)
self.assertEqual(r.status_code, 200)
self.assertEqual(r[ACCESS_CONTROL_ALLOW_ORIGIN], "*")
self.assertNotIn(ACCESS_CONTROL_ALLOW_CREDENTIALS, r.headers)
self.assertEqual(r[ACCESS_CONTROL_ALLOW_METHODS], "HEAD, OPTIONS, GET")

@override_settings(ALLOW_PRIVATE_REPOS=False)
def test_cache_headers_public_version_with_private_projects_not_allowed(self):
r = self.client.get(
Expand Down

0 comments on commit ae4791c

Please sign in to comment.