@directus/api 17.0.0 → 17.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/controllers/fields.js +0 -3
  2. package/dist/controllers/items.js +8 -7
  3. package/dist/extensions/lib/sandbox/generate-api-extensions-sandbox-entrypoint.d.ts +1 -1
  4. package/dist/flows.js +2 -1
  5. package/dist/middleware/collection-exists.js +6 -6
  6. package/dist/operations/item-update/index.js +4 -1
  7. package/dist/operations/request/index.js +5 -5
  8. package/dist/request/agent-with-ip-validation.d.ts +11 -0
  9. package/dist/request/agent-with-ip-validation.js +34 -0
  10. package/dist/request/index.js +6 -5
  11. package/dist/request/is-denied-ip.d.ts +1 -0
  12. package/dist/request/{validate-ip.js → is-denied-ip.js} +10 -12
  13. package/dist/services/collections.d.ts +3 -2
  14. package/dist/services/collections.js +1 -1
  15. package/dist/services/fields.js +8 -1
  16. package/dist/services/files.js +4 -3
  17. package/dist/services/graphql/index.js +3 -2
  18. package/dist/services/import-export.js +62 -26
  19. package/dist/services/items.js +2 -1
  20. package/dist/services/permissions.js +1 -1
  21. package/dist/services/relations.js +1 -1
  22. package/dist/services/roles.d.ts +9 -4
  23. package/dist/services/roles.js +50 -2
  24. package/dist/services/specifications.js +4 -3
  25. package/dist/services/utils.js +1 -1
  26. package/dist/telemetry/utils/get-user-item-count.js +2 -1
  27. package/dist/types/collection.d.ts +2 -13
  28. package/dist/utils/get-field-system-rows.d.ts +2 -0
  29. package/dist/utils/get-field-system-rows.js +17 -0
  30. package/dist/utils/get-permissions.js +1 -1
  31. package/dist/utils/get-schema.js +3 -2
  32. package/dist/utils/merge-permissions-for-share.js +1 -1
  33. package/dist/utils/should-skip-cache.js +1 -1
  34. package/dist/websocket/controllers/hooks.js +1 -1
  35. package/dist/websocket/handlers/items.js +2 -1
  36. package/dist/websocket/messages.d.ts +18 -18
  37. package/package.json +42 -41
  38. package/dist/database/system-data/app-access-permissions/app-access-permissions.yaml +0 -107
  39. package/dist/database/system-data/app-access-permissions/index.d.ts +0 -3
  40. package/dist/database/system-data/app-access-permissions/index.js +0 -17
  41. package/dist/database/system-data/app-access-permissions/schema-access-permissions.yaml +0 -17
  42. package/dist/database/system-data/collections/collections.yaml +0 -103
  43. package/dist/database/system-data/collections/index.d.ts +0 -2
  44. package/dist/database/system-data/collections/index.js +0 -9
  45. package/dist/database/system-data/fields/_defaults.yaml +0 -16
  46. package/dist/database/system-data/fields/activity.yaml +0 -83
  47. package/dist/database/system-data/fields/collections.yaml +0 -249
  48. package/dist/database/system-data/fields/dashboards.yaml +0 -20
  49. package/dist/database/system-data/fields/extensions.yaml +0 -10
  50. package/dist/database/system-data/fields/fields.yaml +0 -104
  51. package/dist/database/system-data/fields/files.yaml +0 -160
  52. package/dist/database/system-data/fields/flows.yaml +0 -26
  53. package/dist/database/system-data/fields/folders.yaml +0 -14
  54. package/dist/database/system-data/fields/index.d.ts +0 -2
  55. package/dist/database/system-data/fields/index.js +0 -33
  56. package/dist/database/system-data/fields/migrations.yaml +0 -10
  57. package/dist/database/system-data/fields/notifications.yaml +0 -15
  58. package/dist/database/system-data/fields/operations.yaml +0 -23
  59. package/dist/database/system-data/fields/panels.yaml +0 -29
  60. package/dist/database/system-data/fields/permissions.yaml +0 -37
  61. package/dist/database/system-data/fields/presets.yaml +0 -56
  62. package/dist/database/system-data/fields/relations.yaml +0 -34
  63. package/dist/database/system-data/fields/revisions.yaml +0 -30
  64. package/dist/database/system-data/fields/roles.yaml +0 -61
  65. package/dist/database/system-data/fields/sessions.yaml +0 -16
  66. package/dist/database/system-data/fields/settings.yaml +0 -471
  67. package/dist/database/system-data/fields/shares.yaml +0 -83
  68. package/dist/database/system-data/fields/translations.yaml +0 -27
  69. package/dist/database/system-data/fields/users.yaml +0 -224
  70. package/dist/database/system-data/fields/versions.yaml +0 -38
  71. package/dist/database/system-data/fields/webhooks.yaml +0 -141
  72. package/dist/database/system-data/relations/index.d.ts +0 -2
  73. package/dist/database/system-data/relations/index.js +0 -9
  74. package/dist/database/system-data/relations/relations.yaml +0 -197
  75. package/dist/request/request-interceptor.d.ts +0 -2
  76. package/dist/request/request-interceptor.js +0 -28
  77. package/dist/request/response-interceptor.d.ts +0 -2
  78. package/dist/request/response-interceptor.js +0 -5
  79. package/dist/request/validate-ip.d.ts +0 -1
@@ -126,9 +126,6 @@ router.patch('/:collection/:field', validateCollection, asyncHandler(async (req,
126
126
  if (error) {
127
127
  throw new InvalidPayloadError({ reason: error.message });
128
128
  }
129
- if (req.body.schema && !req.body.type) {
130
- throw new InvalidPayloadError({ reason: `You need to provide "type" when providing "schema"` });
131
- }
132
129
  const fieldData = req.body;
133
130
  if (!fieldData.field)
134
131
  fieldData.field = req.params['field'];
@@ -8,9 +8,10 @@ import { ItemsService } from '../services/items.js';
8
8
  import { MetaService } from '../services/meta.js';
9
9
  import asyncHandler from '../utils/async-handler.js';
10
10
  import { sanitizeQuery } from '../utils/sanitize-query.js';
11
+ import { isSystemCollection } from '@directus/system-data';
11
12
  const router = express.Router();
12
13
  router.post('/:collection', collectionExists, asyncHandler(async (req, res, next) => {
13
- if (req.params['collection'].startsWith('directus_'))
14
+ if (isSystemCollection(req.params['collection']))
14
15
  throw new ForbiddenError();
15
16
  if (req.singleton) {
16
17
  throw new RouteNotFoundError({ path: req.path });
@@ -47,7 +48,7 @@ router.post('/:collection', collectionExists, asyncHandler(async (req, res, next
47
48
  return next();
48
49
  }), respond);
49
50
  const readHandler = asyncHandler(async (req, res, next) => {
50
- if (req.params['collection'].startsWith('directus_'))
51
+ if (isSystemCollection(req.params['collection']))
51
52
  throw new ForbiddenError();
52
53
  const service = new ItemsService(req.collection, {
53
54
  accountability: req.accountability,
@@ -77,7 +78,7 @@ const readHandler = asyncHandler(async (req, res, next) => {
77
78
  router.search('/:collection', collectionExists, validateBatch('read'), readHandler, respond);
78
79
  router.get('/:collection', collectionExists, readHandler, respond);
79
80
  router.get('/:collection/:pk', collectionExists, asyncHandler(async (req, res, next) => {
80
- if (req.params['collection'].startsWith('directus_'))
81
+ if (isSystemCollection(req.params['collection']))
81
82
  throw new ForbiddenError();
82
83
  const service = new ItemsService(req.collection, {
83
84
  accountability: req.accountability,
@@ -90,7 +91,7 @@ router.get('/:collection/:pk', collectionExists, asyncHandler(async (req, res, n
90
91
  return next();
91
92
  }), respond);
92
93
  router.patch('/:collection', collectionExists, validateBatch('update'), asyncHandler(async (req, res, next) => {
93
- if (req.params['collection'].startsWith('directus_'))
94
+ if (isSystemCollection(req.params['collection']))
94
95
  throw new ForbiddenError();
95
96
  const service = new ItemsService(req.collection, {
96
97
  accountability: req.accountability,
@@ -126,7 +127,7 @@ router.patch('/:collection', collectionExists, validateBatch('update'), asyncHan
126
127
  return next();
127
128
  }), respond);
128
129
  router.patch('/:collection/:pk', collectionExists, asyncHandler(async (req, res, next) => {
129
- if (req.params['collection'].startsWith('directus_'))
130
+ if (isSystemCollection(req.params['collection']))
130
131
  throw new ForbiddenError();
131
132
  if (req.singleton) {
132
133
  throw new RouteNotFoundError({ path: req.path });
@@ -149,7 +150,7 @@ router.patch('/:collection/:pk', collectionExists, asyncHandler(async (req, res,
149
150
  return next();
150
151
  }), respond);
151
152
  router.delete('/:collection', collectionExists, validateBatch('delete'), asyncHandler(async (req, _res, next) => {
152
- if (req.params['collection'].startsWith('directus_'))
153
+ if (isSystemCollection(req.params['collection']))
153
154
  throw new ForbiddenError();
154
155
  const service = new ItemsService(req.collection, {
155
156
  accountability: req.accountability,
@@ -168,7 +169,7 @@ router.delete('/:collection', collectionExists, validateBatch('delete'), asyncHa
168
169
  return next();
169
170
  }), respond);
170
171
  router.delete('/:collection/:pk', collectionExists, asyncHandler(async (req, _res, next) => {
171
- if (req.params['collection'].startsWith('directus_'))
172
+ if (isSystemCollection(req.params['collection']))
172
173
  throw new ForbiddenError();
173
174
  const service = new ItemsService(req.collection, {
174
175
  accountability: req.accountability,
@@ -16,7 +16,7 @@ export declare function generateApiExtensionsSandboxEntrypoint(type: ApiExtensio
16
16
  unregisterFunction: () => Promise<void>;
17
17
  } | {
18
18
  code: string;
19
- hostFunctions: ((path: import("isolated-vm").Reference<string>, method: import("isolated-vm").Reference<"GET" | "POST" | "DELETE" | "PUT" | "PATCH">, cb: import("isolated-vm").Reference<(req: {
19
+ hostFunctions: ((path: import("isolated-vm").Reference<string>, method: import("isolated-vm").Reference<"GET" | "POST" | "DELETE" | "PATCH" | "PUT">, cb: import("isolated-vm").Reference<(req: {
20
20
  url: string;
21
21
  headers: import("http").IncomingHttpHeaders;
22
22
  body: string;
package/dist/flows.js CHANGED
@@ -19,6 +19,7 @@ import { mapValuesDeep } from './utils/map-values-deep.js';
19
19
  import { redactObject } from './utils/redact-object.js';
20
20
  import { sanitizeError } from './utils/sanitize-error.js';
21
21
  import { scheduleSynchronizedJob, validateCron } from './utils/schedule.js';
22
+ import { isSystemCollection } from '@directus/system-data';
22
23
  let flowManager;
23
24
  export function getFlowManager() {
24
25
  if (flowManager) {
@@ -111,7 +112,7 @@ class FlowManager {
111
112
  if (!flow.options?.['collections'])
112
113
  return [];
113
114
  return toArray(flow.options['collections']).map((collection) => {
114
- if (collection.startsWith('directus_')) {
115
+ if (isSystemCollection(collection)) {
115
116
  const action = scope.split('.')[1];
116
117
  return collection.substring(9) + '.' + action;
117
118
  }
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Check if requested collection exists, and save it to req.collection
3
3
  */
4
- import { systemCollectionRows } from '../database/system-data/collections/index.js';
5
4
  import { ForbiddenError } from '@directus/errors';
5
+ import { systemCollectionRows } from '@directus/system-data';
6
6
  import asyncHandler from '../utils/async-handler.js';
7
7
  const collectionExists = asyncHandler(async (req, _res, next) => {
8
8
  if (!req.params['collection'])
@@ -11,11 +11,11 @@ const collectionExists = asyncHandler(async (req, _res, next) => {
11
11
  throw new ForbiddenError();
12
12
  }
13
13
  req.collection = req.params['collection'];
14
- if (req.collection.startsWith('directus_')) {
15
- const systemRow = systemCollectionRows.find((collection) => {
16
- return collection?.collection === req.collection;
17
- });
18
- req.singleton = !!systemRow?.singleton;
14
+ const systemCollectionRow = systemCollectionRows.find((collection) => {
15
+ return collection?.collection === req.collection;
16
+ });
17
+ if (systemCollectionRow !== undefined) {
18
+ req.singleton = !!systemCollectionRow?.singleton;
19
19
  }
20
20
  else {
21
21
  req.singleton = req.schema.collections[req.collection]?.singleton ?? false;
@@ -32,7 +32,10 @@ export default defineOperationApi({
32
32
  return null;
33
33
  }
34
34
  let result;
35
- if (!key || (Array.isArray(key) && key.length === 0)) {
35
+ if (Array.isArray(payloadObject)) {
36
+ result = await itemsService.updateBatch(payloadObject, { emitEvents: !!emitEvents });
37
+ }
38
+ else if (!key || (Array.isArray(key) && key.length === 0)) {
36
39
  result = await itemsService.updateByQuery(sanitizedQueryObject, payloadObject, { emitEvents: !!emitEvents });
37
40
  }
38
41
  else {
@@ -24,12 +24,12 @@ export default defineOperationApi({
24
24
  return { status: result.status, statusText: result.statusText, headers: result.headers, data: result.data };
25
25
  }
26
26
  catch (error) {
27
- if (isAxiosError(error)) {
27
+ if (isAxiosError(error) && error.response) {
28
28
  throw JSON.stringify({
29
- status: error.response?.status,
30
- statusText: error.response?.statusText,
31
- headers: error.response?.headers,
32
- data: error.response?.data,
29
+ status: error.response.status,
30
+ statusText: error.response.statusText,
31
+ headers: error.response.headers,
32
+ data: error.response.data,
33
33
  });
34
34
  }
35
35
  else {
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import type { Agent, ClientRequestArgs } from 'node:http';
3
+ /**
4
+ * 'createConnection' is missing in 'Agent' type, but assigned in actual implementation:
5
+ * https://github.com/nodejs/node/blob/8a41d9b636be86350cd32847c3f89d327c4f6ff7/lib/_http_agent.js#L215
6
+ */
7
+ export type _Agent = Agent & {
8
+ createConnection: NonNullable<ClientRequestArgs['createConnection']>;
9
+ };
10
+ /** Extends a HTTP agent with IP validation */
11
+ export declare const agentWithIpValidation: (agent: Agent) => Agent;
@@ -0,0 +1,34 @@
1
+ import { isIP } from 'node:net';
2
+ import { isDeniedIp } from './is-denied-ip.js';
3
+ const deniedError = (domain) => new Error(`Requested domain "${domain}" resolves to a denied IP address`);
4
+ /** Extends a HTTP agent with IP validation */
5
+ export const agentWithIpValidation = (agent) => {
6
+ const _agent = agent;
7
+ const { createConnection } = _agent;
8
+ _agent.createConnection = function (options, oncreate) {
9
+ const { host } = options;
10
+ /*
11
+ * Unexpected, but according to the types 'host' might be undefined.
12
+ * In that case, the request is denied to be on the safe side,
13
+ * since the host cannot be verified.
14
+ */
15
+ if (!host) {
16
+ throw new Error('Request cannot be verified due to missing host');
17
+ }
18
+ /*
19
+ * At this point, host is only verified if it's already an IP address.
20
+ * Otherwise it will be verified on 'lookup' event.
21
+ */
22
+ if (isIP(host) !== 0 && isDeniedIp(host))
23
+ throw deniedError(host);
24
+ const socket = createConnection.call(this, options, oncreate);
25
+ // Emitted after resolving the host name but before connecting.
26
+ socket.on('lookup', (error, address) => {
27
+ if (error || !isDeniedIp(address))
28
+ return;
29
+ return socket.destroy(deniedError(host));
30
+ });
31
+ return socket;
32
+ };
33
+ return agent;
34
+ };
@@ -1,14 +1,15 @@
1
- import { requestInterceptor } from './request-interceptor.js';
2
- import { responseInterceptor } from './response-interceptor.js';
3
1
  export const _cache = {
4
2
  axiosInstance: null,
5
3
  };
6
4
  export async function getAxios() {
7
5
  if (!_cache.axiosInstance) {
8
6
  const axios = (await import('axios')).default;
9
- _cache.axiosInstance = axios.create();
10
- _cache.axiosInstance.interceptors.request.use(requestInterceptor);
11
- _cache.axiosInstance.interceptors.response.use(responseInterceptor);
7
+ const { Agent: AgentHttp } = await import('node:http');
8
+ const { Agent: AgentHttps } = await import('node:https');
9
+ const { agentWithIpValidation } = await import('./agent-with-ip-validation.js');
10
+ const httpAgent = agentWithIpValidation(new AgentHttp());
11
+ const httpsAgent = agentWithIpValidation(new AgentHttps());
12
+ _cache.axiosInstance = axios.create({ httpAgent, httpsAgent });
12
13
  }
13
14
  return _cache.axiosInstance;
14
15
  }
@@ -0,0 +1 @@
1
+ export declare function isDeniedIp(ip: string): boolean;
@@ -2,34 +2,32 @@ import { useEnv } from '@directus/env';
2
2
  import os from 'node:os';
3
3
  import { useLogger } from '../logger.js';
4
4
  import { ipInNetworks } from '../utils/ip-in-networks.js';
5
- const deniedError = (url) => new Error(`Requested URL "${url}" resolves to a denied IP address`);
6
- export function validateIp(ip, url) {
5
+ export function isDeniedIp(ip) {
7
6
  const env = useEnv();
8
7
  const logger = useLogger();
9
8
  const ipDenyList = env['IMPORT_IP_DENY_LIST'];
10
9
  if (ipDenyList.length === 0)
11
- return;
12
- let denied;
10
+ return false;
13
11
  try {
14
- denied = ipInNetworks(ip, ipDenyList);
12
+ const denied = ipInNetworks(ip, ipDenyList);
13
+ if (denied)
14
+ return true;
15
15
  }
16
16
  catch (error) {
17
- logger.warn(`Invalid "IMPORT_IP_DENY_LIST" configuration`);
17
+ logger.warn(`Cannot verify IP address due to invalid "IMPORT_IP_DENY_LIST" config`);
18
18
  logger.warn(error);
19
- throw deniedError(url);
19
+ return true;
20
20
  }
21
- if (denied)
22
- throw deniedError(url);
23
21
  if (ipDenyList.includes('0.0.0.0')) {
24
22
  const networkInterfaces = os.networkInterfaces();
25
23
  for (const networkInfo of Object.values(networkInterfaces)) {
26
24
  if (!networkInfo)
27
25
  continue;
28
26
  for (const info of networkInfo) {
29
- if (info.address === ip) {
30
- throw deniedError(url);
31
- }
27
+ if (info.address === ip)
28
+ return true;
32
29
  }
33
30
  }
34
31
  }
32
+ return false;
35
33
  }
@@ -3,12 +3,13 @@ import type { Accountability, RawField, SchemaOverview } from '@directus/types';
3
3
  import type Keyv from 'keyv';
4
4
  import type { Knex } from 'knex';
5
5
  import type { Helpers } from '../database/helpers/index.js';
6
- import type { AbstractServiceOptions, Collection, CollectionMeta, MutationOptions } from '../types/index.js';
6
+ import type { AbstractServiceOptions, Collection, MutationOptions } from '../types/index.js';
7
+ import { type BaseCollectionMeta } from '@directus/system-data';
7
8
  export type RawCollection = {
8
9
  collection: string;
9
10
  fields?: RawField[];
10
11
  schema?: Partial<Table> | null;
11
- meta?: Partial<CollectionMeta> | null;
12
+ meta?: Partial<BaseCollectionMeta> | null;
12
13
  };
13
14
  export declare class CollectionsService {
14
15
  knex: Knex;
@@ -7,12 +7,12 @@ import { clearSystemCache, getCache } from '../cache.js';
7
7
  import { ALIAS_TYPES } from '../constants.js';
8
8
  import { getHelpers } from '../database/helpers/index.js';
9
9
  import getDatabase, { getSchemaInspector } from '../database/index.js';
10
- import { systemCollectionRows } from '../database/system-data/collections/index.js';
11
10
  import emitter from '../emitter.js';
12
11
  import { getSchema } from '../utils/get-schema.js';
13
12
  import { shouldClearCache } from '../utils/should-clear-cache.js';
14
13
  import { FieldsService } from './fields.js';
15
14
  import { ItemsService } from './items.js';
15
+ import { systemCollectionRows } from '@directus/system-data';
16
16
  export class CollectionsService {
17
17
  knex;
18
18
  helpers;
@@ -7,7 +7,6 @@ import { ALIAS_TYPES } from '../constants.js';
7
7
  import { translateDatabaseError } from '../database/errors/translate.js';
8
8
  import { getHelpers } from '../database/helpers/index.js';
9
9
  import getDatabase, { getSchemaInspector } from '../database/index.js';
10
- import { systemFieldRows } from '../database/system-data/fields/index.js';
11
10
  import emitter from '../emitter.js';
12
11
  import { ForbiddenError, InvalidPayloadError } from '@directus/errors';
13
12
  import { ItemsService } from './items.js';
@@ -18,6 +17,8 @@ import { getSchema } from '../utils/get-schema.js';
18
17
  import { sanitizeColumn } from '../utils/sanitize-schema.js';
19
18
  import { shouldClearCache } from '../utils/should-clear-cache.js';
20
19
  import { RelationsService } from './relations.js';
20
+ import { getSystemFieldRowsWithAuthProviders } from '../utils/get-field-system-rows.js';
21
+ const systemFieldRows = getSystemFieldRowsWithAuthProviders();
21
22
  export class FieldsService {
22
23
  knex;
23
24
  helpers;
@@ -299,6 +300,12 @@ export class FieldsService {
299
300
  }
300
301
  const runPostColumnChange = await this.helpers.schema.preColumnChange();
301
302
  const nestedActionEvents = [];
303
+ // 'type' is required for further checks on schema update
304
+ if (field.schema && !field.type) {
305
+ const existingType = this.schema.collections[collection]?.fields[field.field]?.type;
306
+ if (existingType)
307
+ field.type = existingType;
308
+ }
302
309
  try {
303
310
  const hookAdjustedField = await emitter.emitFilter(`fields.update`, field, {
304
311
  keys: [field.field],
@@ -288,11 +288,12 @@ export class FilesService extends ItemsService {
288
288
  decompress: false,
289
289
  });
290
290
  }
291
- catch (err) {
292
- logger.warn(err, `Couldn't fetch file from URL "${importURL}"`);
291
+ catch (error) {
292
+ logger.warn(`Couldn't fetch file from URL "${importURL}"${error.message ? `: ${error.message}` : ''}`);
293
+ logger.trace(error);
293
294
  throw new ServiceUnavailableError({
294
295
  service: 'external-file',
295
- reason: `Couldn't fetch file from url "${importURL}"`,
296
+ reason: `Couldn't fetch file from URL "${importURL}"`,
296
297
  });
297
298
  }
298
299
  const parsedURL = url.parse(fileResponse.request.res.responseUrl);
@@ -40,6 +40,7 @@ import { GraphQLStringOrFloat } from './types/string-or-float.js';
40
40
  import { GraphQLVoid } from './types/void.js';
41
41
  import { addPathToValidationError } from './utils/add-path-to-validation-error.js';
42
42
  import processError from './utils/process-error.js';
43
+ import { isSystemCollection } from '@directus/system-data';
43
44
  const env = useEnv();
44
45
  const validationRules = Array.from(specifiedRules);
45
46
  if (env['GRAPHQL_INTROSPECTION'] === false) {
@@ -129,10 +130,10 @@ export class GraphQLService {
129
130
  const { ReadCollectionTypes, VersionCollectionTypes } = getReadableTypes();
130
131
  const { CreateCollectionTypes, UpdateCollectionTypes, DeleteCollectionTypes } = getWritableTypes();
131
132
  const scopeFilter = (collection) => {
132
- if (this.scope === 'items' && collection.collection.startsWith('directus_') === true)
133
+ if (this.scope === 'items' && isSystemCollection(collection.collection))
133
134
  return false;
134
135
  if (this.scope === 'system') {
135
- if (collection.collection.startsWith('directus_') === false)
136
+ if (isSystemCollection(collection.collection) === false)
136
137
  return false;
137
138
  if (SYSTEM_DENY_LIST.includes(collection.collection))
138
139
  return false;
@@ -1,12 +1,14 @@
1
1
  import { useEnv } from '@directus/env';
2
2
  import { ForbiddenError, InvalidPayloadError, ServiceUnavailableError, UnsupportedMediaTypeError, } from '@directus/errors';
3
+ import { isSystemCollection } from '@directus/system-data';
3
4
  import { parseJSON, toArray } from '@directus/utils';
5
+ import { createTmpFile } from '@directus/utils/node';
4
6
  import { queue } from 'async';
5
7
  import destroyStream from 'destroy';
6
8
  import { dump as toYAML } from 'js-yaml';
7
9
  import { parse as toXML } from 'js2xmlparser';
8
10
  import { Parser as CSVParser, transforms as CSVTransforms } from 'json2csv';
9
- import { createReadStream } from 'node:fs';
11
+ import { createReadStream, createWriteStream } from 'node:fs';
10
12
  import { appendFile } from 'node:fs/promises';
11
13
  import Papa from 'papaparse';
12
14
  import StreamArray from 'stream-json/streamers/StreamArray.js';
@@ -32,7 +34,7 @@ export class ImportService {
32
34
  this.schema = options.schema;
33
35
  }
34
36
  async import(collection, mimetype, stream) {
35
- if (this.accountability?.admin !== true && collection.startsWith('directus_'))
37
+ if (this.accountability?.admin !== true && isSystemCollection(collection))
36
38
  throw new ForbiddenError();
37
39
  const createPermissions = this.accountability?.permissions?.find((permission) => permission.collection === collection && permission.action === 'create');
38
40
  const updatePermissions = this.accountability?.permissions?.find((permission) => permission.collection === collection && permission.action === 'update');
@@ -85,7 +87,10 @@ export class ImportService {
85
87
  });
86
88
  });
87
89
  }
88
- importCSV(collection, stream) {
90
+ async importCSV(collection, stream) {
91
+ const tmpFile = await createTmpFile().catch(() => null);
92
+ if (!tmpFile)
93
+ throw new Error('Failed to create temporary file for import');
89
94
  const nestedActionEvents = [];
90
95
  return this.knex.transaction((trx) => {
91
96
  const service = new ItemsService(collection, {
@@ -115,35 +120,66 @@ export class ImportService {
115
120
  transform,
116
121
  };
117
122
  return new Promise((resolve, reject) => {
118
- stream
119
- .pipe(Papa.parse(Papa.NODE_STREAM_INPUT, PapaOptions))
120
- .on('data', (obj) => {
121
- // Filter out all undefined fields
122
- for (const field in obj) {
123
- if (obj[field] === undefined) {
124
- delete obj[field];
123
+ const streams = [stream];
124
+ const cleanup = (destroy = true) => {
125
+ if (destroy) {
126
+ for (const stream of streams) {
127
+ destroyStream(stream);
125
128
  }
126
129
  }
127
- saveQueue.push(obj);
128
- })
129
- .on('error', (err) => {
130
- destroyStream(stream);
131
- reject(new InvalidPayloadError({ reason: err.message }));
130
+ tmpFile.cleanup().catch(() => {
131
+ logger.warn(`Failed to cleanup temporary import file (${tmpFile.path})`);
132
+ });
133
+ };
134
+ saveQueue.error((error) => {
135
+ reject(error);
136
+ });
137
+ const fileWriteStream = createWriteStream(tmpFile.path)
138
+ .on('error', (error) => {
139
+ cleanup();
140
+ reject(new Error('Error while writing import data to temporary file', { cause: error }));
132
141
  })
133
- .on('end', () => {
134
- // In case of empty CSV file
135
- if (!saveQueue.started)
136
- return resolve();
137
- saveQueue.drain(() => {
138
- for (const nestedActionEvent of nestedActionEvents) {
139
- emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
142
+ .on('finish', () => {
143
+ const fileReadStream = createReadStream(tmpFile.path).on('error', (error) => {
144
+ cleanup();
145
+ reject(new Error('Error while reading import data from temporary file', { cause: error }));
146
+ });
147
+ streams.push(fileReadStream);
148
+ fileReadStream
149
+ .pipe(Papa.parse(Papa.NODE_STREAM_INPUT, PapaOptions))
150
+ .on('data', (obj) => {
151
+ // Filter out all undefined fields
152
+ for (const field in obj) {
153
+ if (obj[field] === undefined) {
154
+ delete obj[field];
155
+ }
140
156
  }
141
- return resolve();
157
+ saveQueue.push(obj);
158
+ })
159
+ .on('error', (error) => {
160
+ cleanup();
161
+ reject(new InvalidPayloadError({ reason: error.message }));
162
+ })
163
+ .on('end', () => {
164
+ cleanup(false);
165
+ // In case of empty CSV file
166
+ if (!saveQueue.started)
167
+ return resolve();
168
+ saveQueue.drain(() => {
169
+ for (const nestedActionEvent of nestedActionEvents) {
170
+ emitter.emitAction(nestedActionEvent.event, nestedActionEvent.meta, nestedActionEvent.context);
171
+ }
172
+ return resolve();
173
+ });
142
174
  });
143
175
  });
144
- saveQueue.error((err) => {
145
- reject(err);
146
- });
176
+ streams.push(fileWriteStream);
177
+ stream
178
+ .on('error', (error) => {
179
+ cleanup();
180
+ reject(new Error('Error while retrieving import data', { cause: error }));
181
+ })
182
+ .pipe(fileWriteStream);
147
183
  });
148
184
  });
149
185
  }
@@ -13,6 +13,7 @@ import { shouldClearCache } from '../utils/should-clear-cache.js';
13
13
  import { validateKeys } from '../utils/validate-keys.js';
14
14
  import { AuthorizationService } from './authorization.js';
15
15
  import { PayloadService } from './payload.js';
16
+ import { isSystemCollection } from '@directus/system-data';
16
17
  const env = useEnv();
17
18
  export class ItemsService {
18
19
  collection;
@@ -25,7 +26,7 @@ export class ItemsService {
25
26
  this.collection = collection;
26
27
  this.knex = options.knex || getDatabase();
27
28
  this.accountability = options.accountability || null;
28
- this.eventScope = this.collection.startsWith('directus_') ? this.collection.substring(9) : 'items';
29
+ this.eventScope = isSystemCollection(this.collection) ? this.collection.substring(9) : 'items';
29
30
  this.schema = options.schema;
30
31
  this.cache = getCache().cache;
31
32
  return this;
@@ -1,6 +1,6 @@
1
1
  import { ForbiddenError } from '@directus/errors';
2
2
  import { clearSystemCache, getCache } from '../cache.js';
3
- import { appAccessMinimalPermissions } from '../database/system-data/app-access-permissions/index.js';
3
+ import { appAccessMinimalPermissions } from '@directus/system-data';
4
4
  import { filterItems } from '../utils/filter-items.js';
5
5
  import { AuthorizationService } from './authorization.js';
6
6
  import { ItemsService } from './items.js';
@@ -4,12 +4,12 @@ import { toArray } from '@directus/utils';
4
4
  import { clearSystemCache, getCache } from '../cache.js';
5
5
  import { getHelpers } from '../database/helpers/index.js';
6
6
  import getDatabase, { getSchemaInspector } from '../database/index.js';
7
- import { systemRelationRows } from '../database/system-data/relations/index.js';
8
7
  import emitter from '../emitter.js';
9
8
  import { getDefaultIndexName } from '../utils/get-default-index-name.js';
10
9
  import { getSchema } from '../utils/get-schema.js';
11
10
  import { ItemsService } from './items.js';
12
11
  import { PermissionsService } from './permissions.js';
12
+ import { systemRelationRows } from '@directus/system-data';
13
13
  export class RelationsService {
14
14
  knex;
15
15
  permissionsService;
@@ -1,13 +1,18 @@
1
1
  import type { Query } from '@directus/types';
2
- import type { AbstractServiceOptions, MutationOptions, PrimaryKey } from '../types/index.js';
2
+ import type { AbstractServiceOptions, Item, MutationOptions, PrimaryKey } from '../types/index.js';
3
3
  import { ItemsService } from './items.js';
4
4
  export declare class RolesService extends ItemsService {
5
5
  constructor(options: AbstractServiceOptions);
6
6
  private checkForOtherAdminRoles;
7
7
  private checkForOtherAdminUsers;
8
- updateOne(key: PrimaryKey, data: Record<string, any>, opts?: MutationOptions): Promise<PrimaryKey>;
9
- updateBatch(data: Record<string, any>[], opts?: MutationOptions): Promise<PrimaryKey[]>;
10
- updateMany(keys: PrimaryKey[], data: Record<string, any>, opts?: MutationOptions): Promise<PrimaryKey[]>;
8
+ private isIpAccessValid;
9
+ private assertValidIpAccess;
10
+ createOne(data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey>;
11
+ createMany(data: Partial<Item>[], opts?: MutationOptions): Promise<PrimaryKey[]>;
12
+ updateOne(key: PrimaryKey, data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey>;
13
+ updateBatch(data: Partial<Item>[], opts?: MutationOptions): Promise<PrimaryKey[]>;
14
+ updateMany(keys: PrimaryKey[], data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey[]>;
15
+ updateByQuery(query: Query, data: Partial<Item>, opts?: MutationOptions | undefined): Promise<PrimaryKey[]>;
11
16
  deleteOne(key: PrimaryKey): Promise<PrimaryKey>;
12
17
  deleteMany(keys: PrimaryKey[]): Promise<PrimaryKey[]>;
13
18
  deleteByQuery(query: Query, opts?: MutationOptions): Promise<PrimaryKey[]>;