Summary
SiYuan improperly validates SQL statements in block query embed blocks, allowing malicious documents to contain non-read-only queries that execute against the application database.
When the document is automatically indexed, rendered, or exported, the embedded statements may run without authentication and could modify or damage stored data.
Affected context
The vulnerability occurs in SiYuan deployments using versions before v3.8.4 when a document contains block query embed content that is executed against siyuan.db. Risk increases when untrusted .sy documents can be imported, opened, indexed, rendered, or exported, because these background or document-processing operations may trigger SQL execution without an interactive authorization step.
Exploit preconditions
- Attack surface: Block query embed SQL processing in document-handling workflows
- Required access: Ability to supply, import, or otherwise cause SiYuan to process a malicious
.sydocument - Trigger point: Background indexing, rendering, or export of the document
- Why exploitation is possible: SQL statements are not sufficiently restricted to read-only operations before execution
Why this happens
The underlying bug is inadequate validation and authorization of SQL statements embedded in block query content. The application treats document-provided SQL as an allowable query without reliably enforcing a read-only grammar or equivalent database permission boundary, enabling statements that can alter database state. This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command, commonly known as SQL Injection.
Potential impact
- Stored application data may be modified or deleted
- Document and workspace integrity could be compromised
- Indexing, rendering, or export operations could produce altered or misleading results
- Availability may be affected if destructive or resource-intensive statements execute
- Sensitive database information could potentially be accessed, depending on query permissions
General mitigation guidance
Mitigation generally focuses on treating document-embedded SQL as untrusted input and enforcing a read-only execution boundary.
Common defensive patterns include:
- Allowing only a narrowly defined, read-only SQL grammar for query embeds
- Using parameterized query interfaces for values rather than concatenating document content into SQL
- Parsing and validating statements before execution, with explicit rejection of write, schema, administrative, and multi-statement operations
- Executing query embeds through a read-only database account or read-only connection where supported
- Separating document rendering and export privileges from database modification privileges
- Requiring explicit authorization before processing untrusted documents in high-privilege contexts
- Applying resource limits and transaction isolation appropriate to document-driven queries
How teams detect issues like this
Detection approaches may include:
- Static analysis for SQL construction from document content and execution paths lacking parameterization or allowlists
- Code review checks for query execution during indexing, rendering, preview, and export workflows
- Tests that submit non-read-only statements and confirm they are rejected before reaching the database
- Database auditing for unexpected write, schema, or administrative statements from document-processing components
- Policy enforcement requiring read-only credentials for embedded-query features
- Automated security review tools configured to flag SQL execution involving untrusted document fields

