Skip to content
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

Add admin hash config and warning #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/pwncore/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import bcrypt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use passlib.hash > bcrypt instead.

from dataclasses import dataclass
import warnings

"""
Sample messages:
Expand Down Expand Up @@ -42,6 +44,8 @@
"users_not_found": 24,
}

admin_hash_value = os.environ.get("PWNCORE_ADMIN_HASH", bcrypt.hashpw('pwncore'.encode(), bcrypt.gensalt()).decode())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bcrypt.hashpw('pwncore'.encode(), bcrypt.gensalt()).decode() > bcrypt.hash("pwncore")

using_default_admin = os.environ.get("PWNCORE_ADMIN_HASH") is None

@dataclass
class Config:
Expand All @@ -55,20 +59,25 @@ class Config:
jwt_valid_duration: int
hint_penalty: int
max_members_per_team: int

admin_hash: str

config = Config(
development=True,
development=False,
# db_url="sqlite://:memory:",
db_url=os.environ.get("DATABASE_URL", "sqlite://:memory:"),
docker_url=None, # None for default system docker
# docker_url=None, # None for default system docker
# Or set it to an arbitrary URL for testing without Docker
# docker_url="http://google.com",
docker_url="http://google.com",
flag="C0D",
max_containers_per_team=3,
jwt_secret="mysecret",
jwt_valid_duration=12, # In hours
msg_codes=msg_codes,
hint_penalty=50,
max_members_per_team=3,
admin_hash=admin_hash_value,
)

# Warn in production if env not loaded
if not config.development and using_default_admin:
warnings.warn("Default admin hash being used in production!", RuntimeWarning)
5 changes: 2 additions & 3 deletions src/pwncore/routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
if config.development:
logging.basicConfig(level=logging.INFO)

ADMIN_HASH = "$2b$12$USIGDWgl8WSgSoGauDTKE.ZAKyInaJn84fsZ.ARA6FmntIZeNCTUq"
NAMES = [
"Mimas",
"Enceladus",
Expand Down Expand Up @@ -57,7 +56,7 @@ async def _del_cont(id: str):
async def calculate_team_coins(
response: Response, req: Request
): # Inefficient, anyways will be used only once
if not bcrypt.verify((await req.body()).strip(), ADMIN_HASH):
if not bcrypt.verify((await req.body()).strip(), config.admin_hash): # Use config.admin_hash
response.status_code = 401
return
async with in_transaction():
Expand Down Expand Up @@ -88,7 +87,7 @@ async def calculate_team_coins(
async def init_db(
response: Response, req: Request
): # Inefficient, anyways will be used only once
if not bcrypt.verify((await req.body()).strip(), ADMIN_HASH):
if not bcrypt.verify((await req.body()).strip(), config.admin_hash):
response.status_code = 401
return
await Problem.create(
Expand Down