{"record":{"id":"8ae9c82cc379a9f5","repo":"paperclipai/paperclip","slug":"ctx-db-execute-cannot-contain-ddl-keywords","errorCode":null,"errorMessage":"ctx.db.execute cannot contain DDL keywords","messagePattern":"ctx\\.db\\.execute cannot contain DDL keywords","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/src/services/plugin-database.ts","lineNumber":288,"sourceCode":"      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);\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 (!/^(insert\\s+into|update|delete\\s+from)\\b/.test(normalized)) {\n    throw new Error(\"ctx.db.execute only allows INSERT, UPDATE, or DELETE\");\n  }\n  if (/\\b(alter|create|drop|truncate)\\b/.test(normalized)) {\n    throw new Error(\"ctx.db.execute cannot contain DDL keywords\");\n  }\n\n  const refs = extractQualifiedRefs(statement);\n  const target = refs.find((ref) => [\"into\", \"update\", \"from\"].includes(ref.keyword));\n  if (!target || target.schema !== namespace) {\n    throw new Error(`ctx.db.execute target must be inside plugin namespace \"${namespace}\"`);\n  }\n  for (const ref of refs) {\n    if (ref.schema !== namespace) {\n      throw new Error(\"ctx.db.execute cannot reference public or other non-plugin schemas\");\n    }\n  }\n}\n\nfunction bindSql(statement: string, params: readonly unknown[] = []): SQL {\n  // Safe only after callers run the plugin SQL validators above.\n  if (params.length === 0) return sql.raw(statement);\n  const chunks: SQL[] = [];","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/paperclipai/paperclip/blob/120ae5428fa29bee300bcf806491cd4d965fbb7c/server/src/services/plugin-database.ts#L270-L306","documentation":"DDL ban inside runtime execute: even DML-shaped statements are rejected if they contain alter/create/drop/truncate anywhere. Prevents plugins from changing schema through the execute path; the statement embedding DDL keywords is at fault.","triggerScenarios":"Thrown at server/src/services/plugin-database.ts:288 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove DDL keywords from ctx.db.execute SQL; use migrations for schema changes."],"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"}