Skip to content

Commit 48a970f

Browse files
[RTY-260032]: merge sanitize url logic in valid check time
1 parent fb3ea43 commit 48a970f

3 files changed

Lines changed: 4 additions & 33 deletions

File tree

app/main.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,6 @@ async def lifespan(app: FastAPI):
115115
name="qr",
116116
)
117117

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-
138118

139119
@app.exception_handler(FastAPIHTTPException)
140120
async def http_exception_handler(request: Request, exc: FastAPIHTTPException):

app/routes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
rev_cache,
3636
)
3737
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
38+
from app.utils.helper import generate_code, is_valid_url, format_date
3939
from app.utils.qr import generate_qr_with_logo
4040

4141
# templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
@@ -98,7 +98,6 @@ async def create_short_url(
9898
qr_type: str = Form("short"),
9999
):
100100
session = request.session
101-
original_url = sanitize_url(original_url)
102101

103102
if not original_url or not is_valid_url(original_url):
104103
session["error"] = "Please enter a valid URL."
@@ -330,7 +329,7 @@ class ShortenRequest(BaseModel):
330329

331330
@api_v1.post("/shorten")
332331
def shorten_api(payload: ShortenRequest):
333-
original_url = sanitize_url(payload.url)
332+
original_url = payload.url
334333
if not is_valid_url(original_url):
335334
return JSONResponse(status_code=400, content={"error": "INVALID_URL"})
336335

app/utils/helper.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010

1111
def is_valid_url(url: str) -> bool:
12+
url = url.strip() # sanitize here
13+
1214
try:
1315
parsed = urlparse(url)
1416

@@ -26,10 +28,6 @@ def is_valid_url(url: str) -> bool:
2628
if hostname is None:
2729
return False
2830

29-
# Block localhost
30-
if hostname == "localhost":
31-
return False
32-
3331
# Block private / loopback IPs
3432
try:
3533
ip = ipaddress.ip_address(hostname)
@@ -45,12 +43,6 @@ def is_valid_url(url: str) -> bool:
4543
return False
4644

4745

48-
def sanitize_url(url: str) -> str:
49-
url = url.strip()
50-
51-
return url
52-
53-
5446
def generate_code(length: int = SHORT_CODE_LENGTH) -> str:
5547
chars = string.ascii_letters + string.digits
5648
return "".join(random.choice(chars) for _ in range(length))

0 commit comments

Comments
 (0)