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

Flappybird security #628

Open
wants to merge 7 commits into
base: main
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
18 changes: 18 additions & 0 deletions app/modules/flappybird/endpoints_flappybird.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import uuid
from datetime import UTC, datetime

Expand All @@ -14,6 +15,17 @@
)
from app.types.module import Module


def genKey(score: int) -> int:
"""Generates a key based on the score of the player."""
data = f"{score}"

hash_bytes = hashlib.sha256(data.encode()).digest()
key = int.from_bytes(hash_bytes[:4], byteorder="big")

return key


module = Module(
root="flappybird",
tag="Flappy Bird",
Expand Down Expand Up @@ -91,6 +103,12 @@ async def create_flappybird_score(
# And get the current date and time
creation_time = datetime.now(UTC)

if genKey(flappybird_score.value) != flappybird_score.key:
raise HTTPException(
status_code=400,
detail="Invalid key for the provided score",
)

db_flappybird_score = models_flappybird.FlappyBirdScore(
id=score_id,
user_id=user.id,
Expand Down
1 change: 1 addition & 0 deletions app/modules/flappybird/schemas_flappybird.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class FlappyBirdScoreBase(BaseModel):
value: int
key: int


class FlappyBirdScore(FlappyBirdScoreBase):
Expand Down
Loading