-
Notifications
You must be signed in to change notification settings - Fork 0
Adds safety checks when accessing database env #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
🚀 Preview Deployments Ready!Your changes have been deployed to preview environments: 📦
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 issues found across 7 files
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="mcp-studio/server/db/postgres.ts">
<violation number="1" location="mcp-studio/server/db/postgres.ts:22">
P1: Incomplete optional chaining will still throw TypeError. If `MESH_REQUEST_CONTEXT` or `state` is nullish, `?.DATABASE` evaluates to `undefined`, and calling `.DATABASES_RUN_SQL()` on `undefined` throws. Consider adding `?.` before `DATABASES_RUN_SQL` and handling the undefined response, or throwing an explicit error early if the context is required.</violation>
</file>
<file name="mcp-studio/server/engine/events.ts">
<violation number="1" location="mcp-studio/server/engine/events.ts:19">
P1: Incomplete optional chaining will crash if `state` is nullish. When `state?.DATABASE` returns `undefined`, calling `.DATABASES_RUN_SQL()` throws a TypeError. Either add `?.` before `DATABASES_RUN_SQL` and handle the undefined result, or throw an explicit error early if the database connection is unavailable.</violation>
</file>
<file name="mcp-studio/server/tools/workflow.ts">
<violation number="1" location="mcp-studio/server/tools/workflow.ts:117">
P1: Incomplete optional chaining creates a false sense of safety. If `MESH_REQUEST_CONTEXT` or `state` is null/undefined, the chain returns `undefined`, and calling `.DATABASES_RUN_SQL()` on it will throw a `TypeError`. Either complete the chain (`?.DATABASE?.DATABASES_RUN_SQL?.`) or add an early guard clause to throw a meaningful error.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| ): Promise<T[]> { | ||
| const response = | ||
| await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({ | ||
| await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Incomplete optional chaining will still throw TypeError. If MESH_REQUEST_CONTEXT or state is nullish, ?.DATABASE evaluates to undefined, and calling .DATABASES_RUN_SQL() on undefined throws. Consider adding ?. before DATABASES_RUN_SQL and handling the undefined response, or throwing an explicit error early if the context is required.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At mcp-studio/server/db/postgres.ts, line 22:
<comment>Incomplete optional chaining will still throw TypeError. If `MESH_REQUEST_CONTEXT` or `state` is nullish, `?.DATABASE` evaluates to `undefined`, and calling `.DATABASES_RUN_SQL()` on `undefined` throws. Consider adding `?.` before `DATABASES_RUN_SQL` and handling the undefined response, or throwing an explicit error early if the context is required.</comment>
<file context>
@@ -19,7 +19,7 @@ export async function runSQL<T = unknown>(
): Promise<T[]> {
const response =
- await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
+ await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
sql,
params,
</file context>
| const now = Date.now(); | ||
| const result = | ||
| await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({ | ||
| await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Incomplete optional chaining will crash if state is nullish. When state?.DATABASE returns undefined, calling .DATABASES_RUN_SQL() throws a TypeError. Either add ?. before DATABASES_RUN_SQL and handle the undefined result, or throw an explicit error early if the database connection is unavailable.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At mcp-studio/server/engine/events.ts, line 19:
<comment>Incomplete optional chaining will crash if `state` is nullish. When `state?.DATABASE` returns `undefined`, calling `.DATABASES_RUN_SQL()` throws a TypeError. Either add `?.` before `DATABASES_RUN_SQL` and handle the undefined result, or throw an explicit error early if the database connection is unavailable.</comment>
<file context>
@@ -16,7 +16,7 @@ export async function getPendingEvents(
const now = Date.now();
const result =
- await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
+ await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
sql: `SELECT * FROM workflow_event WHERE execution_id = ? AND consumed_at IS NULL
AND (visible_at IS NULL OR visible_at <= ?) ${type ? "AND type = ?" : ""}
</file context>
| ${whereClause} | ||
| ${orderByClause} | ||
| LIMIT ? OFFSET ? | ||
| `; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Incomplete optional chaining creates a false sense of safety. If MESH_REQUEST_CONTEXT or state is null/undefined, the chain returns undefined, and calling .DATABASES_RUN_SQL() on it will throw a TypeError. Either complete the chain (?.DATABASE?.DATABASES_RUN_SQL?.) or add an early guard clause to throw a meaningful error.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At mcp-studio/server/tools/workflow.ts, line 117:
<comment>Incomplete optional chaining creates a false sense of safety. If `MESH_REQUEST_CONTEXT` or `state` is null/undefined, the chain returns `undefined`, and calling `.DATABASES_RUN_SQL()` on it will throw a `TypeError`. Either complete the chain (`?.DATABASE?.DATABASES_RUN_SQL?.`) or add an early guard clause to throw a meaningful error.</comment>
<file context>
@@ -114,14 +114,14 @@ export const createListTool = (env: Env) =>
const itemsResult: any =
- await env.MESH_REQUEST_CONTEXT.state.DATABASE.DATABASES_RUN_SQL({
+ await env.MESH_REQUEST_CONTEXT?.state?.DATABASE.DATABASES_RUN_SQL({
sql,
params: [...params, limit, offset],
</file context>
Summary by cubic
Add null-safe checks around env.MESH_REQUEST_CONTEXT.state.DATABASE to prevent crashes when the database binding is missing. Calls now fail gracefully or return empty results where appropriate.
Written for commit a258e2d. Summary will update on new commits.