DEV Community

Ekin Yegül
Ekin Yegül

Posted on Originally published at cryptopust.com

Remediating SQL injection without taking production offline

In a system I worked on, an SQL injection vulnerability appeared in production that we could not reproduce in test or pre-production. The application supported multiple database systems. Differences in versions, configuration, live data and traffic exposed behaviour our test environments had missed. We also considered the possible contribution of third-party components; I cannot present that as a confirmed sole root cause.

We needed to close the vulnerability while preserving service as far as possible and maintaining data integrity. In my original article, I reported completing the work without downtime or data loss. That is the result I reported for this case. I have no shareable measurement window, transaction counts or independent verification records, so I do not extrapolate a universal zero-downtime guarantee from it.

The vulnerable query path

SQL injection occurs when untrusted input becomes part of the SQL statement's structure. Database versions and load do not create injection by themselves, but can expose an unsafe query construction path that tests missed.

Parameterized queries, as recommended by OWASP, are a fundamental defence. Where dynamic table or column names are necessary, the application should define the permitted values. I would not use user input directly as an SQL identifier.

Illustrative C# data access code:

await using var command = connection.CreateCommand();
command.CommandText = "SELECT id FROM customers WHERE external_id = @id";
var parameter = command.CreateParameter();
parameter.ParameterName = "@id";
parameter.Value = externalId;
command.Parameters.Add(parameter);
Enter fullscreen mode Exit fullscreen mode

This code is not from the incident. With multiple data providers, test parameter syntax and driver behaviour in each supported environment. A WAF and more restrictive database permissions may reduce the impact. The query construction defect still needs to be fixed.

Rollback during a security patch

We prepared a patched version separately and directed traffic to it gradually. Canary and blue-green deployments allow traffic to be moved away from a problematic release. But if the old release is vulnerable, rolling back can reopen the vulnerability. Plan compensating measures such as temporary restrictions, disabling the affected function or a safe intermediate release.

During deployment I would monitor business outcomes alongside error rates and latency. An HTTP 200 response can coexist with an incorrect financial entry. Do the old and new versions enforce the same business rule? Do retries create duplicate records? Can we trace records processed during the update?

I previously described using backward-compatible migrations, feature flags and idempotency controls in this transition. When versions must coexist, adding the new schema, migrating data, switching reads and writes, then removing the old structure only after confirming it is unused gives a more controlled migration.

Checking whether a transaction ID exists before inserting a record may not be sufficient. Two concurrent transactions can both pass the check. Consider uniqueness constraints and transaction boundaries together. A local rollback also cannot undo an instruction already sent to an external provider.

Bringing the production failure into tests

After the incident we invested in closer environment parity, realistic tests with masked data, SAST, DAST, IAST and continuous monitoring. Their effectiveness needs to be measured across supported database versions and configurations. Moving production data into test environments requires its own security and privacy controls.

In the acceptance record for the same work today, I would expect the vulnerable query path, a test that fails on the old release and passes on the new one, canary metrics, compared records and safeguards against reopening the vulnerability during rollback.

This was a joint effort involving application, infrastructure and security teams and business partners. A substantial part of technical responsibility is ensuring those people can obtain the evidence and decision authority they need at the right time.

Originally published: 2026-07-03. Revised edition: 3 December 2026.

Sources


Originally published on CryptoPust. This article was prepared with AI assistance; please review the disclosure setting before publishing.

Top comments (0)