File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
140120async def http_exception_handler (request : Request , exc : FastAPIHTTPException ):
Original file line number Diff line number Diff line change 3535 rev_cache ,
3636)
3737from 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
3939from 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" )
332331def 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
Original file line number Diff line number Diff line change 99
1010
1111def 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-
5446def 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 ))
You can’t perform that action at this time.
0 commit comments