Summary
A SQL injection vulnerability in the DHIS2 SqlView API allows authenticated users to inject arbitrary SQL via the filter parameter of /api/sqlViews/{viewId}/data.json.
When crafted expressions are executed by PostgreSQL, their results are reflected in application error messages, enabling error-based extraction of database contents under certain access conditions.
Affected context
This vulnerability arises in DHIS2 deployments that expose the SqlView functionality to authenticated users and allow them to supply dynamic filter expressions.
The issue manifests specifically in the /api/sqlViews/{viewId}/data.json endpoint, where user-controlled filter values are incorporated into SQL executed against a PostgreSQL backend without robust parameterization or sanitization.
Risk is higher in environments where SqlViews reference sensitive tables or where role-based access controls do not strictly segregate reporting views from core operational data.
Exploit preconditions
- Attack surface: HTTP requests to the DHIS2 REST API, targeting
/api/sqlViews/{viewId}/data.json. - Required access: Authenticated DHIS2 account with permission to access at least one SqlView and invoke the SqlView data endpoint.
- Trigger point: Submission of a crafted
filterparameter that is concatenated or interpolated into a SQL expression evaluated by PostgreSQL. - Why exploitation is possible: The
filterinput is treated as part of an SQL clause without consistent use of prepared statements or strict validation, allowing attackers to alter the query structure and surface database values through error messages (error-based SQL injection).
Why this happens
The underlying bug is a classic SQL injection pattern where user-supplied data is embedded directly into SQL queries executed by the application.
In this case, the DHIS2 SqlView API uses the filter parameter as part of a dynamically built SQL expression, relying on database evaluation rather than safely parameterizing inputs.
This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), in which an input channel meant for filtering or search logic instead permits manipulation of query syntax and retrieval of unintended data.
Abstractly, any design that allows end-users to inject raw SQL fragments or expressions into reporting or view definitions without strict constraints creates a latent injection channel whenever those fragments are passed to the database engine.
Potential impact
- Authenticated users can extract arbitrary database content accessible through the database account used by the application.
- Sensitive health, demographic, or operational data may be disclosed beyond intended access boundaries.
- Attackers could enumerate database schema elements (tables, columns, types) via crafted error messages.
- Misuse of SQL functions could enable limited data modification, depending on query context and database permissions.
- Compliance violations and privacy breaches can occur if regulated datasets are exposed through exploited SqlViews.
General mitigation guidance
Effective mitigation focuses on eliminating dynamic SQL construction from user-controlled inputs and tightening data access boundaries.
Recommended patterns include:
- Using prepared statements and parameterized queries for all filter and search parameters, avoiding direct concatenation or interpolation of input into SQL.
- Constraining SqlView definitions to static queries, where end-user-provided parameters are treated strictly as values, not SQL fragments or expressions.
- Introducing server-side validation layers that enforce strict schemas and whitelists for any query-related parameters (e.g., allowed operators, fields, and value formats).
- Limiting the database privileges of the DHIS2 application account so that even if a query is manipulated, it cannot access tables or views beyond what is necessary.
- Applying least-privilege and data segmentation for SqlViews to ensure they only expose curated, non-sensitive datasets to standard roles.
- Designing error handling routines that avoid echoing raw database error details or query fragments back to clients, thus reducing information leakage channels.
How teams detect issues like this
Organizations can detect and monitor SQL injection risks in similar APIs through a combination of static and dynamic techniques:
- Static analysis: Configure rules to flag string-based query construction, concatenation of HTTP parameters into SQL, and any use of APIs that execute raw SQL built from user inputs. Target calls around SqlView processing and filter handling.
- Code review: During manual review, pay attention to reporting and view endpoints that offer flexible filtering. Look for patterns where user parameters influence query clauses (WHERE, ORDER BY, LIMIT) without clear parameter binding or validation.
- Security testing: Employ application security testing tools or custom scripts to send malformed or intentionally broken
filtervalues and observe whether database errors or unexpected data appear in responses, indicating potential injection. - Runtime monitoring: Use logging and monitoring to detect anomalous SQL patterns originating from application queries, such as unusual functions, union/select patterns, or access to non-standard tables from SqlView queries.
- Policy enforcement: Implement internal guidelines that prohibit user-configurable SQL fragments in feature designs, and integrate automated checks in CI/CD pipelines to block introduction of new dynamic SQL patterns.

