-
Notifications
You must be signed in to change notification settings - Fork 1
fix: use bcrypt +hash instead of storing plain password (issue #19) #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |||||||||||
| import hmac | ||||||||||||
| import time | ||||||||||||
| import collections | ||||||||||||
| import bcrypt | ||||||||||||
| from datetime import datetime, timedelta | ||||||||||||
| from typing import List, Optional | ||||||||||||
|
|
||||||||||||
|
|
@@ -28,7 +29,7 @@ | |||||||||||
| from scheduler_jobs import send_due_emails, generate_upcoming, sync_to_sheets, advance_processing | ||||||||||||
| from transcript import resolve_playlist | ||||||||||||
| from mailer import send_email as do_send_email, send_welcome_email | ||||||||||||
| from config import CRON_SECRET, ADMIN_SECRET, ADMIN_USER, ADMIN_PASSWORD, SENTRY_DSN, CORS_ORIGINS | ||||||||||||
| from config import CRON_SECRET, ADMIN_SECRET, ADMIN_USER, ADMIN_PASSWORD, ADMIN_PASSWORD_HASH, SENTRY_DSN, CORS_ORIGINS | ||||||||||||
|
|
||||||||||||
| import sentry_sdk | ||||||||||||
| from sentry_sdk.integrations.fastapi import FastApiIntegration | ||||||||||||
|
|
@@ -447,13 +448,30 @@ def _get_admin_token(request: Request) -> str: | |||||||||||
|
|
||||||||||||
| def _check_admin(request: Request): | ||||||||||||
| token = _get_admin_token(request) | ||||||||||||
| if ADMIN_SECRET and token == ADMIN_SECRET: | ||||||||||||
| if ADMIN_SECRET and hmac.compare_digest(token, ADMIN_SECRET): | ||||||||||||
| return | ||||||||||||
|
Comment on lines
+451
to
452
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also applies to: 858-866 🤖 Prompt for AI Agents |
||||||||||||
| if db.validate_admin_session(token): | ||||||||||||
| return | ||||||||||||
| raise HTTPException(401, "Unauthorized") | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def _verify_admin_password(candidate: str) -> bool: | ||||||||||||
| if not candidate: | ||||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| if ADMIN_PASSWORD_HASH: | ||||||||||||
| try: | ||||||||||||
| return bcrypt.checkpw(candidate.encode("utf-8"), ADMIN_PASSWORD_HASH.encode("utf-8")) | ||||||||||||
| except (ValueError, TypeError): | ||||||||||||
| return False | ||||||||||||
|
|
||||||||||||
| # fallback if ADMIN_PASSWORD_HASH is not set | ||||||||||||
| if ADMIN_PASSWORD: | ||||||||||||
| return hmac.compare_digest(candidate, ADMIN_PASSWORD) | ||||||||||||
|
Comment on lines
+468
to
+470
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plaintext fallback keeps the old auth path alive. If Also applies to: 500-500 🤖 Prompt for AI Agents |
||||||||||||
|
|
||||||||||||
| return False | ||||||||||||
|
Comment on lines
+469
to
+472
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Admin auth silently falls back to plaintext Prompt for AI agents
Suggested change
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| # ── Auth ────────────────────────────────────────────────────────────────── | ||||||||||||
|
|
||||||||||||
| class AdminLoginRequest(BaseModel): | ||||||||||||
|
|
@@ -479,9 +497,7 @@ def admin_login(req: AdminLoginRequest, request: Request): | |||||||||||
| raise HTTPException(429, "Too many login attempts. Try again in a minute.") | ||||||||||||
| attempts.append(now) | ||||||||||||
|
|
||||||||||||
| if not ADMIN_PASSWORD: | ||||||||||||
| raise HTTPException(503, "Admin password not configured") | ||||||||||||
| if req.username != ADMIN_USER or not hmac.compare_digest(req.password, ADMIN_PASSWORD): | ||||||||||||
| if not hmac.compare_digest(req.username, ADMIN_USER) or not _verify_admin_password(req.password): | ||||||||||||
| raise HTTPException(401, "Invalid credentials") | ||||||||||||
| token = db.create_admin_session() | ||||||||||||
| db.prune_old_sessions() | ||||||||||||
|
|
@@ -839,12 +855,14 @@ def admin_edit_transcript(video_id: str, body: TranscriptBody, request: Request) | |||||||||||
| class IngestByYoutubeIdBody(BaseModel): | ||||||||||||
| youtube_id: str | ||||||||||||
| text: str | ||||||||||||
| password: str | ||||||||||||
| token: Optional[str] = None | ||||||||||||
|
|
||||||||||||
| @app.post("/api/admin/ingest-by-youtube-id") | ||||||||||||
| def admin_ingest_by_youtube_id(body: IngestByYoutubeIdBody): | ||||||||||||
| """Bookmarklet endpoint: accepts youtube_id + transcript text + admin password.""" | ||||||||||||
| if not ADMIN_PASSWORD or not hmac.compare_digest(body.password, ADMIN_PASSWORD): | ||||||||||||
| """Bookmarklet endpoint: accepts youtube_id + transcript text + admin token.""" | ||||||||||||
| if not ADMIN_SECRET: | ||||||||||||
| raise HTTPException(503, "Admin token is not configured") | ||||||||||||
| if not body.token or not hmac.compare_digest(body.token, ADMIN_SECRET): | ||||||||||||
| raise HTTPException(401, "Unauthorized") | ||||||||||||
| text = body.text.strip() | ||||||||||||
| if len(text) < 100: | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ google-auth | |
| apscheduler | ||
| pytz | ||
| sentry-sdk[fastapi] | ||
| bcrypt | ||
Uh oh!
There was an error while loading. Please reload this page.