{"record":{"id":"a773c4a1398cb982","repo":"paperclipai/paperclip","slug":"ctx-db-query-only-allows-select-statements","errorCode":null,"errorMessage":"ctx.db.query only allows SELECT statements","messagePattern":"ctx\\.db\\.query only allows SELECT statements","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/src/services/plugin-database.ts","lineNumber":259,"sourceCode":"    }\n    throw new Error(`Plugin SQL references schema \"${ref.schema}\" outside namespace \"${namespace}\"`);\n  }\n}\n\nexport function validatePluginRuntimeQuery(\n  query: string,\n  namespace: string,\n  coreReadTables: readonly PluginDatabaseCoreReadTable[] = [],\n): void {\n  const statements = splitSqlStatements(query);\n  if (statements.length !== 1) {\n    throw new Error(\"Plugin runtime SQL must contain exactly one statement\");\n  }\n  const statement = statements[0]!;\n  assertNoBannedSql(statement);\n  const normalized = normaliseSql(statement);\n  if (!normalized.startsWith(\"select \") && !normalized.startsWith(\"with \")) {\n    throw new Error(\"ctx.db.query only allows SELECT statements\");\n  }\n  if (/\\b(insert|update|delete|alter|create|drop|truncate)\\b/.test(normalized)) {\n    throw new Error(\"ctx.db.query cannot contain mutation or DDL keywords\");\n  }\n\n  const allowedCoreReadTables = new Set(coreReadTables);\n  for (const ref of extractQualifiedRefs(statement)) {\n    if (ref.schema === namespace) continue;\n    if (ref.schema === \"public\") {\n      assertAllowedPublicRead(ref, allowedCoreReadTables);\n      continue;\n    }\n    throw new Error(`ctx.db.query cannot read schema \"${ref.schema}\"`);\n  }\n}\n\nexport function validatePluginRuntimeExecute(query: string, namespace: string): void {\n  const statements = splitSqlStatements(query);","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/paperclipai/paperclip/blob/120ae5428fa29bee300bcf806491cd4d965fbb7c/server/src/services/plugin-database.ts#L241-L277","documentation":"Read-only guard: the single runtime statement is neither SELECT nor a WITH (CTE) query. ctx.db.query is the plugin's read API; writes must go through ctx.db.execute. The mutating statement passed to query is at fault.","triggerScenarios":"Thrown at server/src/services/plugin-database.ts:259 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use ctx.db.execute for writes; ctx.db.query only allows SELECT statements."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"120ae5428fa29bee300bcf806491cd4d965fbb7c","analyzedAt":"2026-08-18T22:49:45.177Z","contentChangedAt":"2026-08-18T22:49:45.177Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}