암호 서명 검증 부적절 (JWT None 알고리즘)
Improper Verification of Cryptographic Signature
Last updated
Improper Verification of Cryptographic Signature
Last updated
const express = require("express");
const jwt = require("jsonwebtoken");
const app = express();
// 취약: 키 없이(또는 빈 문자열 등 falsy) 검증 수행
app.get("/profile", (req, res) => {
const auth = req.headers.authorization || "";
const token = auth.replace(/^Bearer\s+/i, "");
// BAD: falsy 키 + none 허용 -> 서명 검증 우회 가능
const payload = jwt.verify(token, "", { algorithms: ["HS256", "none"] });
// 위조된 payload로도 접근 가능해짐
res.json({ user: payload.sub, admin: payload.admin });
});
app.listen(3000);const express = require("express");
const jwt = require("jsonwebtoken");
const app = express();
const JWT_SECRET = process.env.JWT_SECRET; // 안전한 보관 필수
app.get("/profile", (req, res) => {
const m = (req.headers.authorization || "").match(/^Bearer\s+(.+)$/i);
if (!m) return res.status(401).send("Missing token");
try {
const payload = jwt.verify(m[1], JWT_SECRET, {
algorithms: ["HS256"], // none 미허용, 필요한 알고리즘만
issuer: "auth.example.com", // iss 검증
audience: "my-api", // aud 검증
});
return res.json({ user: payload.sub });
} catch (e) {
return res.status(401).send("Invalid token");
}
});
app.listen(3000);