Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions api/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const allowedEnvironmentVars = [
'MAX_BATCH_MUTATION',
'LOGGER_.+',
'ROBOTS_TXT',
'MAX_RELATIONAL_DEPTH',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not present in the rest of the code ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already existed; it just wasn’t present here like all environment variables. Here’s an example of a call:
const maxRelationalDepth = Number(env['MAX_RELATIONAL_DEPTH']) > 2 ? Number(env['MAX_RELATIONAL_DEPTH']) : 2;

'MAX_ITEMS_PER_QUERY',
// server
'SERVER_.+',
// database
Expand Down Expand Up @@ -211,6 +213,7 @@ const defaults: Record<string, any> = {
PUBLIC_URL: '/',
MAX_PAYLOAD_SIZE: '1mb',
MAX_RELATIONAL_DEPTH: 10,
MAX_ITEMS_PER_QUERY: 1000,
MAX_BATCH_MUTATION: Infinity,
ROBOTS_TXT: 'User-agent: *\nDisallow: /',

Expand Down
11 changes: 10 additions & 1 deletion api/src/utils/sanitize-query.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import type { Accountability, Aggregate, Filter, Query } from '@wbce-d9/types';
import { parseFilter, parseJSON } from '@wbce-d9/utils';
import { flatten, get, isPlainObject, merge, set } from 'lodash-es';
import env from '../env.js';
import logger from '../logger.js';
import { Meta } from '../types/index.js';

export function sanitizeQuery(rawQuery: Record<string, any>, accountability?: Accountability | null): Query {
const query: Query = {};

if (rawQuery['limit'] !== undefined) {
const limit = sanitizeLimit(rawQuery['limit']);
let limit = sanitizeLimit(rawQuery['limit']);

if (typeof limit === 'number') {
const maxItemsPerQuery = Number(env['MAX_ITEMS_PER_QUERY']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env already has a functionality to format env value.
It uses either typeMap or a type prefix in the env variable.
Would it be possible to use this instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I will look into that. I based myself on the existing practices on the following line:
const maxRelationalDepth = Number(env['MAX_RELATIONAL_DEPTH']) > 2 ? Number(env['MAX_RELATIONAL_DEPTH']) : 2;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be automatically converted with:
if (
String(value).startsWith('0') === false &&
isNaN(value) === false &&
value.length > 0 &&
value <= Number.MAX_SAFE_INTEGER
) {
env[key] = Number(value);
continue;
}

It could be enforced by adding it in typeMap.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then we can remove the Number(...) constructor ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's done


if (maxItemsPerQuery !== -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxItemsPerQuery !== -1 && limit === -1 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (limit === -1) {
limit = maxItemsPerQuery;
}
}

query.limit = limit;
}
}
Expand Down
14 changes: 14 additions & 0 deletions api/src/utils/validate-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function validateQuery(query: Query): Query {
validateAlias(query.alias);
}

if (query.limit) {
validateLimit(query.limit);
}

validateRelationalDepth(query);

if (error) {
Expand Down Expand Up @@ -232,3 +236,13 @@ function validateRelationalDepth(query: Query) {
}
}
}

function validateLimit(limit: any) {
const maxItemsPerQuery = Number(env['MAX_ITEMS_PER_QUERY']) || 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be manage by the getEnv and env definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, moreover, I initialized it in defaults.


if (limit !== -1 && maxItemsPerQuery !== -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxItemsPerQuery !== -1 is sufficient here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that was the initial test before adding the sanitize.

if (limit > maxItemsPerQuery) {
throw new InvalidQueryException(`"Limit" can't exceed ${maxItemsPerQuery}`);
}
}
}
1 change: 1 addition & 0 deletions docs/self-hosted/config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ prefixing the value with `{type}:`. The following types are available:
| `GRAPHQL_INTROSPECTION` | Whether or not to enable GraphQL Introspection | `true` |
| `MAX_BATCH_MUTATION` | The maximum number of items for batch mutations when creating, updating and deleting. | `Infinity` |
| `MAX_RELATIONAL_DEPTH` | The maximum depth when filtering / querying relational fields, with a minimum value of `2`. | `10` |
| `MAX_ITEMS_PER_QUERY` | The maximum items when allowed querying relational fields. Set it to `-1` to remove the limitation. | `1000` |
| `ROBOTS_TXT` | What the `/robots.txt` endpoint should return | `User-agent: *\nDisallow: /` |
| `X_POWERED_BY_ENABLED` | Whether the response should return the X-Powered-By Directus Header | `true` |

Expand Down
1 change: 1 addition & 0 deletions tests/blackbox/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const directusConfig = {
SERVE_APP: 'false',
DB_EXCLUDE_TABLES: 'knex_migrations,knex_migrations_lock,spatial_ref_sys,sysdiagrams',
MAX_RELATIONAL_DEPTH: '5',
MAX_ITEMS_PER_QUERY: '1000',
MAX_PAYLOAD_SIZE: '10mb',
EXTENSIONS_PATH: './extensions',
ASSETS_TRANSFORM_MAX_CONCURRENT: '2',
Expand Down
Loading