説明
ログインや決済後にリクエストのURLをそのまま sendRedirect に渡すと、利用者の移動先を呼び出し元が指定できます。
想定される影響
サービスのリンクからフィッシングサイトや悪意のあるダウンロード先へ誘導されるおそれがあります。OAuthやSSOがその宛先を信頼すると、認証情報やトークンが漏えいする可能性もあります。
対処方法
next=profileのような固定キーと内部パスをサーバーで対応付けてください。未登録のキーは拒否するか、既定のパスに移動させます。- 外部URLを受け取る必要がある場合は、URLを解析して正規化し、
httpsだけを許可してください。ホストとポートは許可リストと完全一致で照合します。startsWithやcontainsではホストの一致を確認できません。
例
Springコントローラーの抜粋です。必要なimportとアプリケーションの内部ルート定義は省略しています。
変更前
java
@GetMapping("/login/success")
public void loginSuccess(
@RequestParam("next") String next,
HttpServletResponse response
) throws IOException {
response.sendRedirect(next);
}
変更後
java
private static final Map<String, String> ALLOWED_REDIRECTS = Map.of(
"profile", "/account/profile",
"orders", "/account/orders"
);
@GetMapping("/login/success")
public void loginSuccess(
@RequestParam(value = "next", defaultValue = "profile") String next,
HttpServletResponse response
) throws IOException {
String redirectPath = ALLOWED_REDIRECTS.getOrDefault(next, "/account/profile");
response.sendRedirect(redirectPath);
}
profile と orders はサーバーに登録済みのパスを選択します。next が未指定の場合や未登録の値なら /account/profile に移動します。