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

Clean-up middleware #22325

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions api/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ vi.mock('./flows', () => ({
}));

vi.mock('./middleware/check-ip', () => ({
checkIP: Router(),
checkIp: Router(),
}));

vi.mock('./middleware/schema', () => ({
default: Router(),
schema: Router(),
}));

vi.mock('./middleware/get-permissions', () => ({
default: Router(),
getPermissions: Router(),
}));

vi.mock('./auth', () => ({
Expand Down
30 changes: 15 additions & 15 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import fieldsRouter from './controllers/fields.js';
import filesRouter from './controllers/files.js';
import flowsRouter from './controllers/flows.js';
import foldersRouter from './controllers/folders.js';
import graphqlRouter from './controllers/graphql.js';
import itemsRouter from './controllers/items.js';
import graphqlRouter from './controllers/graphql/index.js';
import itemsRouter from './controllers/items/index.js';
import notFoundHandler from './controllers/not-found.js';
import notificationsRouter from './controllers/notifications.js';
import operationsRouter from './controllers/operations.js';
Expand Down Expand Up @@ -51,17 +51,17 @@ import emitter from './emitter.js';
import { getExtensionManager } from './extensions/index.js';
import { getFlowManager } from './flows.js';
import { createExpressLogger, useLogger } from './logger.js';
import authenticate from './middleware/authenticate.js';
import cache from './middleware/cache.js';
import { checkIP } from './middleware/check-ip.js';
import cors from './middleware/cors.js';
import errorHandler from './middleware/error-handler.js';
import extractToken from './middleware/extract-token.js';
import getPermissions from './middleware/get-permissions.js';
import rateLimiterGlobal from './middleware/rate-limiter-global.js';
import rateLimiter from './middleware/rate-limiter-ip.js';
import sanitizeQuery from './middleware/sanitize-query.js';
import schema from './middleware/schema.js';
import { authenticate } from './middleware/authenticate.js';
import { cache } from './middleware/cache.js';
import { checkIp } from './middleware/check-ip.js';
import { cors } from './middleware/cors.js';
import { errorHandler } from './middleware/error-handler.js';
import { extractToken } from './middleware/extract-token.js';
import { getPermissions } from './middleware/get-permissions.js';
import { rateLimiterGlobal } from './middleware/rate-limiter-global.js';
import { rateLimiterIp } from './middleware/rate-limiter-ip.js';
import { sanitizeQuery } from './middleware/sanitize-query.js';
import { schema } from './middleware/schema.js';
import { initTelemetry } from './telemetry/index.js';
import { getConfigFromEnv } from './utils/get-config-from-env.js';
import { Url } from './utils/url.js';
Expand Down Expand Up @@ -252,14 +252,14 @@ export default async function createApp(): Promise<express.Application> {
}

if (env['RATE_LIMITER_ENABLED'] === true) {
app.use(rateLimiter);
app.use(rateLimiterIp);
}

app.get('/server/ping', (_req, res) => res.send('pong'));

app.use(authenticate);

app.use(checkIP);
app.use(checkIp);

app.use(sanitizeQuery);

Expand Down
4 changes: 2 additions & 2 deletions api/src/auth/drivers/ldap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import { Router } from 'express';
import Joi from 'joi';
import type { Client, Error, LDAPResult, SearchCallbackResponse, SearchEntry } from 'ldapjs';
import ldap from 'ldapjs';
import { REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../../constants.js';
import { respond } from '../../controllers/handlers/respond.js';
import getDatabase from '../../database/index.js';
import emitter from '../../emitter.js';
import { useLogger } from '../../logger.js';
import { respond } from '../../middleware/respond.js';
import { AuthenticationService } from '../../services/authentication.js';
import { UsersService } from '../../services/users.js';
import type { AuthDriverOptions, AuthenticationMode, User } from '../../types/index.js';
import asyncHandler from '../../utils/async-handler.js';
import { getIPFromReq } from '../../utils/get-ip-from-req.js';
import { AuthDriver } from '../auth.js';
import { REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../../constants.js';

interface UserInfo {
dn: string;
Expand Down
4 changes: 2 additions & 2 deletions api/src/auth/drivers/local.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEnv } from '@directus/env';
import { InvalidCredentialsError, InvalidPayloadError } from '@directus/errors';
import type { Accountability } from '@directus/types';
import argon2 from 'argon2';
import { Router } from 'express';
import Joi from 'joi';
import { performance } from 'perf_hooks';
import { REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../../constants.js';
import { useEnv } from '@directus/env';
import { respond } from '../../middleware/respond.js';
import { respond } from '../../controllers/handlers/respond.js';
import { AuthenticationService } from '../../services/authentication.js';
import type { AuthenticationMode, User } from '../../types/index.js';
import asyncHandler from '../../utils/async-handler.js';
Expand Down
2 changes: 1 addition & 1 deletion api/src/auth/drivers/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import type { Client } from 'openid-client';
import { errors, generators, Issuer } from 'openid-client';
import { getAuthProvider } from '../../auth.js';
import { REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../../constants.js';
import { respond } from '../../controllers/handlers/respond.js';
import getDatabase from '../../database/index.js';
import emitter from '../../emitter.js';
import { useLogger } from '../../logger.js';
import { respond } from '../../middleware/respond.js';
import { AuthenticationService } from '../../services/authentication.js';
import { UsersService } from '../../services/users.js';
import type { AuthData, AuthDriverOptions, User } from '../../types/index.js';
Expand Down
2 changes: 1 addition & 1 deletion api/src/auth/drivers/openid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import type { Client } from 'openid-client';
import { errors, generators, Issuer } from 'openid-client';
import { getAuthProvider } from '../../auth.js';
import { REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../../constants.js';
import { respond } from '../../controllers/handlers/respond.js';
import getDatabase from '../../database/index.js';
import emitter from '../../emitter.js';
import { useLogger } from '../../logger.js';
import { respond } from '../../middleware/respond.js';
import { AuthenticationService } from '../../services/authentication.js';
import { UsersService } from '../../services/users.js';
import type { AuthData, AuthDriverOptions, User } from '../../types/index.js';
Expand Down
4 changes: 2 additions & 2 deletions api/src/auth/drivers/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import express, { Router } from 'express';
import * as samlify from 'samlify';
import { getAuthProvider } from '../../auth.js';
import { REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../../constants.js';
import { respond } from '../../controllers/handlers/respond.js';
import getDatabase from '../../database/index.js';
import emitter from '../../emitter.js';
import { useLogger } from '../../logger.js';
import { respond } from '../../middleware/respond.js';
import { AuthenticationService } from '../../services/authentication.js';
import { UsersService } from '../../services/users.js';
import type { AuthDriverOptions, User } from '../../types/index.js';
import asyncHandler from '../../utils/async-handler.js';
import { getConfigFromEnv } from '../../utils/get-config-from-env.js';
import { LocalAuthDriver } from './local.js';
import { isLoginRedirectAllowed } from '../../utils/is-login-redirect-allowed.js';
import { LocalAuthDriver } from './local.js';

// Register the samlify schema validator
samlify.setSchemaValidator(validator);
Expand Down
9 changes: 4 additions & 5 deletions api/src/controllers/activity.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Action } from '@directus/constants';
import { isDirectusError } from '@directus/errors';
import { ErrorCode, ForbiddenError, InvalidPayloadError, isDirectusError } from '@directus/errors';
import express from 'express';
import Joi from 'joi';
import { ErrorCode, ForbiddenError, InvalidPayloadError } from '@directus/errors';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { validateBatch } from '../middleware/validate-batch.js';
import { ActivityService } from '../services/activity.js';
import { MetaService } from '../services/meta.js';
import asyncHandler from '../utils/async-handler.js';
import { getIPFromReq } from '../utils/get-ip-from-req.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';
import { validateBatch } from './handlers/validate-batch.js';

const router = express.Router();

Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { merge, pick } from 'lodash-es';
import { ASSET_TRANSFORM_QUERY_KEYS, SYSTEM_ASSET_ALLOW_LIST } from '../constants.js';
import getDatabase from '../database/index.js';
import { useLogger } from '../logger.js';
import useCollection from '../middleware/use-collection.js';
import { AssetsService } from '../services/assets.js';
import { PayloadService } from '../services/payload.js';
import type { TransformationFormat, TransformationParams } from '../types/assets.js';
Expand All @@ -17,6 +16,7 @@ import asyncHandler from '../utils/async-handler.js';
import { getCacheControlHeader } from '../utils/get-cache-headers.js';
import { getConfigFromEnv } from '../utils/get-config-from-env.js';
import { getMilliseconds } from '../utils/get-milliseconds.js';
import { useCollection } from './handlers/use-collection.js';

const router = Router();

Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from '../auth/drivers/index.js';
import { DEFAULT_AUTH_PROVIDER, REFRESH_COOKIE_OPTIONS, SESSION_COOKIE_OPTIONS } from '../constants.js';
import { useLogger } from '../logger.js';
import { respond } from '../middleware/respond.js';
import { AuthenticationService } from '../services/authentication.js';
import { UsersService } from '../services/users.js';
import type { AuthenticationMode } from '../types/auth.js';
Expand All @@ -22,6 +21,7 @@ import { getIPFromReq } from '../utils/get-ip-from-req.js';
import { getSecret } from '../utils/get-secret.js';
import isDirectusJWT from '../utils/is-directus-jwt.js';
import { verifyAccessJWT } from '../utils/jwt.js';
import { respond } from './handlers/respond.js';

const router = Router();
const env = useEnv();
Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/collections.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ErrorCode, isDirectusError } from '@directus/errors';
import type { Item } from '@directus/types';
import { Router } from 'express';
import { respond } from '../middleware/respond.js';
import { validateBatch } from '../middleware/validate-batch.js';
import { CollectionsService } from '../services/collections.js';
import { MetaService } from '../services/meta.js';
import asyncHandler from '../utils/async-handler.js';
import { respond } from './handlers/respond.js';
import { validateBatch } from './handlers/validate-batch.js';

const router = Router();

Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/dashboards.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ErrorCode, isDirectusError } from '@directus/errors';
import type { PrimaryKey } from '@directus/types';
import express from 'express';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { validateBatch } from '../middleware/validate-batch.js';
import { DashboardsService } from '../services/dashboards.js';
import { MetaService } from '../services/meta.js';
import asyncHandler from '../utils/async-handler.js';
import { sanitizeQuery } from '../utils/sanitize-query.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';
import { validateBatch } from './handlers/validate-batch.js';

const router = express.Router();

Expand Down
4 changes: 2 additions & 2 deletions api/src/controllers/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import express from 'express';
import { isNil } from 'lodash-es';
import { UUID_REGEX } from '../constants.js';
import { getExtensionManager } from '../extensions/index.js';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { ExtensionReadError, ExtensionsService } from '../services/extensions.js';
import asyncHandler from '../utils/async-handler.js';
import { getCacheControlHeader } from '../utils/get-cache-headers.js';
import { getMilliseconds } from '../utils/get-milliseconds.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';

const router = express.Router();
const env = useEnv();
Expand Down
21 changes: 10 additions & 11 deletions api/src/controllers/fields.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { TYPES } from '@directus/constants';
import { isDirectusError } from '@directus/errors';
import { ErrorCode, InvalidPayloadError, isDirectusError } from '@directus/errors';
import type { Field, RawField, Type } from '@directus/types';
import { Router } from 'express';
import Joi from 'joi';
import { ALIAS_TYPES } from '../constants.js';
import { ErrorCode, InvalidPayloadError } from '@directus/errors';
import validateCollection from '../middleware/collection-exists.js';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { FieldsService } from '../services/fields.js';
import asyncHandler from '../utils/async-handler.js';
import { collectionExists } from './handlers/collection-exists.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';

const router = Router();

Expand All @@ -33,7 +32,7 @@ router.get(

router.get(
'/:collection',
validateCollection,
collectionExists,
asyncHandler(async (req, res, next) => {
const service = new FieldsService({
accountability: req.accountability,
Expand All @@ -50,7 +49,7 @@ router.get(

router.get(
'/:collection/:field',
validateCollection,
collectionExists,
asyncHandler(async (req, res, next) => {
const service = new FieldsService({
accountability: req.accountability,
Expand Down Expand Up @@ -84,7 +83,7 @@ const newFieldSchema = Joi.object({

router.post(
'/:collection',
validateCollection,
collectionExists,
asyncHandler(async (req, res, next) => {
const service = new FieldsService({
accountability: req.accountability,
Expand Down Expand Up @@ -119,7 +118,7 @@ router.post(

router.patch(
'/:collection',
validateCollection,
collectionExists,
asyncHandler(async (req, res, next) => {
const service = new FieldsService({
accountability: req.accountability,
Expand Down Expand Up @@ -169,7 +168,7 @@ const updateSchema = Joi.object({

router.patch(
'/:collection/:field',
validateCollection,
collectionExists,
asyncHandler(async (req, res, next) => {
const service = new FieldsService({
accountability: req.accountability,
Expand Down Expand Up @@ -206,7 +205,7 @@ router.patch(

router.delete(
'/:collection/:field',
validateCollection,
collectionExists,
asyncHandler(async (req, _res, next) => {
const service = new FieldsService({
accountability: req.accountability,
Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import express from 'express';
import Joi from 'joi';
import { minimatch } from 'minimatch';
import path from 'path';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { validateBatch } from '../middleware/validate-batch.js';
import { FilesService } from '../services/files.js';
import { MetaService } from '../services/meta.js';
import asyncHandler from '../utils/async-handler.js';
import { sanitizeQuery } from '../utils/sanitize-query.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';
import { validateBatch } from './handlers/validate-batch.js';

const router = express.Router();
const env = useEnv();
Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import type { PrimaryKey } from '@directus/types';
import express from 'express';
import { UUID_REGEX } from '../constants.js';
import { getFlowManager } from '../flows.js';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { validateBatch } from '../middleware/validate-batch.js';
import { FlowsService } from '../services/flows.js';
import { MetaService } from '../services/meta.js';
import asyncHandler from '../utils/async-handler.js';
import { sanitizeQuery } from '../utils/sanitize-query.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';
import { validateBatch } from './handlers/validate-batch.js';

const router = express.Router();

Expand Down
6 changes: 3 additions & 3 deletions api/src/controllers/folders.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ErrorCode, isDirectusError } from '@directus/errors';
import type { PrimaryKey } from '@directus/types';
import express from 'express';
import { respond } from '../middleware/respond.js';
import useCollection from '../middleware/use-collection.js';
import { validateBatch } from '../middleware/validate-batch.js';
import { FoldersService } from '../services/folders.js';
import { MetaService } from '../services/meta.js';
import asyncHandler from '../utils/async-handler.js';
import { sanitizeQuery } from '../utils/sanitize-query.js';
import { respond } from './handlers/respond.js';
import { useCollection } from './handlers/use-collection.js';
import { validateBatch } from './handlers/validate-batch.js';

const router = express.Router();

Expand Down