Skip to content

Commit eeaf52a

Browse files
committed
validate input not null and sanitize input
1 parent bfeeab7 commit eeaf52a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

app/services/accident.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from sqlalchemy.future import select
44
from fastapi import HTTPException
55

6-
from ..models.accident import DeAccidentMeta
76
from ..utils.sanitizer import sanitize_string
7+
from ..models.accident import DeAccidentMeta
88

99

1010

app/services/administrative.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from sqlalchemy.sql import text
22
from sqlalchemy.ext.asyncio import AsyncSession
3+
from fastapi import HTTPException
4+
5+
from ..utils.validators import validate_not_none
6+
from ..utils.sanitizer import sanitize_string
37

48

59

@@ -35,6 +39,12 @@ async def get_parcel_meta_by_lat_lng(session: AsyncSession, lat: float, lng: flo
3539

3640

3741
async def get_municipality_by_key(session: AsyncSession, key: str):
42+
try:
43+
validated_key = validate_not_none(key)
44+
validated_key = sanitize_string(validated_key)
45+
except ValueError as e:
46+
raise HTTPException(status_code=400, detail=str(e))
47+
3848
stmt = text('''
3949
SELECT
4050
mk.municipality_key AS municipality_key,
@@ -68,7 +78,7 @@ async def get_municipality_by_key(session: AsyncSession, key: str):
6878
LOWER(mk.municipality_key) = :key
6979
''')
7080

71-
sql = stmt.bindparams(key=key.lower())
81+
sql = stmt.bindparams(key=validated_key.lower())
7282
result = await session.execute(sql)
7383

7484
return result.mappings().all()
@@ -109,6 +119,12 @@ async def get_municipality_by_name(session: AsyncSession, name: str):
109119

110120

111121
async def get_municipality_by_query(session: AsyncSession, query: str):
122+
try:
123+
validated_query = validate_not_none(query)
124+
sanitized_query = sanitize_string(validated_query)
125+
except ValueError as e:
126+
raise HTTPException(status_code=400, detail=str(e))
127+
112128
stmt = text('''
113129
SELECT
114130
gem.ags AS municipality_key,
@@ -145,7 +161,7 @@ async def get_municipality_by_query(session: AsyncSession, query: str):
145161
LIMIT 10
146162
''')
147163

148-
sql = stmt.bindparams(q=query.lower())
164+
sql = stmt.bindparams(q=sanitized_query.lower())
149165
result = await session.execute(sql)
150166
rows = result.mappings().all()
151167

0 commit comments

Comments
 (0)