m1m02530125 Claude Sonnet 4.6 commited on
Commit
88bdacf
·
1 Parent(s): 93c7d2b

fix: auth password trim + session version to invalidate stale sessions

Browse files

- Strip whitespace from APP_PASSWORD and submitted password to prevent
comparison failure caused by trailing newline in HuggingFace secrets
- Add SESSION_VERSION check so old sessions (set before password was
configured) are automatically invalidated on next request

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (3) hide show
  1. auth.py +7 -2
  2. routes/auth_routes.py +7 -4
  3. web_app.py +3 -2
auth.py CHANGED
@@ -3,7 +3,11 @@ import os
3
  from functools import wraps
4
  from flask import session, redirect, url_for, request
5
 
6
- APP_PASSWORD = os.environ.get("APP_PASSWORD", "")
 
 
 
 
7
 
8
 
9
  def login_required(f):
@@ -11,7 +15,8 @@ def login_required(f):
11
  def decorated(*args, **kwargs):
12
  if not APP_PASSWORD:
13
  return f(*args, **kwargs) # パスワード未設定なら認証スキップ
14
- if not session.get("authenticated"):
 
15
  return redirect(url_for("auth.login", next=request.path))
16
  return f(*args, **kwargs)
17
  return decorated
 
3
  from functools import wraps
4
  from flask import session, redirect, url_for, request
5
 
6
+ # 末尾スペース・改行を除去して比較ミスを防ぐ
7
+ APP_PASSWORD = os.environ.get("APP_PASSWORD", "").strip()
8
+
9
+ # セッション無効化用バージョン — 上げると全デバイスの既存セッションが失効する
10
+ SESSION_VERSION = 1
11
 
12
 
13
  def login_required(f):
 
15
  def decorated(*args, **kwargs):
16
  if not APP_PASSWORD:
17
  return f(*args, **kwargs) # パスワード未設定なら認証スキップ
18
+ if not session.get("authenticated") or session.get("sv") != SESSION_VERSION:
19
+ session.clear()
20
  return redirect(url_for("auth.login", next=request.path))
21
  return f(*args, **kwargs)
22
  return decorated
routes/auth_routes.py CHANGED
@@ -1,26 +1,29 @@
1
  from flask import Blueprint, render_template, request, redirect, url_for, session, flash
2
- from auth import APP_PASSWORD
3
 
4
  bp = Blueprint("auth", __name__)
5
 
6
 
7
  @bp.get("/login")
8
  def login():
9
- if session.get("authenticated"):
10
  return redirect(url_for("employees.index"))
11
  return render_template("auth/login.html")
12
 
13
 
14
  @bp.post("/login")
15
  def login_post():
16
- pwd = request.form.get("password", "")
 
17
  next_url = request.args.get("next") or url_for("employees.index")
18
  if APP_PASSWORD and pwd == APP_PASSWORD:
 
19
  session["authenticated"] = True
 
20
  session.permanent = True
21
  return redirect(next_url)
22
  flash("パスワードが違います", "error")
23
- return render_template("auth/login.html", next=next_url)
24
 
25
 
26
  @bp.get("/logout")
 
1
  from flask import Blueprint, render_template, request, redirect, url_for, session, flash
2
+ from auth import APP_PASSWORD, SESSION_VERSION
3
 
4
  bp = Blueprint("auth", __name__)
5
 
6
 
7
  @bp.get("/login")
8
  def login():
9
+ if session.get("authenticated") and session.get("sv") == SESSION_VERSION:
10
  return redirect(url_for("employees.index"))
11
  return render_template("auth/login.html")
12
 
13
 
14
  @bp.post("/login")
15
  def login_post():
16
+ # 入力パスワードも strip して比較
17
+ pwd = request.form.get("password", "").strip()
18
  next_url = request.args.get("next") or url_for("employees.index")
19
  if APP_PASSWORD and pwd == APP_PASSWORD:
20
+ session.clear()
21
  session["authenticated"] = True
22
+ session["sv"] = SESSION_VERSION
23
  session.permanent = True
24
  return redirect(next_url)
25
  flash("パスワードが違います", "error")
26
+ return render_template("auth/login.html")
27
 
28
 
29
  @bp.get("/logout")
web_app.py CHANGED
@@ -8,7 +8,7 @@ from flask import Flask, g, session, redirect, url_for, request
8
  from flask_compress import Compress
9
  from cache import cache
10
  from db.database import initialize_db
11
- from auth import APP_PASSWORD
12
 
13
 
14
  def create_app():
@@ -45,7 +45,8 @@ def create_app():
45
  exempt = {"auth.login", "auth.login_post", "static"}
46
  if request.endpoint in exempt:
47
  return
48
- if not session.get("authenticated"):
 
49
  return redirect(url_for("auth.login", next=request.path))
50
 
51
  from routes.employees import bp as emp_bp
 
8
  from flask_compress import Compress
9
  from cache import cache
10
  from db.database import initialize_db
11
+ from auth import APP_PASSWORD, SESSION_VERSION
12
 
13
 
14
  def create_app():
 
45
  exempt = {"auth.login", "auth.login_post", "static"}
46
  if request.endpoint in exempt:
47
  return
48
+ if not session.get("authenticated") or session.get("sv") != SESSION_VERSION:
49
+ session.clear()
50
  return redirect(url_for("auth.login", next=request.path))
51
 
52
  from routes.employees import bp as emp_bp