Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking int fields for params and limiting retrieval amount #1089

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from fastapi.responses import ORJSONResponse
from fastapi import Depends, Request, HTTPException
from fastapi.security import APIKeyHeader
from fastapi.exceptions import RequestValidationError
import numpy as np
import scipy.stats
from pydantic import BaseModel
Expand Down Expand Up @@ -192,6 +193,8 @@ def get_timeline_query(user, uri, filename, machine_id, branch, metrics, phase,
if branch is None or branch.strip() == '':
branch = 'main'

check_int_field_api(machine_id, 'machine_id', 1024) # can cause exception
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: The comment 'can cause exception' is redundant since the function name and its usage clearly indicate exception handling


params = [user.is_super_user(), user.visible_users(), uri, filename, branch, machine_id, f"%{phase}"]

metrics_condition = ''
Expand Down Expand Up @@ -776,3 +779,15 @@ def get_connecting_ip(request):
return connecting_ip.split(",")[0]

return request.client.host

def check_int_field_api(field, name, max_value):
if not isinstance(field, int):
raise RequestValidationError(f'{name} must be integer')

if field <= 0:
raise RequestValidationError(f'{name} must be > 0')

if field > max_value:
raise RequestValidationError(f'{name} must be <= {max_value}')

return True
16 changes: 8 additions & 8 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
html_escape_multi, get_phase_stats, get_phase_stats_object,
is_valid_uuid, convert_value, get_timeline_query,
get_run_info, get_machine_list, get_artifact, store_artifact,
authenticate)
authenticate, check_int_field_api)

from lib.global_config import GlobalConfig
from lib.db import DB
Expand Down Expand Up @@ -179,7 +179,7 @@ async def get_jobs(
machine_id_condition = ''
state_condition = ''

if machine_id is not None:
if machine_id and check_int_field_api(machine_id, 'machine_id', 1024):
machine_id_condition = 'AND j.machine_id = %s'
params.append(machine_id)

Expand Down Expand Up @@ -276,7 +276,7 @@ async def get_repositories(uri: str | None = None, branch: str | None = None, ma
query = f"{query} AND r.filename LIKE %s \n"
params.append(f"%{filename}%")

if machine_id:
if machine_id and check_int_field_api(machine_id, 'machine_id', 1024):
query = f"{query} AND m.id = %s \n"
params.append(machine_id)

Expand All @@ -302,7 +302,7 @@ async def get_repositories(uri: str | None = None, branch: str | None = None, ma

# A route to return all of the available entries in our catalog.
@app.get('/v1/runs')
async def get_runs(uri: str | None = None, branch: str | None = None, machine_id: int | None = None, machine: str | None = None, filename: str | None = None, limit: int | None = None, uri_mode = 'none', user: User = Depends(authenticate)):
async def get_runs(uri: str | None = None, branch: str | None = None, machine_id: int | None = None, machine: str | None = None, filename: str | None = None, limit: int = 5, uri_mode = 'none', user: User = Depends(authenticate)):

query = '''
SELECT r.id, r.name, r.uri, r.branch, r.created_at, r.invalid_run, r.filename, m.description, r.commit_hash, r.end_measurement, r.failed, r.machine_id
Expand All @@ -329,7 +329,7 @@ async def get_runs(uri: str | None = None, branch: str | None = None, machine_id
query = f"{query} AND r.filename LIKE %s \n"
params.append(f"%{filename}%")

if machine_id:
if machine_id and check_int_field_api(machine_id, 'machine_id', 1024):
query = f"{query} AND m.id = %s \n"
params.append(machine_id)

Expand All @@ -339,9 +339,9 @@ async def get_runs(uri: str | None = None, branch: str | None = None, machine_id

query = f"{query} ORDER BY r.created_at DESC"

if limit:
query = f"{query} LIMIT %s"
params.append(limit)
check_int_field_api(limit, 'limit', 50)
query = f"{query} LIMIT %s"
params.append(limit)


data = DB().fetch_all(query, params=params)
Expand Down