{"record":{"id":"780096015262a7da","repo":"siyuan-note/siyuan","slug":"encrypted-box-db-not-opened-for-box","errorCode":null,"errorMessage":"encrypted box db not opened for box ","messagePattern":"encrypted box db not opened for box ","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/sql/stmt_validate.go","lineNumber":193,"sourceCode":"// CheckReadonlyBlockQueryStatement 校验原始块查询语句为「单条 + 只读」，boxID 为空或普通笔记本时\n// 在全局库上校验，加密笔记本在其独立加密库上校验。\n//\n// 嵌入块脚本、模板查询和导出路径的 SQL 都来自文档内容，属于不可信输入，执行前必须经过本校验。\n// 见 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-67p9-hm94-xwf3\nfunc CheckReadonlyBlockQueryStatement(stmt, boxID string) error {\n\tif err := CheckSingleStatement(stmt); nil != err {\n\t\treturn err\n\t}\n\treturn CheckReadonlyStatementInBox(stmt, boxID)\n}\n\n// CheckReadonlyStatementInBox 在指定笔记本对应的数据库连接上检查 SQL 是否只读。\nfunc CheckReadonlyStatementInBox(stmt, boxID string) error {\n\ttargetDB := db\n\tif boxDB := GetEncryptedDB(boxID); nil != boxDB {\n\t\ttargetDB = boxDB\n\t} else if IsEncryptedBoxFn != nil && IsEncryptedBoxFn(boxID) {\n\t\treturn errors.New(\"encrypted box db not opened for box \" + boxID)\n\t}\n\treturn checkReadonlyStatement(stmt, targetDB)\n}\n\nfunc checkReadonlyStatement(stmt string, targetDB *sql.DB) error {\n\tif strings.TrimSpace(stmt) == \"\" {\n\t\treturn errors.New(\"SQL statement is empty\")\n\t}\n\tif !isReadonlyQueryStatement(stmt) {\n\t\treturn errors.New(\"SQL statement is not a read-only query\")\n\t}\n\tif nil == targetDB {\n\t\treturn errors.New(\"database is nil\")\n\t}\n\tctx := context.Background()\n\tconn, err := targetDB.Conn(ctx)\n\tif err != nil {\n\t\treturn err","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/sql/stmt_validate.go#L175-L211","documentation":"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.","triggerScenarios":"Calling CheckReadonlyStatementInBox (directly or via sqlQuery, validateDocumentStatQuery, CheckReadonlyBlockQueryStatement) with a boxID of a locked encrypted notebook.","commonSituations":"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.","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"],"exampleFix":"// before\nif err := sql.CheckReadonlyBlockQueryStatement(stmt, boxID); err != nil { ... }\n// after\nif sql.IsEncryptedBoxFn(boxID) && sql.GetEncryptedDB(boxID) == nil {\n    return errors.New(\"unlock encrypted notebook before querying\")\n}\nif err := sql.CheckReadonlyBlockQueryStatement(stmt, boxID); err != nil { ... }","handlingStrategy":"validation","validationCode":"if sql.IsEncryptedBoxFn(boxID) && sql.GetEncryptedDB(boxID) == nil {\n    return errors.New(\"cannot validate SQL: encrypted box locked\")\n}","typeGuard":null,"tryCatchPattern":"if err := sql.CheckReadonlyStatementInBox(stmt, boxID); err != nil {\n    if strings.Contains(err.Error(), \"encrypted box db not opened\") {\n        return unlockThenValidate(stmt, boxID)\n    }\n    return err\n}","preventionTips":["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"],"tags":["sql","encrypted-notebook","validation","fail-closed"],"backgroundTag":"sql-query-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}