-
-
Notifications
You must be signed in to change notification settings - Fork 53
Feature/tasks architectural refactoring #283
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
Open
MOHITKOURAV01
wants to merge
8
commits into
openml:main
Choose a base branch
from
MOHITKOURAV01:feature/tasks-architectural-refactoring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−19
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8dc0b56
security: implement SQL parameterization and table whitelisting in da…
MOHITKOURAV01 83abbf5
Architectural refactoring: Move list_datasets logic to database layer…
MOHITKOURAV01 193a0dd
Address review feedback: Hoist allowed_tables, refactor quality_claus…
MOHITKOURAV01 567f15c
Fix SQLAlchemy ArgumentError: conditionally bind data_ids
MOHITKOURAV01 7d8d391
Architectural refactoring: Move task template lookup logic to databas…
MOHITKOURAV01 2054eb7
Address final review feedback: implement PK mapping and validation gu…
MOHITKOURAV01 db9b7fb
Address final CodeRabbit review: fix list_datasets signature, expansi…
MOHITKOURAV01 778faa8
Revert dataset list sanitation and keep lookup table checks as reques…
MOHITKOURAV01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -3,8 +3,8 @@ | |||
| from typing import Annotated, cast | ||||
|
|
||||
| import xmltodict | ||||
| from fastapi import APIRouter, Depends | ||||
| from sqlalchemy import RowMapping, text | ||||
| from fastapi import APIRouter, Depends, HTTPException | ||||
| from sqlalchemy import RowMapping | ||||
| from sqlalchemy.ext.asyncio import AsyncConnection | ||||
|
|
||||
| import config | ||||
|
|
@@ -17,6 +17,7 @@ | |||
| router = APIRouter(prefix="/tasks", tags=["tasks"]) | ||||
|
|
||||
| type JSON = dict[str, "JSON"] | list["JSON"] | str | int | float | bool | None | ||||
| ALLOWED_LOOKUP_TABLES = {"estimation_procedure", "evaluation_measure", "task_type", "dataset"} | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This variable does not seem to be used. |
||||
|
|
||||
|
|
||||
| def convert_template_xml_to_json(xml_template: str) -> dict[str, JSON]: | ||||
|
|
@@ -95,7 +96,7 @@ async def fill_template( | |||
| ) | ||||
|
|
||||
|
|
||||
| async def _fill_json_template( # noqa: C901 | ||||
| async def _fill_json_template( # noqa: C901, PLR0912 | ||||
| template: JSON, | ||||
| task: RowMapping, | ||||
| task_inputs: dict[str, str | int], | ||||
|
|
@@ -128,23 +129,29 @@ async def _fill_json_template( # noqa: C901 | |||
| (field,) = match.groups() | ||||
| if field not in fetched_data: | ||||
| table, _ = field.split(".") | ||||
| result = await connection.execute( | ||||
| text( | ||||
| f""" | ||||
| SELECT * | ||||
| FROM {table} | ||||
| WHERE `id` = :id_ | ||||
| """, # noqa: S608 | ||||
| ), | ||||
| # Not sure how parametrize table names, as the parametrization adds | ||||
| # quotes which is not legal. | ||||
| parameters={"id_": int(task_inputs[table])}, | ||||
| ) | ||||
| rows = result.mappings() | ||||
| row_data = next(rows, None) | ||||
| # List of tables allowed for [LOOKUP:table.column] directive. | ||||
| # This is a security measure to prevent SQL injection via table names. | ||||
| if table not in task_inputs or not task_inputs[table]: | ||||
| msg = f"Missing or empty input for lookup table: {table}" | ||||
| raise HTTPException(status_code=400, detail=msg) | ||||
|
|
||||
| try: | ||||
| id_val = int(task_inputs[table]) | ||||
| except ValueError: | ||||
| msg = f"Invalid integer id for table {table}: {task_inputs[table]}" | ||||
| raise HTTPException(status_code=400, detail=msg) from None | ||||
|
|
||||
| try: | ||||
| row_data = await database.tasks.get_lookup_data( | ||||
| table=table, | ||||
| id_=id_val, | ||||
| expdb=connection, | ||||
| ) | ||||
| except ValueError as e: | ||||
| raise HTTPException(status_code=400, detail=str(e)) from e | ||||
| if row_data is None: | ||||
| msg = f"No data found for table {table} with id {task_inputs[table]}" | ||||
| raise ValueError(msg) | ||||
| raise HTTPException(status_code=400, detail=msg) | ||||
| for column, value in row_data.items(): | ||||
| fetched_data[f"{table}.{column}"] = value | ||||
| if match.string == template: | ||||
|
|
||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you determine which tables to allow? It's been a little while since I've looked at this code so correct me if I am wrong, but based on the docstring of
fill_templateit seems thatLOOKUPstatements always must reference a table that is ainputintask_inputs.