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 4 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: 15 additions & 2 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,16 @@
)
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 @@ -95,25 +106,27 @@ async def create_flappybird_score(
id=score_id,
user_id=user.id,
value=flappybird_score.value,
key=flappybird_score.key,
creation_time=creation_time,
)
db_flappybird_best_score = models_flappybird.FlappyBirdBestScore(
id=score_id,
user_id=user.id,
value=flappybird_score.value,
key=flappybird_score.key,
creation_time=creation_time,
)
personal_best = await cruds_flappybird.get_flappybird_personal_best_by_user_id(
user_id=user.id,
db=db,
)
if personal_best is None:
if personal_best is None and genKey(flappybird_score.value) == flappybird_score.key:
await cruds_flappybird.create_flappybird_best_score(
flappybird_best_score=db_flappybird_best_score,
db=db,
)
else:
if personal_best.value < flappybird_score.value:
if personal_best.value < flappybird_score.value and genKey(flappybird_score.value) == flappybird_score.key:
await cruds_flappybird.update_flappybird_best_score(
user_id=user.id,
best_score=flappybird_score.value,
Expand Down
2 changes: 2 additions & 0 deletions app/modules/flappybird/models_flappybird.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FlappyBirdScore(Base):
user_id: Mapped[str] = mapped_column(ForeignKey("core_user.id"))
user: Mapped[CoreUser] = relationship("CoreUser", init=False)
value: Mapped[int]
key: Mapped[int]
julien4215 marked this conversation as resolved.
Show resolved Hide resolved
creation_time: Mapped[datetime]


Expand All @@ -24,4 +25,5 @@ class FlappyBirdBestScore(Base):
user_id: Mapped[str] = mapped_column(ForeignKey("core_user.id"))
user: Mapped[CoreUser] = relationship("CoreUser", init=False)
value: Mapped[int]
key: Mapped[int]
creation_time: Mapped[datetime]
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