siyuan-note/siyuan · error
encrypted box db not opened for box
Error message
encrypted box db not opened for box
What it means
CheckReadonlyStatementInBox prepares the SQL on the notebook-specific database to verify it is read-only. If the boxID refers to an encrypted notebook whose DB handle is not open (GetEncryptedDB returns nil) while IsEncryptedBoxFn says it is encrypted, validation fails closed with this error rather than checking against the global database.
Solutions
- Unlock/open the encrypted notebook before validating or running SQL against it
- Check IsEncryptedBoxFn(boxID) && GetEncryptedDB(boxID)==nil first and prompt the user to unlock
- Retry validation after the box DB is opened
Example fix
// before
if err := sql.CheckReadonlyBlockQueryStatement(stmt, boxID); err != nil { ... }
// after
if sql.IsEncryptedBoxFn(boxID) && sql.GetEncryptedDB(boxID) == nil {
return errors.New("unlock encrypted notebook before querying")
}
if err := sql.CheckReadonlyBlockQueryStatement(stmt, boxID); err != nil { ... } Defensive patterns
Strategy: validation
Validate before calling
if sql.IsEncryptedBoxFn(boxID) && sql.GetEncryptedDB(boxID) == nil {
return errors.New("cannot validate SQL: encrypted box locked")
} Try / catch
if err := sql.CheckReadonlyStatementInBox(stmt, boxID); err != nil {
if strings.Contains(err.Error(), "encrypted box db not opened") {
return unlockThenValidate(stmt, boxID)
}
return err
} Prevention
- Validate SQL only after the target notebook's DB is opened
- Surface an unlock prompt when validation fails for encrypted boxes
- Keep boxID state (locked vs unlocked) in the UI so SQL panels disable validation for locked boxes
When it happens
Trigger: Calling CheckReadonlyStatementInBox (directly or via sqlQuery, validateDocumentStatQuery, CheckReadonlyBlockQueryStatement) with a boxID of a locked encrypted notebook.
Common situations: SQL query panel opened for an encrypted notebook before the user unlocked it; a session where the encrypted DB was closed but the box remains flagged encrypted.
Understand the failure class
Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.
Related errors
- Conf.Language(313)
- encrypted asset history is missing valid notebook context
- encrypted box db not opened for box
- Encrypted notebooks do not support this operation
- password must not be empty
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/780096015262a7da.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/sql/stmt_validate.go:193
// CheckReadonlyBlockQueryStatement 校验原始块查询语句为「单条 + 只读」,boxID 为空或普通笔记本时
// 在全局库上校验,加密笔记本在其独立加密库上校验。
//
// 嵌入块脚本、模板查询和导出路径的 SQL 都来自文档内容,属于不可信输入,执行前必须经过本校验。
// 见 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-67p9-hm94-xwf3
func CheckReadonlyBlockQueryStatement(stmt, boxID string) error {
if err := CheckSingleStatement(stmt); nil != err {
return err
}
return CheckReadonlyStatementInBox(stmt, boxID)
}
// CheckReadonlyStatementInBox 在指定笔记本对应的数据库连接上检查 SQL 是否只读。
func CheckReadonlyStatementInBox(stmt, boxID string) error {
targetDB := db
if boxDB := GetEncryptedDB(boxID); nil != boxDB {
targetDB = boxDB
} else if IsEncryptedBoxFn != nil && IsEncryptedBoxFn(boxID) {
return errors.New("encrypted box db not opened for box " + boxID)
}
return checkReadonlyStatement(stmt, targetDB)
}
func checkReadonlyStatement(stmt string, targetDB *sql.DB) error {
if strings.TrimSpace(stmt) == "" {
return errors.New("SQL statement is empty")
}
if !isReadonlyQueryStatement(stmt) {
return errors.New("SQL statement is not a read-only query")
}
if nil == targetDB {
return errors.New("database is nil")
}
ctx := context.Background()
conn, err := targetDB.Conn(ctx)
if err != nil {
return errView on GitHub (pinned to 9f775e8a12)