ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Authentication က မေးခွန်းတစ်ခုကို ဖြေပါတယ် — ခင်ဗျားက ဘယ်သူလဲ။ Authorization ကတော့ နောက်တစ်ခုကို ဖြေပါတယ် — ခင်ဗျားက ဘာတွေ လုပ်ခွင့်ရှိလဲ။ ဟိုတယ်ရဲ့ key card က ကောင်းတဲ့ ပုံတစ်ပုံပါ။ ကောင်တာက card ပေးတဲ့အခါ ခင်ဗျားက ဘယ်သူလဲ စစ်ပြီးပါပြီ (authentication)။ အဲဒီ card က ကိုယ့်အခန်း၊ ကာယဗလကျွန်းမာရေးခန်းနဲ့ lift ကို ဖွင့်ပေမယ့် တခြားဧည့်သည်တွေရဲ့ အခန်းကို မဖွင့်ပါဘူး (authorization)။ API ကလည်း အတူတူပါပဲ၊ ကွာတာက card ဟာ request တိုင်းမှာ တွဲပို့တဲ့ အက္ခရာစီတန်းတစ်ခု ဖြစ်တာပါ။ အဲဒီစီတန်းကို ကိုင်ထားသူကို ကိုယ်တိုင်လို့ သဘောထားတာမို့ password လို အကာအကွယ်ပေးဖို့ လိုပါတယ်။
Problem → Why → Solution
ပြဿနာ: code ထဲ ထည့်လိုက်တဲ့ key က လူတိုင်းဆီ ရောက်တယ်
Team တစ်ခုက ငွေပေးရတဲ့ weather API ကို ခေါ်တဲ့ mobile app ကို ထုတ်ပါတယ်။ မြန်မြန်အလုပ်ဖြစ်ဖို့ developer က API key ကို app ရဲ့ JavaScript ထဲ ကူးထည့်ပြီး file ကို commit လုပ်လိုက်ပါတယ်။ တစ်ပတ်ကြာတော့ ငွေတောင်းခံလွှာက မျှော်လင့်ထားတာထက် ဆယ်ဆ များနေပါတယ်။ Network tab ကို ဘယ်သူမဆို ဖွင့်ပြီး key ကို ကူးယူကာ ကိုယ့် project တွေအတွက် account ကို သုံးနိုင်ပါတယ်။ Key က git history ထဲမှာလည်း ရှိနေလို့ အဲဒီစာကြောင်းကို ဖျက်ပြီးတောင် repository ရထားသူတိုင်းမှာ key ရှိနေဆဲပါ။
ဒုတိယပြဿနာက မရှင်းလင်းတဲ့ error တွေပါ။ Credential မပါဘဲ ခေါ်တာ၊ key မှားပြီး ခေါ်တာ၊ key မှန်ပေမယ့် ခွင့်ပြုချက် မရှိတာ — သုံးမျိုးလုံး ကျရှုံးပေမယ့် အကြောင်းရင်းသုံးခုနဲ့ status code နှစ်ခု မတူပါဘူး။ ရောထွေးသွားရင် debug လုပ်ရတာ နှေးပါတယ်။
// BAD: a secret baked into code that ships to users and lives in git
const API_KEY = "key_live_EXAMPLE_DO_NOT_USE";
// api.example.com is a placeholder host used for illustration only
const res = await fetch("https://api.example.com/v1/forecast?city=Yangon", {
headers: { Authorization: "Bearer " + API_KEY },
});ဘာကြောင့် ဒီလို အန္တရာယ်ရှိတာလဲ
Browser ဒါမှမဟုတ် ဖုန်းဆီ ရောက်တဲ့ code ကို ဘယ်သူမဆို ဖတ်လို့ရပြီး git က commit လုပ်ဖူးတဲ့ စာကြောင်းတိုင်းကို မှတ်ထားပါတယ်။ Credential တစ်ခုရဲ့ လုံခြုံမှုက ၎င်းကို ရေးခဲ့ဖူးတဲ့ အများသိဆုံးနေရာလောက်ပဲ ရှိပါတယ်။ ဒါကြောင့် credential တွေကို run တဲ့အချိန်မှာ environment ကနေ ယူရမယ်၊ ကိုယ်ထိန်းချုပ်တဲ့ server ပေါ်မှာ ထားရမယ်၊ ပေါက်ကြားရင်လည်း ပျက်စီးမှု ကန့်သတ်ရှိအောင် scope ကန့်ထားရမယ်။
Core concept: ဘယ်သူလဲ၊ ပြီးမှ ဘာလုပ်ခွင့်ရှိလဲ
Authentication (authn) က identity ကို သက်သေပြတာပါ — ဒီ request က application X ကနေ ဒါမှမဟုတ် user Y ကနေ လာတာ။ Authorization (authz) က အဲဒီ identity ဘာလုပ်ခွင့်ရှိလဲ ဆုံးဖြတ်တာပါ — report တွေ ဖတ်လို့ရလား၊ ဖျက်လို့ရလား။ API တွေက အမှားနှစ်မျိုးကို မတူအောင် ပြောပါတယ်။ 401 Unauthorized က နာမည်နဲ့ မတူဘဲ authentication မအောင်မြင်ခြင်း ဒါမှမဟုတ် မပါခြင်းကို ဆိုလိုပါတယ် — credential မပါ၊ token သက်တမ်းကုန်၊ key မှား။ 401 နဲ့ အတူ server မျှော်လင့်တဲ့ scheme ကို ဖော်ပြတဲ့ WWW-Authenticate header ပါသင့်ပါတယ်။ 403 Forbidden ကတော့ server က ကိုယ်ဘယ်သူလဲ သိပါပြီ၊ ဒါပေမယ့် ငြင်းတာပါ — credential မှန်ပေမယ့် ခွင့်ပြုချက် မရှိတာပါ။ API အချို့က resource ရှိတယ်ဆိုတာကို ဖုံးဖို့ 403 အစား 404 ကို ပြန်ပါတယ်။
Credential အမျိုးအစား လေးမျိုး ရှိပါတယ်။ API key ဆိုတာ ခေါ်တဲ့ application ဒါမှမဟုတ် project ကို ဖော်ထုတ်တဲ့ ကျပန်း စာလုံးရှည်ပါ။ ရိုးရှင်းပြီး ကိုယ်တိုင် သက်တမ်း မကုန်တတ်ဘဲ server-to-server ခေါ်ဆိုမှုတွေနဲ့ ကိုက်ပါတယ်။ Bearer token ဆိုတာ Authorization: Bearer <token> (RFC 6750) အဖြစ် ပို့တဲ့ token မှန်သမျှပါ၊ ကိုင်ထား (bear) သူကို လက်ခံပါတယ်။ JWT (JSON Web Token) က လူကြိုက်များတဲ့ token format တစ်ခုဖြစ်ပြီး user id နဲ့ သက်တမ်းကုန်ချိန်လို လက်မှတ်ထိုးထားတဲ့ claim တွေကို သယ်ပြီး ပုံမှန်အားဖြင့် သက်တမ်းတိုပါတယ်။ OAuth 2.0 ဆိုတာ user က သူ့ password မပေးဘဲ application တစ်ခုကို သူ့ကိုယ်စား ကန့်သတ်ထားတဲ့ ခွင့်ပြုချက်နဲ့ လုပ်ခွင့်ပေးတဲ့ framework (RFC 6749) ပါ၊ ရလဒ်က scope ပါတဲ့ access token ဖြစ်ပြီး refresh token ပါတတ်ပါတယ်။ Session cookie ကတော့ login ပြီးရင် server က session သိမ်းပြီး browser ကို cookie ပေးတာပါ၊ browser က အလိုအလျောက် ပို့တာမို့ website တွေနဲ့ ကိုက်ပေမယ့် CSRF ကို ကာကွယ်ဖို့ လိုပါတယ်။
ဘယ်အမျိုးအစားမဆို credential က request မှာ တွဲထားတဲ့ စာသားပဲ ဖြစ်ပြီး ခပ်များများက Authorization header (session အတွက်ဆိုရင် Cookie) ထဲ ပါပါတယ်။ API အချို့က API key ကို X-API-Key လို custom header မှာ မျှော်လင့်ပြီး အချို့က query string မှာ လက်ခံပါတယ်၊ ဒါကိုတော့ URL တွေကို log မှတ်တတ်လို့ ရှောင်ပါ။ HTTPS ကို အမြဲသုံးပါ — plain HTTP မှာ လမ်းကြောင်းပေါ်က ဘယ်သူမဆို header ကို ဖတ်နိုင်ပါတယ်။
Secret ကို client-side code (browser JavaScript၊ mobile app)၊ git၊ log၊ URL၊ chat message ဒါမှမဟုတ် screenshot ထဲ ဘယ်တော့မှ မပေါ်သင့်တဲ့ အရာအဖြစ် သဘောထားပါ။ Development အချိန်မှာ secret တွေကို .gitignore ထဲ ထည့်ထားတဲ့ .env file ထဲ ထားပြီး နာမည်ပဲ ပါတဲ့ (တန်ဖိုး မပါတဲ့) .env.example ကို commit လုပ်ပါ။ အဲဒါတွေကို environment variable ထဲ load လုပ်ပြီး process.env.NAME နဲ့ ဖတ်ပါ၊ production မှာတော့ hosting platform ဒါမှမဟုတ် cloud ရဲ့ secret store ကို သုံးပါ။ Browser က API ကို သုံးချင်ရင် key ကို ကိုင်ထားပြီး provider ကို ကိုယ်စားခေါ်ပေးတဲ့ ကိုယ့်ရဲ့ backend ကို ခေါ်ပါ။
ပေါက်ကြားမှုအတွက် စီစဉ်ထားပါ၊ နောက်ဆုံးမှာ တစ်ခါခါ ဖြစ်မှာမို့ပါ။ Rotation ဆိုတာ credential ကို အချိန်ဇယားအတိုင်း ဒါမှမဟုတ် ဖြစ်ရပ်ပြီးနောက် အစားထိုးတာပါ — key အသစ်ဆောက်၊ deploy လုပ်၊ traffic အလုပ်လုပ်မလုပ် စစ်၊ ပြီးမှ key အဟောင်းကို revoke လုပ်ပါ။ Least privilege ဆိုတာ credential တစ်ခုစီကို လိုအပ်တဲ့ ခွင့်ပြုချက်ပဲ ပေးတာပါ — dashboard အတွက် read-only key၊ service တစ်ခုစီနဲ့ environment တစ်ခုစီအတွက် သီးခြား key၊ ဖြစ်နိုင်ရင် သက်တမ်းတို token။ ဒီအလေ့အကျင့်တွေရှိရင် test environment က ပေါက်ကြားတဲ့ key က production data ကို မထိနိုင်ပါဘူး။
- Request က credential နဲ့ ရောက်လာ
- Authenticate: ဒါ ဘယ်သူလဲ
- Authorize: ဒါ လုပ်ခွင့်ရှိလား
- ရရင် 200၊ မရရင် 401 ဒါမှမဟုတ် 403
401 က "ဘယ်သူလဲ မသိ" ဖြစ်ပြီး 403 က "သိတယ်၊ ဒါပေမယ့် မရဘူး" ပါ။
အထွေထွေ ပုံစံ: credential ကို ပို့တဲ့ header
Authorization: <scheme> <credential>
Authorization: Bearer <access-token>
X-API-Key: <api-key> (the header name is provider-specific)
Cookie: session=<session-id> (sent by the browser automatically)Authorization header က scheme (Bearer, Basic) နဲ့ credential ကို space တစ်ခုခြားပြီး ပါပါတယ်။ API key အတွက် header နာမည်က provider ပေါ်မူတည်လို့ documentation ကို စစ်ပါ။ Cookie ကိုတော့ ကိုယ်တိုင် မသတ်မှတ်ပါဘူး၊ browser က ပို့ပါတယ်။
Example: 401 နဲ့ 403 ကို ကိုယ်တိုင် မြင်ကြည့်မယ်
ဒီ mock API မှာ key နှစ်ခု ရှိပါတယ် — report ဖတ်ခွင့်ပဲ ရှိတဲ့ read key နဲ့ ဖျက်ခွင့်ပါ ရှိတဲ့ admin key။ Client က credential မပါဘဲ၊ မသိတဲ့ key နဲ့၊ read key နဲ့၊ admin key နဲ့ ခေါ်ပါတယ်။ ရလဒ်ကို ကြည့်ပါ — 401 နှစ်မျိုး (credential မပါ၊ မသိတဲ့ key)၊ 403 တစ်ခု (key မှန်ပေမယ့် ခွင့်ပြုချက်မရှိ) နဲ့ 200 တွေပါ။ Key တွေကို environment variable ကနေ ဖတ်တာ (မရှိရင် demo အတွက် အတု တန်ဖိုး သုံး) ကိုလည်း သတိထားပါ။
import http from "node:http";
// Fake credentials for this demo only. Real ones come from the environment.
const readKey = process.env.APIGUIDE_DEMO_READ_KEY ?? "read-key-123";
const adminKey = process.env.APIGUIDE_DEMO_ADMIN_KEY ?? "admin-key-456";
const scopesByKey = new Map([
[readKey, ["reports:read"]],
[adminKey, ["reports:read", "reports:write"]],
]);
function send(res, status, payload, extra = {}) {
res.writeHead(status, { "Content-Type": "application/json", ...extra });
res.end(JSON.stringify(payload));
}
// Returns true when the request may continue; otherwise it has already answered.
function authorize(req, res, scope) {
const header = req.headers.authorization ?? "";
const key = header.startsWith("Bearer ") ? header.slice(7) : "";
const scopes = scopesByKey.get(key);
if (!scopes) {
const error = header ? "invalid credentials" : "missing credentials";
send(res, 401, { error }, { "WWW-Authenticate": 'Bearer realm="reports"' });
return false;
}
if (!scopes.includes(scope)) {
send(res, 403, { error: "insufficient scope" });
return false;
}
return true;
}
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/reports") {
if (authorize(req, res, "reports:read")) send(res, 200, { reports: ["Q1", "Q2"] });
} else if (req.method === "DELETE" && req.url === "/reports/7") {
if (authorize(req, res, "reports:write")) send(res, 200, { deleted: 7 });
} else {
send(res, 404, { error: "not_found" });
}
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
async function call(label, method, path, key) {
const res = await fetch(base + path, {
method,
headers: key ? { Authorization: "Bearer " + key } : {},
});
console.log(label, "->", res.status, JSON.stringify(await res.json()));
const challenge = res.headers.get("www-authenticate");
if (challenge) console.log(" WWW-Authenticate:", challenge);
}
await call("GET /reports, no credentials", "GET", "/reports");
await call("GET /reports, unknown key", "GET", "/reports", "wrong-key");
await call("GET /reports, read-only key", "GET", "/reports", readKey);
await call("DELETE /reports/7, read-only key", "DELETE", "/reports/7", readKey);
await call("DELETE /reports/7, admin key", "DELETE", "/reports/7", adminKey);
server.close();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Secrets from the environment
const readKey = process.env.APIGUIDE_DEMO_READ_KEY ?? "read-key-123";Secret ကို process.env ကနေ ဖတ်တဲ့ ပုံစံပါ။ ?? ပြီးနောက်က တန်ဖိုးက demo run လို့ရအောင်ပဲ ပါတာပါ။ တကယ့် service မှာ variable မရှိရင် ချက်ချင်း error ပစ်ပြီး ရပ်သင့်ပါတယ်၊ အတု တန်ဖိုး မထားပါနဲ့။
- Key to permissions
[adminKey, ["reports:read", "reports:write"]],Credential တစ်ခုစီကို ခွင့်ပြုချက်စာရင်း (scope) နဲ့ ချိတ်ထားပါတယ်။ read key မှာ reports:write မပါလို့ ဖျက်လို့မရပါဘူး၊ ဒါက least privilege ပါပဲ။
- Parse the scheme
const key = header.startsWith("Bearer ") ? header.slice(7) : "";Authorization header က "Bearer " နဲ့ စပြီး credential လိုက်ပါတယ်။ "Bearer " က အက္ခရာ ၇ လုံးမို့ slice(7) က credential ကိုပဲ ယူပါတယ်။ Scheme မမှန်ရင် key က အလွတ်ဖြစ်ပြီး 401 ဖြစ်ပါတယ်။
- 401: who are you?
if (!scopes) {Key ကို မသိရင် (မပါလည်း၊ မှားလည်း) identity မသိတာမို့ 401 ပြန်ပါတယ်။ WWW-Authenticate header က client ကို Bearer scheme လိုကြောင်း ပြောပါတယ်။
- 403: known but not allowed
if (!scopes.includes(scope)) {Key ကို သိပြီး scope မပါရင် 403 ပါ။ Client က key တစ်ခုခုနဲ့ ထပ်ကြိုးစားလို့ ပြေလည်မှာ မဟုတ်ပါဘူး၊ ခွင့်ပြုချက် ပိုရှိတဲ့ credential လိုပါတယ်။
- Client sends the key
headers: key ? { Authorization: "Bearer " + key } : {},Key ရှိမှသာ Authorization header ကို ထည့်ပါတယ်၊ ပထမ call မှာ header လုံးဝ မပါပါဘူး။ Key ကို header မှာ ထည့်တာက URL ထဲ ထည့်တာထက် ဘေးကင်းပါတယ်။
GET /reports, no credentials -> 401 {"error":"missing credentials"}
WWW-Authenticate: Bearer realm="reports"
GET /reports, unknown key -> 401 {"error":"invalid credentials"}
WWW-Authenticate: Bearer realm="reports"
GET /reports, read-only key -> 200 {"reports":["Q1","Q2"]}
DELETE /reports/7, read-only key -> 403 {"error":"insufficient scope"}
DELETE /reports/7, admin key -> 200 {"deleted":7}Credential လေးမျိုးကို နှိုင်းယှဉ်ကြည့်ရအောင်
| အမျိုးအစား | ဘာကို ဖော်ထုတ်လဲ | ပုံမှန် အသုံးပြုမှု |
|---|---|---|
| API key | Application ဒါမှမဟုတ် project | Server-to-server ခေါ်ဆိုမှု၊ ရိုးရှင်းတဲ့ data API |
| Bearer token (JWT) | User ဒါမှမဟုတ် client၊ claim နဲ့ သက်တမ်းပါ | ကိုယ့် app ရဲ့ API၊ mobile နဲ့ single-page app |
| OAuth 2.0 access token | ကန့်သတ်ခွင့်ပြုချက် ပေးထားတဲ့ user | "... နဲ့ sign in"၊ ပြင်ပ ပေါင်းစပ်မှုတွေ |
| Session cookie | Login ဝင်ထားတဲ့ browser session | ကိုယ့် domain ပေါ်က ရိုးရိုး website |
တစ်ခုချင်းစီမှာ အဓိက အန္တရာယ်နဲ့ ကာကွယ်နည်း ရှိပါတယ်။ အောက်ပါ table က လက်တွေ့မှာ အကြိမ်ကြိမ် ကြုံရမယ့် အချက်တွေကို အကျဉ်းချုပ် ပြထားပါတယ်။
| အမျိုးအစား | အဓိက အန္တရာယ် | အဓိက ကာကွယ်နည်း |
|---|---|---|
| API key | မကုန်ဘဲ ကျယ်ကျယ်ပြန့်ပြန့် မျှဝေမိတတ် | Rotate၊ ရနိုင်ရင် scope နဲ့ IP ကန့်၊ server မှာပဲ ထား |
| Bearer token (JWT) | ခိုးထားတဲ့ token က သက်တမ်းမကုန်မချင်း အလုပ်လုပ် | သက်တမ်းတို၊ HTTPS၊ log မရေး |
| OAuth 2.0 | Scope ကျယ်လွန်းတာ၊ refresh token ပေါက်ကြားတာ | လိုတဲ့ scope ပဲ တောင်း၊ refresh token ကို လုံခြုံစွာ သိမ်း |
| Session cookie | CSRF | HttpOnly, Secure, SameSite flag နဲ့ CSRF token |
Project တစ်ခုထဲမှာ secret တွေကို ဘယ်လို ထိန်းမလဲ
တူညီတဲ့ ပုံစံက project တိုင်းမှာ အလုပ်ဖြစ်ပါတယ်။ Real token ကို .env ထဲမှာ ထားပြီး .gitignore ထဲ ထည့်ပါ၊ commit လုပ်မယ့် .env.example မှာ နာမည်ပဲ ထားပါ။ Node 20.6 ဒါမှမဟုတ် ပိုသစ်တာဆိုရင် node --env-file=.env app.mjs နဲ့ .env ကို load လုပ်နိုင်ပါတယ်။ Package မလိုပါဘူး။
# .gitignore
.env
# .env (never committed; real values live here)
API_TOKEN=paste-your-real-token-here
# .env.example (committed; names only)
API_TOKEN=
# Run with the variables loaded, then call an API with the token
node --env-file=.env app.mjs
curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com/v1/reportsVariable မရှိတာကို ကြိုစစ်တဲ့ helper တစ်ခုက "ပထမ request မှာ 401 ရလို့ ဘာကြောင့်လဲ ရှာနေရတာ" ကို ကာကွယ်ပေးပါတယ်။ ပျောက်နေရင် app စတင်တာနဲ့ ရှင်းရှင်းလင်းလင်း error ပေးပါတယ်။ (Windows မှာ variable ကို cmd က %API_TOKEN%၊ PowerShell က $env:API_TOKEN နဲ့ ကိုးကားပါတယ်၊ Windows quote ကွာခြားပုံကို tools lesson မှာ ပြထားပါတယ်။)
function requireEnv(name) {
const value = process.env[name];
if (!value) {
throw new Error("Missing environment variable " + name);
}
return value;
}
try {
requireEnv("APIGUIDE_DEMO_TOKEN_THAT_IS_NOT_SET");
} catch (error) {
console.log(error.message);
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ auth ကို ဘယ်လို ကိုင်တွယ်လဲ
ပေါက်ကြားမှုတွေက အမြဲလိုလို ဆိုးရွားတဲ့ ရည်ရွယ်ချက်မှ မဟုတ်ပါဘူး၊ မတော်တဆ commit ပါ။ အလိုအလျောက် scanner တွေက public repository တွေမှာ key တွေကို ရှာနေလို့ ပေါက်ကြားတဲ့ key ကို မြန်မြန် အလွဲသုံးစားလုပ်နိုင်ပါတယ်၊ ဒါကြောင့် provider အချို့ကိုယ်တိုင် secret scanning လုပ်ပြီး ပေါက်ကြားတဲ့ key ကို revoke လုပ်ပါတယ်။ Team တွေက CI pipeline ထဲ secret scanner ထည့်ထားကြပြီး key တစ်ခုစီမှာ ပိုင်ရှင်၊ ရည်ရွယ်ချက်နဲ့ သက်တမ်းကို မှတ်ထားကြပါတယ်။
Production မှာ secret တွေကို hosting platform ရဲ့ secret store ဒါမှမဟုတ် cloud provider ရဲ့ secret manager ထဲ သိမ်းပြီး app စတင်တဲ့အခါ environment variable အဖြစ် ထိုးသွင်းပါတယ်။ Environment တစ်ခုစီ (development၊ staging၊ production) မှာ သီးခြား key ရှိပြီး service တစ်ခုစီမှာလည်း ကိုယ်ပိုင် key ရှိတာမို့ ပေါက်ကြားမှုတစ်ခုက အကျိုးသက်ရောက်မှု ကန့်သတ်ပါတယ်။ Browser ဒါမှမဟုတ် mobile app က provider ကို တိုက်ရိုက် မခေါ်ဘဲ ကိုယ့် backend ကို ခေါ်ပြီး backend က key နဲ့ provider ကို ခေါ်တဲ့ ပုံစံက ရိုးရှင်းပြီး ထိရောက်ပါတယ်၊ ဒါကြောင့် backend ကလည်း rate limit နဲ့ အသုံးပြုမှု ထိန်းချုပ်မှုကို ထည့်နိုင်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Secret ကို front-end ဒါမှမဟုတ် mobile code ထဲ ထည့်တာ
Minify ဒါမှမဟုတ် obfuscate လုပ်ထားလည်း browser ထဲက JavaScript နဲ့ app package ကို ဖွင့်ကြည့်လို့ ရပါတယ်၊ network tab ထဲမှာလည်း header က မြင်ရပါတယ်။ Client ဘက်မှာ ရှိတဲ့ ဘာမဆိုက လူတိုင်း သိနိုင်တာမို့ secret ကို backend မှာပဲ ထားပါ။
2. .env ကို commit လုပ်ပြီး နောက်မှ ဖျက်တာ
Git က commit တိုင်းကို မှတ်ထားလို့ နောက် commit မှာ ဖျက်လိုက်လည်း history ထဲမှာ ရှိနေဆဲပါ၊ repository ကို clone လုပ်သူ အားလုံးက ဖတ်နိုင်ပါတယ်။ ပေါက်ကြားပြီဆိုရင် history ကို ရှင်းဖို့ မစောင့်ဘဲ key ကို ချက်ချင်း revoke လုပ်ပြီး အသစ်ထုတ်ပါ။
3. 401 နဲ့ 403 ကို တစ်ခုတည်းလို သဘောထားတာ
401 မှာ credential မပါတာ၊ မှားတာ၊ သက်တမ်းကုန်တာကို စစ်ပြီး token အသစ် ယူကြည့်လို့ ရပါတယ်။ 403 မှာတော့ identity မှန်နေပြီမို့ ထပ်ကြိုးစားလည်း အလကားပါ၊ ခွင့်ပြုချက် (scope) ပြဿနာကို ကြည့်ရပါမယ်။ နှစ်ခုကို တစ်ခုတည်း retry loop ထဲ ထည့်ရင် အချိန်ကုန်ပြီး ငြင်းပယ်မှုတွေ များလာနိုင်ပါတယ်။
4. Authorization header တစ်ခုလုံးကို log ထဲ ရေးတာ
Debug လုပ်ရင်း request header အားလုံးကို print ထုတ်ရင် token က log file၊ monitoring tool နဲ့ အဖွဲ့လိုက် ကြည့်တဲ့ dashboard တွေထဲ ရောက်ပါတယ်။ Authorization, Cookie နဲ့ X-API-Key လို header တွေကို log မရေးခင် ဖျောက်ပါ (အောက်က challenge မှာ ကိုယ်တိုင် ရေးကြည့်ပါမယ်)။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Credential တစ်ခုစီကို လိုအပ်တဲ့ ခွင့်ပြုချက်ပဲ ပေးပါ၊ dashboard အတွက် read-only key ကို သုံးပါ။
- Environment တစ်ခုစီနဲ့ service တစ်ခုစီအတွက် သီးခြား key ထုတ်ပါ၊ key တွေကို အချိန်ဇယားအတိုင်း rotate လုပ်ပါ။
- ဖြစ်နိုင်ရင် သက်တမ်းတို token ကို သုံးပါ၊ ခိုးခံရရင် အလုပ်လုပ်မယ့် အချိန် တိုသွားပါမယ်။
- CI မှာ secret scanning ထည့်ပါ၊ ပေါက်ကြားမှု သံသယရှိရင် ချက်ချင်း revoke လုပ်ပါ။
Mini challenge
Mini challenge: log ထဲက secret တွေကို ဖျောက်ပါ
Header object တစ်ခုကို လက်ခံပြီး Authorization၊ Proxy-Authorization၊ Cookie၊ X-API-Key တန်ဖိုးတွေကို ဖျောက်ထားတဲ့ ကူးယူမှုအသစ်တစ်ခု ပြန်ပေးတဲ့ redactHeaders() ကို ရေးပါ။ တန်ဖိုးတစ်ခုရဲ့ နောက်ဆုံး အက္ခရာ ၄ လုံးပဲ ကျန်ခဲ့ရမယ်။
- Header နာမည်ကို စာလုံးအကြီးအသေး မခွဲဘဲ နှိုင်းယှဉ်ရမယ် (Authorization နဲ့ authorization နှစ်ခုလုံး ဖျောက်ရမယ်)
- "Bearer " လို scheme ပါရင် scheme ကို ထားပြီး credential ကိုပဲ ဖျောက်ရမယ် (Bearer ****9999)
- Secret အပိုင်းက အက္ခရာ ၈ လုံး ဒါမှမဟုတ် ပိုတိုရင် **** ချည်းပဲ ပြရမယ် (နောက်ဆုံး ၄ လုံး မပြရ)
- မူရင်း object ကို မပြင်ရ၊ sensitive မဟုတ်တဲ့ header တွေ မပြောင်းရ
const SENSITIVE = [
// TODO: authorization, proxy-authorization, cookie, x-api-key (lower case)
];
function mask(value) {
// TODO: keep a leading scheme such as "Bearer " when the value has a space
// TODO: replace the secret part with **** plus its last 4 characters
// (only **** when the secret part has 8 characters or fewer)
}
function redactHeaders(headers) {
// TODO: build a new object, masking the sensitive headers
}
// TODO: print JSON.stringify(redactHeaders({ ... })) for a sample set of headersရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ် (တစ်ကြောင်းတည်း): {"Accept":"application/json","Authorization":"Bearer ****9999","X-API-Key":"****5555","Cookie":"****7777","Proxy-Authorization":"Basic ****"}
အရိပ်အမြွက် ကြည့်ရန်
Scheme ကို ခွဲဖို့ text.indexOf(" ") ကို သုံးပါ၊ မတွေ့ရင် -1 ပါ။ Secret အပိုင်းက secret.slice(-4) နဲ့ နောက်ဆုံး ၄ လုံးကို ယူပါ။ Header နာမည်ကို name.toLowerCase() ပြီးမှ SENSITIVE.includes(...) နဲ့ စစ်ပါ။
အဖြေ ကြည့်ရန်
const SENSITIVE = ["authorization", "proxy-authorization", "cookie", "x-api-key"];
function mask(value) {
const text = String(value);
const space = text.indexOf(" ");
const scheme = space > 0 ? text.slice(0, space + 1) : "";
const secret = text.slice(scheme.length);
return scheme + "****" + (secret.length > 8 ? secret.slice(-4) : "");
}
function redactHeaders(headers) {
const copy = {};
for (const [name, value] of Object.entries(headers)) {
copy[name] = SENSITIVE.includes(name.toLowerCase()) ? mask(value) : value;
}
return copy;
}
const headers = {
Accept: "application/json",
Authorization: "Bearer abc123SECRET9999",
"X-API-Key": "key_live_5555",
Cookie: "session=s3cr3t-value-7777",
"Proxy-Authorization": "Basic abc",
};
console.log(JSON.stringify(redactHeaders(headers)));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Authentication က ဘယ်သူလဲ၊ authorization က ဘာလုပ်ခွင့်ရှိလဲ ဆိုတာပါ။ 401 က credential မပါ၊ မှားတာ၊ 403 က identity သိပေမယ့် ခွင့်ပြုချက်မရှိတာပါ။
- API key က application ကို၊ bearer token (JWT) က သက်တမ်းပါတဲ့ user/client ကို၊ OAuth 2.0 က user ပေးထားတဲ့ ကန့်သတ်ခွင့်ကို၊ session cookie က login ဝင်ထားတဲ့ browser ကို ကိုယ်စားပြုပါတယ်။
- Secret တွေကို client-side code၊ git၊ log၊ URL ထဲ မထည့်ဘဲ .env နဲ့ environment variable ထဲ ထားပါ။
- Rotation နဲ့ least privilege က ပေါက်ကြားမှုကို ကန့်သတ်ပေးပါတယ် — key တစ်ခုစီကို လိုအပ်တဲ့ ခွင့်ပြုချက်ပဲ ပေးပြီး အချိန်ဇယားအတိုင်း အစားထိုးပါ။