Browser security
browser แยกของแต่ละ origin ไว้ไม่ให้ปนกัน CORS คือวิธีที่ server ยอมให้ origin อื่นอ่านคำตอบของมัน ส่วน CSP คือวิธีที่หน้าเว็บจำกัดตัวเองว่าจะโหลดหรือส่งอะไรได้ ดู request ถูกส่ง ถูกกั้น และถูกปล่อยผ่าน แล้วลองหาว่า CORS แบบไหนทำข้อมูลคุณหลุด
สิ่งที่จะได้
- ดูออกว่าสอง URL เป็น origin เดียวกันไหม: scheme, host และ port ต้องตรงกันหมด
- อธิบายได้ว่าทำไมขึ้น CORS error แล้ว request อาจถึง server และทำงานไปแล้วก็ได้
- ตอบ CORS ด้วย allowlist ได้ ไม่สะท้อน Origin ที่ส่งมาทุกตัวกลับไปพร้อม credentials
- เขียน CSP ด้วย script-src, connect-src กับ frame-ancestors ได้ โดยลอง Report-Only ก่อน
สรุป 1 นาที
กดข้อไหน ภาพข้างล่างจะเล่นขั้นนั้นให้ดู
(no Access-Control-* headers)ยังไม่มี request
ลอง evil.example กับแต่ละแบบของ API ดูว่าแบบไหนทำให้คนร้ายอ่านข้อมูลคุณได้ แล้วสลับไป CSP ลองทั้งสี่ท่าตอนปิดและเปิด
หน้า https://shop.example เรียก fetch("https://api.shop.example/me") แบบ GET แล้ว API ไม่ตอบ header CORS เลย เกิดอะไรขึ้น
ในภาพ: browser ของคุณคือลานด้านหน้าของเมือง ล้อมด้วยเส้นประ ในลานมีแท็บสองแท็บเป็นบ้าน คือหน้าของร้านกับหน้าบน evil.example (สีแดง) ทุก request ออกจากลานทางถนนเดียว ผ่านด่านของ browser เอง ซึ่งเป็นเสากับไม้กั้น ถนนด้านหลังมี server ของ shop.example กับ API ของมัน ส่วน server ของ evil.example เป็นตึกสีแดงบนถนนของตัวเอง แต่ละขั้นคือ request หนึ่งครั้งหรือการตรวจหนึ่งครั้ง ไฟวิ่งออกไป (มีไฟสีทองวิ่งตามเมื่อส่ง cookie ไปด้วย) แล้วหยุดที่ด่าน หรือกลับมาถึงหน้าเว็บ ป้ายกลางภาพบอก origin ที่เทียบกัน header CORS ที่ server ตอบ หรือ CSP ที่ใช้อยู่ เป็นแบบจำลองตามมาตรฐาน Fetch กับ CSP ไม่ใช่ browser จริง
เทียบทุกแบบ: CORS
ทุกช่องเจอห้า request เดิม สามตัวแรกมาจาก shop.example อีกสองตัวมาจาก evil.example ที่คุณเผลอเปิดไว้ ✓ คือหน้าเว็บอ่านได้, ✗ คือไปถึง server แต่ browser ไม่ให้อ่าน, – คือ preflight ไม่ผ่านเลยไม่ได้ส่ง, !! คือคนร้ายอ่านข้อมูลของคุณได้
ใช้เมื่อ API ที่ไม่ได้มีไว้ให้หน้าเว็บจาก origin อื่นเรียก: ค่าเริ่มต้นที่ปลอดภัยสุด
ไม่ควรใช้เมื่อ หน้าเว็บของคุณเองอยู่คนละ origin กับ API (เช่น shop กับ api.shop) จะอ่านอะไรไม่ได้เลย
แบบจำลองตามมาตรฐาน Fetch ไม่ได้วัดจาก browser จริง สมมติว่า cookie ของ API เป็น SameSite=None (แบบที่ API ข้าม site ต้องใช้) ถ้าเป็น Lax browser จะไม่ส่งมันจาก evil.example ตั้งแต่แรก · กดช่องไหนก็ได้ ภาพใหญ่ด้านบนจะเปลี่ยนเป็นแบบนั้น พร้อมบอกว่าเหมาะกับงานแบบไหน
สามกฎ คนคุมคนเดียว
| กฎ | ใครตั้ง | ทำอะไร |
|---|---|---|
| same-origin policy | browser ตั้งไว้เสมอ | หน้าเว็บอ่านคำตอบ frame หรือ storage ของ origin อื่นไม่ได้ |
| CORS | server ที่ถือข้อมูล | ยอมให้ origin ที่ระบุอ่านคำตอบได้ |
| CSP | หน้าเว็บเอง | จำกัดว่าหน้าจะโหลด รัน ส่งอะไร และใครฝังมันได้ |
CORS ไม่ใช่ firewall
request ข้าม origin แบบธรรมดาถูกส่งไปจริง และ server ก็ทำงานไปแล้ว browser แค่ซ่อนคำตอบจากหน้าเว็บ อะไรที่เปลี่ยนข้อมูลยังต้องมีด่านของตัวเอง: cookie SameSite, CSRF token หรือตรวจ header Origin ที่ server
ใช้ allowlist อย่าใช้กระจกสะท้อน
ตอบ Access-Control-Allow-Origin เป็น origin ที่รู้จักตรง ๆ ถ้าก๊อป Origin ที่ส่งมาคืนไปพร้อม Allow-Credentials เว็บไหนก็อ่านข้อมูลผู้ใช้คุณได้ ใส่ Vary: Origin ด้วย cache จะได้ไม่เอาคำตอบของ origin หนึ่งไปให้อีก origin
CSP คือด่านที่สอง
script-src ที่เข้มกัน script ที่ถูกฉีดไม่ให้รัน connect-src กันข้อมูลไหลออก แต่รูที่ทำให้ script เข้ามาได้ก็ยังเป็น bug อยู่ escape output ก่อน แล้วให้ CSP คอยรับที่หลุดมา เริ่มจาก Content-Security-Policy-Report-Only เพื่อดูก่อนว่ามันจะบล็อกอะไร
ในโค้ดจริง
// An API on another origin with CORS done right: an allowlist (never "echo any Origin"), credentials only for it,
// Vary: Origin, a preflight answer, and the page's own CSP. A real node:http server on 127.0.0.1; the requests below
// carry the Origin header a browser would add. Node doesn't enforce CORS (a browser does), so this checks what the
// server answers: the headers the browser then goes by.
// Run: node cors.ts (Node 23.6+; on Node 22: node --experimental-strip-types cors.ts)
import assert from "node:assert/strict";
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
const ALLOWED = new Set(["https://shop.example"]); // exact origins: scheme + host + port
const CSP = "default-src 'self'; script-src 'self'; connect-src 'self' https://api.shop.example; frame-ancestors 'none'";
const server = createServer((req, res) => {
const origin = req.headers.origin;
res.setHeader("Vary", "Origin"); // the answer depends on Origin: caches must keep them apart
if (origin && ALLOWED.has(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
}
if (req.method === "OPTIONS") { // the preflight
if (origin && ALLOWED.has(origin)) {
res.setHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
res.setHeader("Access-Control-Max-Age", "600");
}
res.writeHead(204);
return res.end();
}
if (req.url === "/") { res.writeHead(200, { "content-type": "text/html", "content-security-policy": CSP }); return res.end("<!doctype html><title>shop</title>"); }
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ user: "mai" }));
});
await new Promise<void>((ok) => server.listen(0, "127.0.0.1", ok));
const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
const ask = (origin: string, method = "GET", extra: Record<string, string> = {}) => fetch(`${base}/me`, { method, headers: { origin, ...extra } });
const shop = await ask("https://shop.example");
const evil = await ask("https://evil.example");
const pre = await ask("https://shop.example", "OPTIONS", { "access-control-request-method": "PUT", "access-control-request-headers": "content-type" });
const preEvil = await ask("https://evil.example", "OPTIONS", { "access-control-request-method": "PUT" });
const page = await fetch(base + "/");
server.close();
assert.equal(shop.headers.get("access-control-allow-origin"), "https://shop.example");
assert.equal(shop.headers.get("access-control-allow-credentials"), "true");
assert.equal(evil.status, 200); // it was sent and answered...
assert.equal(evil.headers.get("access-control-allow-origin"), null); // ...but a browser won't let evil.example read it
assert.equal(pre.headers.get("access-control-allow-methods"), "GET, PUT, DELETE");
assert.equal(preEvil.headers.get("access-control-allow-methods"), null);
assert.equal(shop.headers.get("vary"), "Origin");
assert.match(page.headers.get("content-security-policy") ?? "", /frame-ancestors 'none'/);
console.log("shop.example: Allow-Origin shop.example + credentials · evil.example: 200 but no Allow-Origin (unreadable in a browser) · preflight PUT: allowed for shop, not for evil · Vary: Origin · CSP on the page");
node cors.ts (Node 23.6+ · Node 22 ใส่ --experimental-strip-types)ผลที่พิมพ์ออกมา: shop.example: Allow-Origin shop.example + credentials · evil.example: 200 but no Allow-Origin (unreadable in a browser) · preflight PUT: allowed for shop, not for evil · Vary: Origin · CSP on the pageAPI node:http ตัวจริงบน 127.0.0.1: allowlist ของ origin, credentials เฉพาะ origin ในรายการ, Vary: Origin, ตอบ preflight และ CSP ของหน้าเว็บ Node ไม่ได้บังคับ CORS (browser เป็นคนทำ) ไฟล์นี้เลยตรวจสิ่งที่ server ตอบ ซึ่งก็คือ header ที่ browser ใช้ตัดสิน
คำถามที่เจอบ่อย
ขึ้น CORS error แปลว่า request ไม่ได้ส่งไปใช่ไหม
ไม่เสมอไป GET ธรรมดาถูกส่งไปและ server ทำงานแล้วด้วย browser แค่ซ่อนคำตอบจากหน้าเว็บ ที่ไม่ได้ส่งจริงคือ request ตัวจริงหลัง preflight ไม่ผ่าน เช่น PUT หรือ body ที่เป็น JSON
CORS กัน CSRF ได้ไหม
ไม่ได้ CORS แค่ตัดสินว่าใครอ่านคำตอบได้ ฟอร์มหรือ request ธรรมดาจากเว็บอื่นยังไปถึง server และทำงานได้อยู่ อะไรที่เปลี่ยนข้อมูลต้องมีด่านของตัวเอง: cookie SameSite, CSRF token หรือเช็ก header Origin ที่ server
preflight คืออะไร เกิดตอนไหน
คือ request OPTIONS ที่ browser ส่งไปถามก่อน สำหรับ request ข้าม origin ที่ไม่ธรรมดา เช่น PUT, DELETE, body เป็น JSON หรือมี header ของตัวเอง server ต้องตอบ Allow-Origin, Allow-Methods และ Allow-Headers ให้ตรง request ตัวจริงถึงจะตามไป
ตอบ Access-Control-Allow-Origin: * ไปเลยได้ไหม
ได้กับข้อมูลสาธารณะที่ใครอ่านก็ได้ เช่นราคาสินค้า แต่ใช้กับ request ที่มี cookie ไม่ได้ browser ไม่ยอมใช้ * คู่กับ credentials ถ้าต้องใช้ cookie ต้องตอบ origin ที่ชัดเจนพร้อม Allow-Credentials: true
ตอบ Allow-Origin ตาม Origin ที่ส่งมาทุกครั้ง ผิดตรงไหน
browser เชื่อที่ server บอก ถ้าสะท้อนทุก origin พร้อม credentials เว็บไหนก็ได้ที่ผู้ใช้เปิดอยู่จะอ่านข้อมูลส่วนตัวของเขาด้วย cookie ได้ (ถ้า cookie เป็น SameSite=None) ใช้ allowlist ของ origin ที่รู้จัก แล้วใส่ Vary: Origin ด้วย
CORS กับ CSP ต่างกันยังไง
CORS คือ server ที่ถือข้อมูลบอก browser ว่าหน้าจาก origin ไหนอ่านคำตอบได้ เป็นการคลายกฎ same-origin ส่วน CSP คือหน้าเว็บบอก browser ว่าตัวเองยอมโหลดหรือส่งอะไรไปที่ไหน เป็นการรัดตัวเอง ทั้งคู่ browser เป็นคนบังคับ
CSP แต่ละข้อกันอะไร
script-src บอกว่า script มาจากไหนได้ ไม่ใส่ 'unsafe-inline' ก็ปิด script ที่โดนฉีด connect-src คุมว่า fetch กับ WebSocket ไปที่ไหนได้ กันข้อมูลรั่วออก frame-ancestors คุมว่าใครฝังหน้าเราใน iframe ได้ กัน clickjacking
จะเริ่มใส่ CSP ยังไงไม่ให้เว็บพัง
เริ่มจาก Content-Security-Policy-Report-Only ก่อน browser จะบอกว่าจะบล็อกอะไร แต่ยังไม่บล็อกจริง ดูรายงานไปสักพัก แก้ของที่ติด แล้วค่อยเปลี่ยนเป็น header ตัวจริง
บทต่อไป
Cookie กับ sessionระดับ: กลาง
same-origin กับ CORS ตัดสินว่าหน้าเว็บอ่านอะไรได้ บทต่อไปดู flag ของ cookie ที่ตัดสินว่า login ของคุณจะติดไปกับ request ไหน