Use HTML meta-refresh for external OAuth redirects behind IIS ARR

ARR's machine-level reverseRewriteHostInResponseHeaders rewrote the host of
Location headers, sending Facebook/Google login dialogs to loma.chat/v21.0/...
Serve a 200 HTML hop to the provider URL instead of a 3xx Location.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Loma Bot Ops
2026-11-23 16:44:26 +07:00
parent fd282c8ce4
commit dc6ee09a55
3 changed files with 20 additions and 2 deletions
+16
View File
@@ -29,6 +29,22 @@ export function htmlRedirect(path: string): Response {
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" } });
}
/**
* Sends the browser to an absolute external URL (Facebook / Google dialogs) with
* a 200 HTML meta-refresh instead of a 3xx `Location`. Behind an IIS ARR reverse
* proxy (loma.chat/pagiva) the machine-level `reverseRewriteHostInResponseHeaders`
* rewrites the host of any `Location` header to the public host, which turned
* https://www.facebook.com/v21.0/dialog/oauth into https://loma.chat/v21.0/...
* A response body is never rewritten. Returns a NextResponse so callers can set cookies.
*/
export function externalRedirect(url: string): NextResponse {
const target = new URL(url);
if (target.protocol !== "https:") throw new Error("externalRedirect requires an https URL");
const safe = target.toString().replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
const html = `<!doctype html><meta charset="utf-8"><meta http-equiv="refresh" content="0;url=${safe}"><title>กำลังไปยังหน้าเข้าสู่ระบบ…</title><p style="font-family:system-ui;padding:24px">กำลังไปยังหน้าเข้าสู่ระบบ… <a href="${safe}">ไปต่อ</a></p>`;
return new NextResponse(html, { headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" } });
}
/** Cron routes authenticate with `Authorization: Bearer ${CRON_SECRET}` (Vercel Cron sends it). */
export function cronAuthorized(req: Request): boolean {
const secret = process.env.CRON_SECRET;