Repository navigation
Fix SQL injection in MzMLSqliteHandler::writeRunLevelInformation (#9730) - #9734
Conversation
|
Welcome @aliviahossain and thank you for your contribution to OpenMS! To help us better understand our userbase and contributors, we would appreciate if you could complete the survey at the link below. Completing this survey will help us get to your PR sooner. Thank you for your participation! |
|
@aliviahossain The following names might be missing from the
Please consider applying the following patch: --- AUTHORS 2026-07-13 21:00:39.286196641 +0000
+++ AUTHORS.new 2026-07-13 21:00:44.873539145 +0000
@@ -14,6 +14,7 @@
- Alexandra Scherbart
- Alexandra Zerck
- Alivia Hossain
+ - Alivia Hossain
- Amanda Wein
- Andreas Bertsch
- Andreas Simon |
📝 WalkthroughWalkthrough
ChangesSQLite RUN insertion safety
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/tests/class_tests/openms/source/MzMLSqliteHandler_test.cpp`:
- Around line 508-538: Extend the regression test around
MzMLSqliteHandler::writeExperiment to query the written RUN row after verifying
countTableRows("RUN"). Assert that both FILENAME and NATIVE_ID contain the exact
evil_path value, preserving the existing SQL-injection and single-row checks.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 42b6d469-e026-4901-916e-626640ccd6b2
📒 Files selected for processing (4)
AUTHORSCHANGELOGsrc/openms/source/FORMAT/HANDLERS/MzMLSqliteHandler.cppsrc/tests/class_tests/openms/source/MzMLSqliteHandler_test.cpp
| START_SECTION([EXTRA] writeRunLevelInformation is safe against SQL injection via the loaded file path) | ||
| { | ||
| // Regression test for GH #9730: the loaded file path was concatenated into an | ||
| // INSERT executed via sqlite3_exec, so a path containing a single quote broke | ||
| // the statement and a crafted path could inject arbitrary SQL. | ||
| MSExperiment exp_orig; | ||
| MzMLFile().load(OPENMS_GET_TEST_DATA_PATH("MzMLSqliteHandler_1.mzML"), exp_orig); | ||
|
|
||
| // Leading '/' => setLoadedFilePath keeps it verbatim (treated absolute on all | ||
| // platforms) instead of prepending the CWD. | ||
| const std::string evil_path = "/tmp/o'brien'; DROP TABLE RUN;--/run.mzML"; | ||
| exp_orig.setLoadedFilePath(evil_path); | ||
| TEST_EQUAL(exp_orig.getLoadedFilePath(), evil_path) // sanity: stored verbatim | ||
|
|
||
| std::string tmp_filename; | ||
| NEW_TMP_FILE(tmp_filename); | ||
| std::filesystem::remove(tmp_filename); | ||
|
|
||
| MzMLSqliteHandler handler(tmp_filename, 12345); | ||
| handler.createTables(); | ||
| // With the old concatenating code this throws (malformed SQL) or runs the | ||
| // injected DROP; with the parameterized fix it must simply succeed. | ||
| handler.writeExperiment(exp_orig); | ||
|
|
||
| // The RUN table must still exist and hold exactly the one row we wrote: if the | ||
| // injected "DROP TABLE RUN" had executed, countTableRows() would instead throw. | ||
| SqliteConnector conn(tmp_filename); | ||
| TEST_EQUAL(conn.countTableRows("RUN"), 1) | ||
| } | ||
| END_SECTION | ||
|
|
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Verify the exact path value, not just row survival.
The test would pass if the implementation silently stored an empty or altered path. Query the RUN row and assert that both FILENAME and NATIVE_ID equal evil_path to cover quote handling and data preservation.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/tests/class_tests/openms/source/MzMLSqliteHandler_test.cpp` around lines
508 - 538, Extend the regression test around MzMLSqliteHandler::writeExperiment
to query the written RUN row after verifying countTableRows("RUN"). Assert that
both FILENAME and NATIVE_ID contain the exact evil_path value, preserving the
existing SQL-injection and single-row checks.
|
thanks for your contribution. looks good! |
|
@timosachsenberg Great, please review it and merge it. Also I've discussed about the parquet implementation in the discord group. Meanwhile I'll look into other bugs/features as well. Thanks ! |
Summary
When an MSExperiment is written to sqlite (.sqMass, used by the OpenSWATH tools), the loaded file path was concatenated straight into an INSERT statement and run through sqlite3_exec. A path containing a single quote broke the query, and a crafted path or filename could inject arbitrary SQL. This PR switches that INSERT to a parameterized (bound) statement. The developer had already flagged this with a TODO in the code.
Checklist
Before
The file path was built into the SQL text directly, twice:
const std::string& native_id = exp.getLoadedFilePath(); // TODO escape stuff like ' (SQL inject)
insert_run_sql << "INSERT INTO RUN (ID, FILENAME, NATIVE_ID) VALUES ("
<< run_id_ << ",'" << native_id << "','" << native_id << "'); ";
conn.executeStatement(insert_run_sql.str());
A path like /data/o'brien/run.mzML throws a syntax error, and ', 'x'); DROP TABLE RUN;-- executes the DROP.
After

The path is bound through a placeholder using the existing helper (already used elsewhere in the same file). run_id_ is a UInt64 we generate ourselves, so it stays inline; only the file path, which can come from outside, is bound:
insert_run_sql << "INSERT INTO RUN (ID, FILENAME, NATIVE_ID) VALUES (" << run_id_ << ", ?, ?); ";
std::vectorstd::string data {native_id, native_id};
conn.executeBindStatement(insert_run_sql.str(), data);
Any quote or SQL in the path is now stored as plain data, never executed.
Files changed
Testing
Built and ran the test locally (Windows, MSVC, WITH_GUI=OFF). It fails on the old code with IllegalArgument: near "brien": syntax error and passes after the fix. 100% of the test passes.
Note for reviewers
executeBindStatement binds via sqlite3_bind_blob, so the path lands in the TEXT-declared FILENAME and NATIVE_ID columns with BLOB storage class. This matches the approach suggested in the issue and is harmless for the OpenMS read path (values read back fine via sqlite3_column_text). If you would prefer the columns keep TEXT storage class, a text-binding variant can be added, happy to do that in a follow-up.
Will discuss about Parquet implementations as suggested, in the discord group. Thanks !
Closes #9730
Summary by CodeRabbit
Bug Fixes
Documentation
Chores