Skip to content

Commit 9b98d89

Browse files
committed
refactor: body 값 및 검증 코드 개선
1 parent f6619d9 commit 9b98d89

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/middlewares/auth.middleware.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,28 @@ const verifyBearerTokens = () => {
7777
function verifySignature(request: Request, res: Response, next: NextFunction) {
7878
try {
7979
if(!process.env.SENTRY_CLIENT_SECRET) throw new Error("SENTRY_CLIENT_SECRET가 env에 없습니다");
80+
8081
const hmac = crypto.createHmac("sha256", process.env.SENTRY_CLIENT_SECRET);
81-
hmac.update(JSON.stringify(request.body), "utf8");
82+
83+
// Raw body 사용 - Express에서 파싱되기 전의 원본 데이터 필요
84+
// request.rawBody가 없다면 fallback으로 JSON.stringify 사용 (완벽하지 않음)
85+
// @ts-expect-error - rawBody는 커스텀 미들웨어에서 추가되는 속성
86+
const bodyToVerify = request.rawBody || JSON.stringify(request.body);
87+
const sentrySignature = request.headers["sentry-hook-signature"];
88+
89+
if (!bodyToVerify) throw new Error("요청 본문이 없습니다.");
90+
if(!sentrySignature) throw new Error("시그니처 헤더가 없습니다.");
91+
92+
hmac.update(bodyToVerify, "utf8");
8293
const digest = hmac.digest("hex");
8394

84-
if(digest !== request.headers["sentry-hook-signature"]) {
85-
throw new Error("유효하지 않은 시그니처 헤더입니다.");
86-
}
95+
if(digest !== sentrySignature) throw new Error(`유효하지 않은 시그니처 헤더입니다.`);
96+
8797
next();
88-
} catch (error) {
98+
} catch (error) {
8999
logger.error('시그니처 검증 중 오류가 발생하였습니다. : ', error);
90100
next(error);
91101
}
92-
93102
}
94103

95104
/**

0 commit comments

Comments
 (0)