Skip to content

fix: issue 599, throw error if the given user context doesn't contain full id fields #629

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

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/runtime/src/enhancements/policy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import path from 'path';
import semver from 'semver';
import { PRISMA_MINIMUM_VERSION } from '../../constants';
import { AuthUser, DbClientContract } from '../../types';
import { hasAllFields } from '../../validation';
import { getDefaultModelMeta } from '../model-meta';
import { makeProxy } from '../proxy';
import type { ModelMeta, PolicyDef, ZodSchemas } from '../types';
import { getIdFields } from '../utils';
import { PolicyProxyHandler } from './handler';

/**
Expand Down Expand Up @@ -70,6 +72,21 @@ export function withPolicy<DbClient extends object>(
const _modelMeta = options?.modelMeta ?? getDefaultModelMeta();
const _zodSchemas = options?.zodSchemas ?? getDefaultZodSchemas();

// validate user context
if (context?.user) {
const idFields = getIdFields(_modelMeta, 'User');
if (
!hasAllFields(
context.user,
idFields.map((f) => f.name)
)
) {
throw new Error(
`Invalid user context: must have valid ID field ${idFields.map((f) => `"${f.name}"`).join(', ')}`
);
}
}

return makeProxy(
prisma,
_modelMeta,
Expand Down
8 changes: 6 additions & 2 deletions packages/runtime/src/enhancements/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ export function getModelFields(data: object) {
* Gets id fields for the given model.
*/
export function getIdFields(modelMeta: ModelMeta, model: string, throwIfNotFound = false) {
const fields = modelMeta.fields[lowerCaseFirst(model)];
let fields = modelMeta.fields[lowerCaseFirst(model)];
if (!fields) {
throw new Error(`Unable to load fields for ${model}`);
if (throwIfNotFound) {
throw new Error(`Unable to load fields for ${model}`);
} else {
fields = {};
}
}
const result = Object.values(fields).filter((f) => f.isId);
if (result.length === 0 && throwIfNotFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ describe('With Policy: auth() test', () => {
const authDb = withPolicy();
await expect(authDb.post.update({ where: { id: '1' }, data: { title: 'bcd' } })).toBeRejectedByPolicy();

const authDb1 = withPolicy({ id: null });
await expect(authDb1.post.update({ where: { id: '1' }, data: { title: 'bcd' } })).rejects.toThrow();
expect(() => withPolicy({ id: null })).toThrow(/Invalid user context/);

const authDb2 = withPolicy({ id: 'user1' });
await expect(authDb2.post.update({ where: { id: '1' }, data: { title: 'bcd' } })).toResolveTruthy();
Expand Down Expand Up @@ -148,9 +147,6 @@ describe('With Policy: auth() test', () => {

await expect(db.post.update({ where: { id: '1' }, data: { title: 'bcd' } })).toBeRejectedByPolicy();

const authDb1 = withPolicy({ id: null });
await expect(authDb1.post.update({ where: { id: '1' }, data: { title: 'bcd' } })).rejects.toThrow();

const authDb2 = withPolicy({ id: 'user1' });
await expect(authDb2.post.update({ where: { id: '1' }, data: { title: 'bcd' } })).toResolveTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('With Policy: multiple id fields', () => {
await prisma.user.create({ data: { x: '1', y: '1' } });
await prisma.user.create({ data: { x: '1', y: '2' } });

const anonDb = withPolicy({});
const anonDb = withPolicy();

await expect(
anonDb.m.create({ data: { owner: { connect: { x_y: { x: '1', y: '2' } } } } })
Expand Down