Skip to content

Answer CORS preflight OPTIONS on the enquiry endpoint - #562

Merged
tomhamiltonstubber merged 3 commits into
masterfrom
handle-cors-preflight-options
Sep 1, 2026
Merged

tomhamiltonstubber merged 3 commits into
masterfrom
handle-cors-preflight-options

Conversation

@tejas7777

Copy link
Copy Markdown
Contributor

Technical description of changes

The tutorcruncher.com contact form cannot submit: it posts to /{company}/enquiry with Content-Type: application/json, so the browser first sends a CORS preflight OPTIONS request. json_request_middleware treats every non-GET/HEAD method as a JSON request, tries to parse the empty OPTIONS body, and returns 400 ("Value Error: Expecting value: line 1 column 1 (char 0)"), so the preflight fails and the browser never sends the POST. Reproducible with curl -X OPTIONS https://socket.tutorcruncher.com/<key>/enquiry.

Changes:

  • json_request_middleware skips body parsing for OPTIONS.
  • The enquiry view (registered with add_route('*', ...)) answers OPTIONS with 204 and Access-Control-Allow-Origin/Methods/Headers so the preflight succeeds.
  • Test for the preflight response.

The embedded widget (socket-frontend) is unaffected either way — its XHR never sets Content-Type, so its posts are simple requests with no preflight.

tejas7777 and others added 2 commits September 1, 2026 14:22
Browsers send a preflight OPTIONS request before any cross-origin POST
with a Content-Type of application/json. json_request_middleware tried
to JSON-parse the empty OPTIONS body and returned a 400, so the
preflight failed and the browser never sent the enquiry POST — the
tutorcruncher.com contact form has been unable to submit because of
this. Skip body parsing for OPTIONS and have the enquiry view answer
it with 204 plus the Access-Control-Allow-* headers.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@tejas7777 tejas7777 added the bug label Sep 1, 2026
@tejas7777 tejas7777 self-assigned this Sep 1, 2026

- name: set up python
uses: actions/setup-python@v1
uses: actions/setup-python@v5

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated test runner

…tributes

The test set extra_attributes='default', which makes the
tell-us-about-yourself attribute required, but posted no attributes, so
the enquiry was rejected with a 400 instead of the asserted 201. It
merged unnoticed because CI's setup-python step was already broken and
tests never ran. The test only cares that a long grecaptcha token
passes validation, so drop the extra_attributes line.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread tests/test_enquiries.py


async def test_post_enquiry_long_grecaptcha_response(cli, company, other_server, worker):
other_server.app['extra_attributes'] = 'default'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Says: this line made the options define a required attribute (tell-us-about-yourself) that the posted data never included, so the test could only ever get a 400 — it merged green because CI's setup-python step was broken and tests never ran. The test only checks that a long grecaptcha token passes validation, so the attribute setup is dropped.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

@tomhamiltonstubber
tomhamiltonstubber merged commit 20b780a into master Sep 1, 2026
2 checks passed
@tomhamiltonstubber
tomhamiltonstubber deleted the handle-cors-preflight-options branch September 1, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants