Skip to content

fix: derive the api host, not the console host - #7

Merged
AmishaBisht merged 2 commits into
glific:mainfrom
AmishaBisht:fix/derive-api-host
Sep 7, 2026
Merged

fix: derive the api host, not the console host#7
AmishaBisht merged 2 commits into
glific:mainfrom
AmishaBisht:fix/derive-api-host

Conversation

@AmishaBisht

Copy link
Copy Markdown
Contributor

Follow-up to #3. The widget cannot reach any backend once deployed — this fixes that.

The bug

deriveApiBase strips the web. prefix and stops there, so it targets the staff console, not the API:

web.staging.glific.com  ->  https://staging.glific.com/api      ← the console
                            https://api.staging.glific.com/api  ← the API

Verified against staging:

Host Result
staging.glific.com/api/v1/web_channel/branding 200<!doctype html>
api.staging.glific.com/api/v1/web_channel/branding 200{"data":{"theme":"rose",...}}

The console is a static site: it answers any path with index.html and a 200, and static hosts send no CORS headers. So the browser reports

No 'Access-Control-Allow-Origin' header is present on the requested resource.

which reads as a CORS misconfiguration when the request is simply going to the wrong server. That misdirection is the main cost of this bug — it sends you looking at CORSPlug and check_origin instead of at the hostname.

The fix

-  hostname.startsWith('web.') ? `https://${hostname.slice('web.'.length)}/api` : '/api';
+  hostname.startsWith('web.') ? `https://api.${hostname.slice('web.'.length)}/api` : '/api';

No backend change needed. api.staging.glific.com already answers with access-control-allow-origin: * and a 204 preflight — I checked both before assuming.

Why it shipped

The tech design §4.1 says to map to <shortcode>.glific.com. The README (https://api.<org>.glific.com/api) and SubdomainPlug stripping an api. prefix both say otherwise, and they are right. I followed the doc.

The test failed to catch it because it asserted the design doc's mapping rather than a reachable host — so it passed while the widget could not talk to staging at all. The added test asserts the base contains //api. and is explicitly not the console host, which is a property the doc cannot make wrong.

Worth correcting §4.1 separately so the next person does not repeat this.

Verification

tsc -b · oxlint · 48 unit tests · 36 e2e across 4 devices — all green.

The e2e suite stubs /branding with page.route, so it never exercises real hostname derivation; that gap is why this needed a unit test rather than a journey.

Not in scope

allow-origin: * alongside allow-credentials: true is an invalid pairing per the CORS spec. It works today because axios does not send credentials — but if the socket or auth work (#5662 / #5663) sets withCredentials, browsers will reject * and CORS will need configuring properly.

`web.<shortcode>.glific.com` mapped to `https://<shortcode>.glific.com/api`, which is the staff
console — a static site that answers any path with index.html and no CORS headers. The browser
reports that as "No 'Access-Control-Allow-Origin' header is present", so it reads as a CORS
misconfiguration when the request is simply reaching the wrong server.

Confirmed on staging:

    staging.glific.com/api/v1/web_channel/branding      -> 200, index.html
    api.staging.glific.com/api/v1/web_channel/branding  -> 200, the branding payload

So the widget could not reach any backend once deployed. It now maps to
`https://api.<shortcode>.glific.com/api`. No backend change is needed — that host already answers
with `access-control-allow-origin: *` and a 204 preflight.

The tech design says to map to `<shortcode>.glific.com`; the README and SubdomainPlug's stripping
of an `api.` prefix both say otherwise, and they are right. The original test encoded the design
doc's mapping rather than a reachable host, which is why it passed while the widget was broken —
the added test asserts the base contains `//api.` and is not the console host.
@vercel

vercel Bot commented Sep 7, 2026

Copy link
Copy Markdown

@AmishaBisht is attempting to deploy a commit to the Glific Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Team

Run ID: f7e2c928-31e8-4335-bdee-87fe5b12a77d


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

The first real run of this workflow failed in 19s:

    yarn test
    vite:plugin:mkcert:WARN  The mkcert does not exist, download it now
    Error: Request failed [GET] .../mkcert-v1.4.4-linux-amd64 (504 Gateway Time-out)

Vitest resolves the Vite config with `command === "serve"`, same as the dev server, so the
mkcert plugin activated during `yarn test` and tried to download a binary from GitHub. The 504
is incidental — a CI job should not be installing a certificate authority even when the
download succeeds.

Three things resolve this config and none of them wants mkcert: `vite build`, `vite preview`
(Playwright serves the production build over plain HTTP) and `vitest`. The gate covered the
first two; it now covers vitest, with `CI` excluded outright as a backstop.

Verified by running the whole workflow locally with `CI=true` and no `certs/` directory:
typecheck, lint, 48 unit tests, build and 36 e2e all pass with no mkcert activity.

Only reachable now that the workflow actually runs — glific#3 merged it, and this is its first
execution on a PR.
@AmishaBisht
AmishaBisht merged commit 12e6231 into glific:main Sep 7, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant