Summary
SiYuan contains a second-order SQL injection vulnerability in attribute-view template columns that expose the queryBlocks function. When a crafted document or package is imported and rendered, raw SQL may be executed on the victim's kernel, potentially allowing unauthorized reading or modification of notebook data.
Affected context
The issue can arise in SiYuan deployments that support attribute-view template columns and execute the queryBlocks function while rendering imported content. Risk is higher when users import documents or packages from untrusted sources and the rendering process reaches the affected template logic.
Exploit preconditions
- Attack surface: Imported SiYuan documents or packages containing attribute-view template columns
- Required access: The victim must import the crafted content into a SiYuan instance
- Trigger point: Rendering or processing the template column after import
- Why exploitation is possible: The queryBlocks function constructs SQL through string substitution rather than parameterized query handling, allowing attacker-controlled template data to influence database queries
Why this happens
The vulnerability is caused by constructing SQL statements through direct string substitution of attacker-influenced values. Because the resulting query is executed without reliable parameter binding or equivalent input separation, crafted second-order data can become SQL syntax when the document is later rendered. This behavior maps to CWE-89, Improper Neutralization of Special Elements used in an SQL Command.
Potential impact
- An attacker may read notebook or application database contents
- An attacker could modify, corrupt, or delete stored notebook data
- Sensitive information may be exposed to an importing user or an external party
- Application integrity and trust in shared document packages could be reduced
- Follow-on compromise could occur if database contents influence other application behavior
General mitigation guidance
Mitigation generally focuses on separating SQL code from all document- or
template-controlled values.
Common defensive patterns include:
- Using parameterized queries or prepared statements for every dynamic value
- Avoiding SQL construction through string concatenation or substitution
- Applying strict allowlists for selectable fields, operators, and query modes
- Validating template structures before storage and again before execution
- Treating imported documents and packages as untrusted input
- Restricting database permissions to only the operations required by the feature
- Isolating application data stores and maintaining recoverable backups
How teams detect issues like this
Detection approaches can include:
- Static analysis for SQL strings assembled with concatenation, interpolation, or substitution
- Code review checks around queryBlocks and other template-exposed database functions
- Tests using hostile metacharacters and unexpected query values to verify parameter binding
- Runtime logging of query construction failures, anomalous database access, and unusual write activity
- Policy enforcement requiring prepared statements for application database access
- Automated security review tools that flag unparameterized SQL execution paths

