Skip to content

Commit 2977783

Browse files
Merge pull request #36 from recursivezero/feature/RTY-260032
update meta tag
2 parents aab00b2 + c762b56 commit 2977783

File tree

12 files changed

+153
-102
lines changed

12 files changed

+153
-102
lines changed

.github/workflows/project-assign.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ run-name: Assign project under issue and pull requests
44
on:
55
issues:
66
types: [opened]
7-
pull_request:
7+
pull_request_target:
88
types: [opened, reopened]
99

1010
permissions:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __pycache__/
55

66
.venv*
77
venv/
8+
.venv-dist/
89
env/
910
.venv*
1011

app/main.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@
33
from pathlib import Path
44
import logging
55
import asyncio
6-
76
from fastapi import FastAPI, Request
8-
97
from fastapi.responses import JSONResponse
108
from fastapi.staticfiles import StaticFiles
119
from starlette.middleware.sessions import SessionMiddleware
12-
13-
# from fastapi.exceptions import RequestValidationError
14-
# from starlette.exceptions import HTTPException as StarletteHTTPException
1510
from fastapi.exceptions import HTTPException as FastAPIHTTPException
1611
from fastapi.templating import Jinja2Templates
17-
1812
from app.routes import ui_router
1913
from app.utils import db
2014
from app.utils.cache import cleanup_expired
@@ -115,26 +109,6 @@ async def lifespan(app: FastAPI):
115109
name="qr",
116110
)
117111

118-
# -----------------------------
119-
# Global error handler
120-
# -----------------------------
121-
# @app.exception_handler(Exception)
122-
# async def global_exception_handler(request: Request, exc: Exception):
123-
# traceback.print_exc()
124-
# return JSONResponse(
125-
# status_code=500,
126-
# content={"success": False, "error": "INTERNAL_SERVER_ERROR"},
127-
# )
128-
129-
130-
# @app.exception_handler(404)
131-
# async def custom_404_handler(request: Request, exc):
132-
# return templates.TemplateResponse(
133-
# "404.html",
134-
# {"request": request},
135-
# status_code=404,
136-
# )
137-
138112

139113
@app.exception_handler(FastAPIHTTPException)
140114
async def http_exception_handler(request: Request, exc: FastAPIHTTPException):

app/routes.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@
3434
remove_cache_key,
3535
rev_cache,
3636
)
37-
from app.utils.config import DOMAIN, MAX_RECENT_URLS, CACHE_PURGE_TOKEN, QR_DIR
38-
from app.utils.helper import generate_code, is_valid_url, sanitize_url, format_date
37+
from app.utils.config import (
38+
DOMAIN,
39+
MAX_RECENT_URLS,
40+
CACHE_PURGE_TOKEN,
41+
QR_DIR,
42+
)
43+
from app.utils.helper import (
44+
generate_code,
45+
sanitize_url,
46+
is_valid_url,
47+
authorize_url,
48+
format_date,
49+
)
3950
from app.utils.qr import generate_qr_with_logo
4051

41-
# templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
4252
templates = Jinja2Templates(directory="app/templates")
4353
# Routers
4454
ui_router = APIRouter()
@@ -98,12 +108,18 @@ async def create_short_url(
98108
qr_type: str = Form("short"),
99109
):
100110
session = request.session
101-
original_url = sanitize_url(original_url)
111+
original_url = sanitize_url(original_url) # sanitize the URL input
102112

103-
if not original_url or not is_valid_url(original_url):
113+
if not original_url or not is_valid_url(original_url): # validate the URL
104114
session["error"] = "Please enter a valid URL."
105115
return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)
106116

117+
if not authorize_url(
118+
original_url
119+
): # authorize the URL based on whitelist/blacklist
120+
session["error"] = "This domain is not allowed."
121+
return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)
122+
107123
short_code: Optional[str] = get_short_from_cache(original_url)
108124

109125
if not short_code and db.is_connected():
@@ -219,7 +235,6 @@ def redirect_short_ui(short_code: str, background_tasks: BackgroundTasks):
219235
set_cache_pair(short_code, original_url)
220236
return RedirectResponse(original_url)
221237

222-
# return PlainTextResponse("Invalid short URL", status_code=404)
223238
raise HTTPException(status_code=404, detail="Page not found")
224239

225240

@@ -331,9 +346,13 @@ class ShortenRequest(BaseModel):
331346
@api_v1.post("/shorten")
332347
def shorten_api(payload: ShortenRequest):
333348
original_url = sanitize_url(payload.url)
349+
334350
if not is_valid_url(original_url):
335351
return JSONResponse(status_code=400, content={"error": "INVALID_URL"})
336352

353+
if not authorize_url(original_url):
354+
return JSONResponse(status_code=400, content={"error": "DOMAIN_NOT_ALLOWED"})
355+
337356
short_code = get_short_from_cache(original_url)
338357
if not short_code:
339358
short_code = generate_code()

0 commit comments

Comments
 (0)