Skip to content

Fix SQL injection in MzMLSqliteHandler::writeRunLevelInformation (#9730) - #9734

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
aliviahossain:develop
Jul 14, 2026
Merged

timosachsenberg merged 1 commit into
OpenMS:developfrom
aliviahossain:develop

Conversation

@aliviahossain

@aliviahossain aliviahossain commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

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

  • [ yes] Make sure that you are listed in the AUTHORS file
  • [ yes] Add relevant changes and new features to the CHANGELOG file
  • [ yes] I have commented my code, particularly in hard-to-understand areas
  • [ yes] New and existing unit tests pass locally with my changes
  • [ yes] Updated or added python bindings for changed or new classes (Tick if no updates were necessary.)

Before

Screenshot 2026-07-14 013438

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
Screenshot 2026-07-14 012526

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

  • src/openms/source/FORMAT/HANDLERS/MzMLSqliteHandler.cpp: rewrote the RUN insert in writeRunLevelInformation to use executeBindStatement. No header or API change.
  • src/tests/class_tests/openms/source/MzMLSqliteHandler_test.cpp: added a regression section that writes an experiment whose loaded path contains '... ; DROP TABLE RUN;--, then checks the write succeeds and the RUN table still holds exactly one row (a run DROP would have made the row count query throw).

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

    • Improved SQLite export reliability for file paths containing apostrophes or special characters.
    • Added protection against SQL injection through crafted file path values.
    • Added regression coverage to verify exported run information remains intact.
  • Documentation

    • Updated the changelog with the security and robustness fix.
  • Chores

    • Added a contributor to the authors list.

@github-actions

Copy link
Copy Markdown
Contributor

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!

Survey

@github-actions

Copy link
Copy Markdown
Contributor

@aliviahossain The following names might be missing from the AUTHORS file:

  • aliviahossain

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

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

writeRunLevelInformation() now inserts loaded file paths through bound SQLite parameters. A regression test covers quoted SQL-like paths, and the changelog records the fix. AUTHORS adds Alivia Hossain.

Changes

SQLite RUN insertion safety

Layer / File(s) Summary
Parameterized RUN row write
src/openms/source/FORMAT/HANDLERS/MzMLSqliteHandler.cpp, CHANGELOG
The RUN insertion uses placeholders with SqliteConnector::executeBindStatement, and the changelog documents the change.
Loaded path regression coverage
src/tests/class_tests/openms/source/MzMLSqliteHandler_test.cpp
A crafted loaded file path is written to SQLite, then the test verifies the RUN table remains present with exactly one row.

Suggested reviewers: timosachsenberg, cbielow, poshul, copilot

Poem

A rabbit bound the path with care,
No rogue quotes could wander there.
SQLite rows stayed safe and sound,
One RUN row neatly held its ground.
Hop, hop—fixed! 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The AUTHORS addition is unrelated to the SQL-injection fix and appears outside the issue's scope. Move the AUTHORS update to a separate metadata-only PR unless it is required by project contribution policy.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main change: fixing SQL injection in MzMLSqliteHandler::writeRunLevelInformation for issue #9730.
Linked Issues check ✅ Passed The patch parameterizes the RUN insert, binds the file path, and adds a regression test covering quote and payload input.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 79e97f8 and 8842086.

📒 Files selected for processing (4)
  • AUTHORS
  • CHANGELOG
  • src/openms/source/FORMAT/HANDLERS/MzMLSqliteHandler.cpp
  • src/tests/class_tests/openms/source/MzMLSqliteHandler_test.cpp

Comment on lines +508 to +538
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ 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.

@timosachsenberg

Copy link
Copy Markdown
Contributor

thanks for your contribution. looks good!

@aliviahossain

Copy link
Copy Markdown
Contributor Author

@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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL injection risk in MzMLSqliteHandler::writeRunLevelInformation via unescaped file path

2 participants