fix: better cors handling#443
Merged
Merged
Conversation
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The bundled browser demo at
src/scripts/app_demo/was failing withFailed to fetch/ERR_CERT_AUTHORITY_INVALID. Two underlying issues:api.openai.com, etc.) don't return browser-friendly CORS headers, so any page calling them viafetchgets blocked by the browser. The transparent proxy was forwarding preflights upstream and copying upstream headers verbatim — neither produced anything usable for a browser.CertManagerwas still generating certs with CN/OYaak Proxy CA(pre-rebrand leftover), which (a) didn't match what docs and the settings modal told users to look for, and (b) caused stale entries in the system keychain to be orphaned across upgrades.Also rolled up some smaller polish from the same session.
Changes
Backend — CORS on intercepted responses (
src/backend/proxy/)cors.gowith shared helpers:setCORSHeaders,writeCORSPreflight(forhttp.ResponseWriter),writeCORSPreflightOverTLS(for raw MITMnet.Conn), anddrainAndClose.transparent.go: in bothinterceptHTTP(plain) andinterceptHTTPOverTLS(MITM-decrypted TLS):OPTIONSpreflights → respond204 No Content+ CORS headers, do not forward upstream.Origin(or*), echoesAccess-Control-Request-Headersfrom the preflight, allows common methods, exposes all response headers, 1h max-age.Backend — CA rename (
src/backend/proxy/certmanager.go)Yaak Proxy CA→Kiji Privacy Proxy CA.Yaak Proxy→Kiji Privacy Proxy.Yaakreferences remain insrc/backend.Frontend — CA cert setup modal (
src/frontend/src/components/modals/CACertSetupModal.tsx)~/Library/Application Support/Kiji Privacy Proxy/certs/ca.crtcontains spaces, so the shell was splitting it across foursecurityarguments. Now uses~/"Library/Application Support/..."so~still expands while the rest is quoted.Copybutton that writes the command (with real newlines, not<br>) to the clipboard vianavigator.clipboard.writeTextand flips to a greenCheck + "Copied"badge for 2s.<code>with<br>s to a<pre><code>driven by a single string constant, so what's rendered and what's copied are guaranteed to match.Docs (
docs/05-advanced-topics.md)### Browser-Based Clients (CORS)subsection under## Transparent Proxy & MITM— explains the preflight short-circuit, response-header injection, the full table of headers added, where the policy lives, prerequisites for browser callers, and theapp_demoas an end-to-end verifier.#### Migrating from "Yaak Proxy CA"under### Removing Trust— bash to delete the legacy cert from System + login keychains and the on-diskca.crt/ca.keyso the rebuilt backend regenerates under the new name.Migration for existing dev installs
After pulling this branch you'll need to rotate the CA (one time):
Test plan
go vet ./src/backend/proxy/...cleanCGO_LDFLAGS="-L./build/tokenizers" go test ./src/backend/proxy/...passesmake build-gosucceedstsc --noEmitclean for the modaleslintclean for the modalmake go-backend-dev, install the freshly-minted CA, startbash src/scripts/app_demo/run_demo.sh, paste an OpenAI key in the demo athttp://localhost:8888, confirm a 200 response and PII masking in the backend logs