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

fix: changed query to accept user input in prepared sql statement #2652

Merged
merged 5 commits into from
Nov 4, 2022
Merged
Changes from 4 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
19 changes: 8 additions & 11 deletions warehouse/warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,7 @@ func pendingEventsHandler(w http.ResponseWriter, r *http.Request) {
}

func getPendingStagingFileCount(sourceOrDestId string, isSourceId bool) (fileCount int64, err error) {
sourceOrDestId = pq.QuoteIdentifier(sourceOrDestId)
sourceOrDestColumn := ""
if isSourceId {
sourceOrDestColumn = "source_id"
Expand All @@ -1807,16 +1808,14 @@ func getPendingStagingFileCount(sourceOrDestId string, isSourceId bool) (fileCou
FROM
%[1]s
WHERE
%[1]s.%[3]s = '%[2]s';
%[2]s = $1;
`,
warehouseutils.WarehouseUploadsTable,
sourceOrDestId,
sourceOrDestColumn,
Comment on lines 1813 to 1814
Copy link
Member

Choose a reason for hiding this comment

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

Ideally, we need to use pq.QuoteIdentifier for table/column names as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done for the said variable. Doing it for the whole code will take time. Should we do it after as soon as get some bandwidth? Wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's discuss further refactoring and prioritise accordingly.

)

err = dbHandle.QueryRow(sqlStatement).Scan(&lastStagingFileIDRes)
err = dbHandle.QueryRow(sqlStatement, sourceOrDestId).Scan(&lastStagingFileIDRes)
if err != nil && err != sql.ErrNoRows {
err = fmt.Errorf("query: %s failed with Error : %w", sqlStatement, err)
err = fmt.Errorf("query: %s run failed with Error : %w", sqlStatement, err)
return
}
lastStagingFileID := int64(0)
Expand All @@ -1830,18 +1829,16 @@ func getPendingStagingFileCount(sourceOrDestId string, isSourceId bool) (fileCou
FROM
%[1]s
WHERE
%[1]s.id > %[2]v
AND %[1]s.%[4]s = '%[3]s';
id > %[2]v
AND %[3]s = $1;
`,
warehouseutils.WarehouseStagingFilesTable,
lastStagingFileID,
sourceOrDestId,
sourceOrDestColumn,
)

err = dbHandle.QueryRow(sqlStatement).Scan(&fileCount)
err = dbHandle.QueryRow(sourceOrDestId, sourceOrDestId).Scan(&fileCount)
achettyiitr marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && err != sql.ErrNoRows {
err = fmt.Errorf("query: %s failed with Error : %w", sqlStatement, err)
err = fmt.Errorf("query: %s run failed with Error : %w", sqlStatement, err)
return
}

Expand Down