From c700518a15375f7bb6b70b01e7c0c57ee8473063 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 23 Dec 2023 00:40:01 +0530 Subject: [PATCH 1/8] limited upload size --- src/paste/main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/paste/main.py b/src/paste/main.py index 2437515..9aa3291 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -2,6 +2,7 @@ from fastapi.responses import PlainTextResponse, HTMLResponse, RedirectResponse import shutil import os +import sys from pathlib import Path from fastapi import FastAPI from fastapi.templating import Jinja2Templates @@ -26,9 +27,16 @@ templates = Jinja2Templates(directory=str(Path(BASE_DIR, 'templates'))) +MAX_UPLOAD_SIZE = 20_000_000 # 20 MB @app.post("/file") def post_as_a_file(file: UploadFile = File(...)): + if file.content_type != "text/plain": + raise HTTPException(detail="Only text/plain is supported", + status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE) + if file.filesize > MAX_UPLOAD_SIZE: + raise HTTPException(detail="File size is too large", + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) try: uuid = generate_uuid() if uuid in large_uuid_storage: @@ -37,7 +45,6 @@ def post_as_a_file(file: UploadFile = File(...)): with open(path, 'wb') as f: shutil.copyfileobj(file.file, f) large_uuid_storage.append(uuid) - print(large_uuid_storage) except Exception: # return {"message": "There was an error uploading the file"} raise HTTPException(detail="There was an error uploading the file", @@ -51,9 +58,13 @@ def post_as_a_file(file: UploadFile = File(...)): @app.get("/paste/{uuid}") def post_as_a_text(uuid): path = f"data/{uuid}" + text = "" try: with open(path, 'rb') as f: - return PlainTextResponse(f.read()) + text = f.read() + if sys.getsizeof(text) > MAX_UPLOAD_SIZE: + raise HTTPException(detail="File size is too large", + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) except Exception as e: print(e) raise HTTPException(detail="404: The Requested Resource is not found", From e4bcc2d3928e431027f450e90e3e0de20ef86b87 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sat, 23 Dec 2023 04:14:53 +0530 Subject: [PATCH 2/8] Update .gitignore --- .gitignore | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2dc53ca..06109db 100644 --- a/.gitignore +++ b/.gitignore @@ -151,10 +151,3 @@ dmypy.json # Cython debug symbols cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -.idea/ From d163bcafb8111e98add3f59979818f4d55c9f864 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Dec 2023 22:01:24 +0530 Subject: [PATCH 3/8] minor fix in post_as_a_text Co-authored-by: A91y --- pre-commit-config.yaml | 10 ++++++++++ src/paste/main.py | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 pre-commit-config.yaml diff --git a/pre-commit-config.yaml b/pre-commit-config.yaml new file mode 100644 index 0000000..6f4b854 --- /dev/null +++ b/pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.9 + hooks: + - id: ruff + args: + - --fix + - id: ruff-format +ci: + autoupdate_schedule: weekly diff --git a/src/paste/main.py b/src/paste/main.py index 11697ac..aa8274a 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -65,6 +65,8 @@ def post_as_a_text(uuid): if sys.getsizeof(text) > MAX_UPLOAD_SIZE: raise HTTPException(detail="File size is too large", status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) + else: + return PlainTextResponse(text) except Exception as e: print(e) raise HTTPException(detail="404: The Requested Resource is not found", From 75eb3c889f908e759ab5e4979a435817ac9b7fc0 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Dec 2023 22:26:26 +0530 Subject: [PATCH 4/8] review patch --- .gitignore | 9 +++++++++ data/test | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) delete mode 100644 data/test diff --git a/.gitignore b/.gitignore index 06109db..d47fa0e 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ +data/ # Translations *.mo @@ -151,3 +152,11 @@ dmypy.json # Cython debug symbols cython_debug/ + + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ \ No newline at end of file diff --git a/data/test b/data/test deleted file mode 100644 index af27ff4..0000000 --- a/data/test +++ /dev/null @@ -1 +0,0 @@ -This is a test file. \ No newline at end of file From f9966145f970bf485b41f103e668c63c7e3c5401 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Dec 2023 22:39:27 +0530 Subject: [PATCH 5/8] conflicts prevented Co-authored-by: Kanishk Pachauri --- src/paste/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/paste/main.py b/src/paste/main.py index 177e830..c4eb327 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -36,7 +36,8 @@ MAX_UPLOAD_SIZE = 20_000_000 # 20 MB @app.post("/file") -def post_as_a_file(file: UploadFile = File(...)): +@limiter.limit("100/minute") +async def post_as_a_file(file: UploadFile = File(...)): if file.content_type != "text/plain": raise HTTPException(detail="Only text/plain is supported", status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE) From b8a8881aabb35c7cde717af2f58c2093ee35f528 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Dec 2023 22:39:27 +0530 Subject: [PATCH 6/8] conflicts prevented Co-authored-by: Kanishk Pachauri --- .gitignore | 1 - data/MAQR | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 data/MAQR diff --git a/.gitignore b/.gitignore index d47fa0e..4ed7433 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,6 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ -data/ # Translations *.mo diff --git a/data/MAQR b/data/MAQR new file mode 100644 index 0000000..7d957d0 --- /dev/null +++ b/data/MAQR @@ -0,0 +1 @@ +This is a test data \ No newline at end of file From a6ca93db374861ff4c880bb0792a54e746e14ed7 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Dec 2023 22:39:27 +0530 Subject: [PATCH 7/8] conflicts prevented Co-authored-by: Kanishk Pachauri --- .gitignore | 2 +- data/MAQR | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 data/MAQR diff --git a/.gitignore b/.gitignore index d47fa0e..7d490f1 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ -data/ +/data # Translations *.mo diff --git a/data/MAQR b/data/MAQR new file mode 100644 index 0000000..7d957d0 --- /dev/null +++ b/data/MAQR @@ -0,0 +1 @@ +This is a test data \ No newline at end of file From 6a142076e13a315c0b24f8852785fd2b96f62580 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 28 Dec 2023 23:06:02 +0530 Subject: [PATCH 8/8] resolved --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4ed7433..7d490f1 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ +/data # Translations *.mo