Skip to content
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

Sanitize query for item read/update/delete operations in Flows #13900

Merged
merged 1 commit into from Jun 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions api/src/operations/item-delete/index.ts
@@ -1,8 +1,9 @@
import { Accountability, PrimaryKey } from '@directus/shared/types';
import { defineOperationApi, toArray } from '@directus/shared/utils';
import { ItemsService } from '../../services';
import { optionToObject } from '../../utils/operation-options';
import { getAccountabilityForRole } from '../../utils/get-accountability-for-role';
import { optionToObject } from '../../utils/operation-options';
import { sanitizeQuery } from '../../utils/sanitize-query';

type Options = {
collection: string;
Expand Down Expand Up @@ -36,11 +37,12 @@ export default defineOperationApi<Options>({
});

const queryObject = query ? optionToObject(query) : {};
const sanitizedQueryObject = sanitizeQuery(queryObject, customAccountability);

let result: PrimaryKey | PrimaryKey[] | null;

if (!key) {
result = await itemsService.deleteByQuery(queryObject);
result = await itemsService.deleteByQuery(sanitizedQueryObject);
} else {
const keys = toArray(key);

Expand Down
10 changes: 6 additions & 4 deletions api/src/operations/item-read/index.ts
Expand Up @@ -2,8 +2,9 @@ import { Accountability, PrimaryKey } from '@directus/shared/types';
import { defineOperationApi, toArray } from '@directus/shared/utils';
import { ItemsService } from '../../services';
import { Item } from '../../types';
import { optionToObject } from '../../utils/operation-options';
import { getAccountabilityForRole } from '../../utils/get-accountability-for-role';
import { optionToObject } from '../../utils/operation-options';
import { sanitizeQuery } from '../../utils/sanitize-query';

type Options = {
collection: string;
Expand Down Expand Up @@ -37,18 +38,19 @@ export default defineOperationApi<Options>({
});

const queryObject = query ? optionToObject(query) : {};
const sanitizedQueryObject = sanitizeQuery(queryObject, customAccountability);

let result: Item | Item[] | null;

if (!key) {
result = await itemsService.readByQuery(queryObject);
result = await itemsService.readByQuery(sanitizedQueryObject);
} else {
const keys = toArray(key);

if (keys.length === 1) {
result = await itemsService.readOne(keys[0], queryObject);
result = await itemsService.readOne(keys[0], sanitizedQueryObject);
} else {
result = await itemsService.readMany(keys, queryObject);
result = await itemsService.readMany(keys, sanitizedQueryObject);
}
}

Expand Down
6 changes: 4 additions & 2 deletions api/src/operations/item-update/index.ts
Expand Up @@ -2,8 +2,9 @@ import { Accountability, PrimaryKey } from '@directus/shared/types';
import { defineOperationApi, toArray } from '@directus/shared/utils';
import { ItemsService } from '../../services';
import { Item } from '../../types';
import { optionToObject } from '../../utils/operation-options';
import { getAccountabilityForRole } from '../../utils/get-accountability-for-role';
import { optionToObject } from '../../utils/operation-options';
import { sanitizeQuery } from '../../utils/sanitize-query';

type Options = {
collection: string;
Expand Down Expand Up @@ -40,6 +41,7 @@ export default defineOperationApi<Options>({
const payloadObject: Partial<Item> | Partial<Item>[] | null = optionToObject(payload) ?? null;

const queryObject = query ? optionToObject(query) : {};
const sanitizedQueryObject = sanitizeQuery(queryObject, customAccountability);

if (!payloadObject) {
return null;
Expand All @@ -48,7 +50,7 @@ export default defineOperationApi<Options>({
let result: PrimaryKey | PrimaryKey[] | null;

if (!key) {
result = await itemsService.updateByQuery(queryObject, payloadObject);
result = await itemsService.updateByQuery(sanitizedQueryObject, payloadObject);
} else {
const keys = toArray(key);

Expand Down