Skip to content

useConvexAccessControl (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useConvexAccessControl": "error"
}
}
}
}

Require public Convex functions to call an access control function.

Anyone on the internet can call an exported query, mutation or action, so a missing access check lets any user read or change data they shouldn’t have access to. The rule checks these functions when they’re imported from _generated/server. Internal functions, such as internalMutation, are ignored.

Access control functions are recognized by name. By default, these match: requireUser, assertAdmin, checkAccess, ensureOwner, canEditNote, hasRole and authz.require. See the prefixes option for the exact rule. A plain require(...) call never counts.

The call must be at the top level of the handler: on its own, in a variable declaration, in a return statement, or in the condition of an if statement. Calls inside an if body, a try block or another function aren’t detected. The rule can’t tell whether the check is the right one for the function.

import { mutation } from "./_generated/server";
export const send = mutation({
args: {},
handler: async (ctx) => {
await ctx.db.insert("messages", { body: "hello" });
},
});
code-block.js:3:21 lint/nursery/useConvexAccessControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This public Convex mutation doesn't call an access control function.

1 │ import { mutation } from "./_generated/server";
2 │
> 3 │ export const send = mutation({
│ ^^^^^^^^
4 │ args: {},
5 │ handler: async (ctx) => {

ℹ Anyone on the internet can call this function, so without an access control check, any user can read or change data they shouldn't have access to.

ℹ Call an access control function, such as requireUser(ctx) or canEditNote(ctx), at the top level of the handler. If this function shouldn't be callable from outside, use internalMutation instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { internalMutation, mutation, query } from "./_generated/server";
export const list = query({
args: {},
handler: async (ctx) => {
const user = await requireUser(ctx);
},
});
export const update = mutation({
args: {},
handler: async (ctx) => {
if (!(await canEditNote(ctx))) {
throw new Error("Unauthorized");
}
},
});
export const remove = mutation({
args: {},
handler: async (ctx) => {
await authz.require(ctx, "documents:delete");
},
});
export const cleanup = internalMutation({
args: {},
handler: async (ctx) => {
await ctx.db.delete("messages", messageId);
},
});

Type: string[]

Default: []

Names of functions that count as access control functions, in addition to the names matched by prefixes. The whole name must match.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useConvexAccessControl": {
"level": "on",
"options": {
"functions": [
"getCurrentUserOrThrow"
]
}
}
}
}
}
}
import { query } from "./_generated/server";
export const list = query({
args: {},
handler: async (ctx) => {
await getCurrentUser(ctx);
},
});
code-block.js:3:21 lint/nursery/useConvexAccessControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This public Convex query doesn't call an access control function.

1 │ import { query } from "./_generated/server";
2 │
> 3 │ export const list = query({
│ ^^^^^
4 │ args: {},
5 │ handler: async (ctx) => {

ℹ Anyone on the internet can call this function, so without an access control check, any user can read or change data they shouldn't have access to.

ℹ Call an access control function, such as requireUser(ctx) or canEditNote(ctx), at the top level of the handler. If this function shouldn't be callable from outside, use internalQuery instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { query } from "./_generated/server";
export const list = query({
args: {},
handler: async (ctx) => {
await getCurrentUserOrThrow(ctx);
},
});

Type: string[]

Default: ["assert", "can", "check", "ensure", "has", "require"]

Words that start the name of an access control function. A name matches a prefix when it’s the prefix on its own, or the prefix followed by an uppercase letter or _. A plain has(...) call never counts, because it’s usually a Map or Set lookup. Setting this option replaces the default prefixes.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useConvexAccessControl": {
"level": "on",
"options": {
"prefixes": [
"authorize"
]
}
}
}
}
}
}
import { query } from "./_generated/server";
export const list = query({
args: {},
handler: async (ctx) => {
await requireUser(ctx);
},
});
code-block.js:3:21 lint/nursery/useConvexAccessControl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This public Convex query doesn't call an access control function.

1 │ import { query } from "./_generated/server";
2 │
> 3 │ export const list = query({
│ ^^^^^
4 │ args: {},
5 │ handler: async (ctx) => {

ℹ Anyone on the internet can call this function, so without an access control check, any user can read or change data they shouldn't have access to.

ℹ Call one of your access control functions at the top level of the handler. If this function shouldn't be callable from outside, use internalQuery instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { query } from "./_generated/server";
export const list = query({
args: {},
handler: async (ctx) => {
await authorizeUser(ctx);
},
});