Commit ·
a90e733
1
Parent(s): 8700e64
security: fix open redirect, password length leak, needs_regen on failure
Browse files1. Open redirect: validate `next` param with urlparse; reject URLs that
have a scheme or netloc (external URLs) and fall back to employees.index
2. Password length disclosure: remove len(APP_PASSWORD) from login failure
flash message — no reason to expose the expected password length
3. needs_regen stale on failed generation: reset needs_regen=0 when
status is "failed" too, not only "done" — a failed run produces no
valid schedule to become outdated
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- db/repositories.py +2 -2
- routes/auth_routes.py +15 -3
db/repositories.py
CHANGED
|
@@ -199,8 +199,8 @@ def update_period_gen_status(period_id: int, status: str, message: str = ""):
|
|
| 199 |
"""app_context / キャッシュ不要な低レベル DB 更新(コールバックスレッドから安全に呼べる)"""
|
| 200 |
from db.database import Connection
|
| 201 |
conn = Connection()
|
| 202 |
-
# 生成完了時は needs_regen をリセット
|
| 203 |
-
if status
|
| 204 |
conn.execute(
|
| 205 |
"UPDATE schedule_periods SET gen_status=?, gen_message=?, needs_regen=0 WHERE id=?",
|
| 206 |
(status, message, period_id)
|
|
|
|
| 199 |
"""app_context / キャッシュ不要な低レベル DB 更新(コールバックスレッドから安全に呼べる)"""
|
| 200 |
from db.database import Connection
|
| 201 |
conn = Connection()
|
| 202 |
+
# 生成完了・失敗時はともに needs_regen をリセット(failedは新しいシフトがない)
|
| 203 |
+
if status in ("done", "failed"):
|
| 204 |
conn.execute(
|
| 205 |
"UPDATE schedule_periods SET gen_status=?, gen_message=?, needs_regen=0 WHERE id=?",
|
| 206 |
(status, message, period_id)
|
routes/auth_routes.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import logging
|
|
|
|
| 2 |
from flask import Blueprint, render_template, request, redirect, url_for, session, flash, make_response
|
| 3 |
from auth import APP_PASSWORD, SESSION_VERSION
|
| 4 |
|
|
@@ -6,6 +7,15 @@ logger = logging.getLogger(__name__)
|
|
| 6 |
bp = Blueprint("auth", __name__)
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def _login_page(**kwargs):
|
| 10 |
"""キャッシュ無効ヘッダー付きでログインページを返す"""
|
| 11 |
resp = make_response(render_template("auth/login.html", pw_len=len(APP_PASSWORD), **kwargs))
|
|
@@ -24,8 +34,9 @@ def login():
|
|
| 24 |
@bp.post("/login")
|
| 25 |
def login_post():
|
| 26 |
pwd = request.form.get("password", "").strip()
|
| 27 |
-
# next は hidden フィールドでも query param でも受け取る
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
logger.warning("LOGIN attempt: input_len=%d, expected_len=%d, match=%s",
|
| 31 |
len(pwd), len(APP_PASSWORD), pwd == APP_PASSWORD)
|
|
@@ -37,7 +48,8 @@ def login_post():
|
|
| 37 |
session.permanent = True
|
| 38 |
return redirect(next_url)
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
return _login_page()
|
| 42 |
|
| 43 |
|
|
|
|
| 1 |
import logging
|
| 2 |
+
from urllib.parse import urlparse
|
| 3 |
from flask import Blueprint, render_template, request, redirect, url_for, session, flash, make_response
|
| 4 |
from auth import APP_PASSWORD, SESSION_VERSION
|
| 5 |
|
|
|
|
| 7 |
bp = Blueprint("auth", __name__)
|
| 8 |
|
| 9 |
|
| 10 |
+
def _safe_next(url: str) -> str:
|
| 11 |
+
"""next パラメータを相対パスのみに限定してオープンリダイレクトを防ぐ"""
|
| 12 |
+
parsed = urlparse(url)
|
| 13 |
+
# scheme や netloc が含まれる(外部URL)場合はデフォルトに戻す
|
| 14 |
+
if parsed.scheme or parsed.netloc:
|
| 15 |
+
return url_for("employees.index")
|
| 16 |
+
return url or url_for("employees.index")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
def _login_page(**kwargs):
|
| 20 |
"""キャッシュ無効ヘッダー付きでログインページを返す"""
|
| 21 |
resp = make_response(render_template("auth/login.html", pw_len=len(APP_PASSWORD), **kwargs))
|
|
|
|
| 34 |
@bp.post("/login")
|
| 35 |
def login_post():
|
| 36 |
pwd = request.form.get("password", "").strip()
|
| 37 |
+
# next は hidden フィールドでも query param でも受け取る(外部URLは除外)
|
| 38 |
+
raw_next = request.form.get("next") or request.args.get("next") or ""
|
| 39 |
+
next_url = _safe_next(raw_next)
|
| 40 |
|
| 41 |
logger.warning("LOGIN attempt: input_len=%d, expected_len=%d, match=%s",
|
| 42 |
len(pwd), len(APP_PASSWORD), pwd == APP_PASSWORD)
|
|
|
|
| 48 |
session.permanent = True
|
| 49 |
return redirect(next_url)
|
| 50 |
|
| 51 |
+
# パスワード長は漏らさない
|
| 52 |
+
flash("パスワードが違います", "error")
|
| 53 |
return _login_page()
|
| 54 |
|
| 55 |
|