Skip to content

Commit

Permalink
Merge pull request #39 from Baroshem/feat/allowed-methods
Browse files Browse the repository at this point in the history
feat: add allowedMethodsRestricter
  • Loading branch information
Baroshem authored Oct 29, 2022
2 parents 1a076ab + c7d5120 commit c7c975c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Rate Limiter
- XSS Validator for both GET and POST requests
- CORS Handler similar to popular Express.js middleware
- Allowed HTTP Methods Restricter
- TypeScript support

[📖  Read the documentation](https://nuxt-security.vercel.app)
Expand Down
5 changes: 5 additions & 0 deletions docs/content/1.getting-started/2.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ModuleOptions {
rateLimiter: MiddlewareConfiguration<RateLimiter> | boolean;
xssValidator: MiddlewareConfiguration<XssValidator> | boolean;
corsHandler: MiddlewareConfiguration<CorsOptions> | boolean;
allowedMethodsRestricter: MiddlewareConfiguration<AllowedHTTPMethods> | boolean;
hidePoweredBy: boolean;
}
```
Expand Down Expand Up @@ -125,6 +126,10 @@ security: {
},
route: '',
},
allowedMethodsRestricter: {
value: '*',
route: '',
},
hidePoweredBy: true,
}
```
Expand Down
1 change: 1 addition & 0 deletions docs/content/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Security Module for Nuxt based on OWASP Top 10 and Helmet
- Rate Limiter
- XSS Validator for both GET and POST requests
- CORS Handler similar to popular Express.js middleware
- Allowed HTTP Methods Restricter
- TypeScript support
::
::
32 changes: 32 additions & 0 deletions docs/content/2.middlewares/6.allowed-methods-restricter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Allowed Methods Restricter
description: ''
---

This middleware works by default for `*` HTTP Methods and will throw an `405 Method Not Allowed` error when the there will be a request sent with an HTTP Method that is not allowed.

It will help you solve [this](https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html#restrict-http-methods) security problem.

```ts
export type HTTPMethod = 'GET' | 'POST' | 'DELETE' | 'PATCH' | 'POST' | string;

export type AllowedHTTPMethods = HTTPMethod[] | '*'
```
To write a custom logic for this middleware follow this pattern:
```javascript
// nuxt.config.js

{
modules: [
"nuxt-security",
],
security: {
allowedMethodsRestricter: {
value: ['POST'],
route: '/my-custom-route'
}
}
}
```
4 changes: 4 additions & 0 deletions src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,9 @@ export const defaultSecurityConfig: ModuleOptions = {
},
...defaultMiddlewareRoute,
},
allowedMethodsRestricter: {
value: '*',
...defaultMiddlewareRoute,
},
hidePoweredBy: true,
};
8 changes: 7 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'path'
import { fileURLToPath } from 'url'
import { defineNuxtModule, addServerHandler } from '@nuxt/kit'
import defu from 'defu'
import { MiddlewareConfiguration, ModuleOptions, RateLimiter, RequestSizeLimiter, SecurityHeaders, XssValidator } from './types'
import { AllowedHTTPMethods, MiddlewareConfiguration, ModuleOptions, RateLimiter, RequestSizeLimiter, SecurityHeaders, XssValidator } from './types'
import { defaultSecurityConfig } from './defaultConfig'
import { SECURITY_HEADER_NAMES } from './headers'
import { Nuxt, NuxtOptions, RuntimeConfig } from '@nuxt/schema'
Expand Down Expand Up @@ -71,5 +71,11 @@ export default defineNuxtModule<ModuleOptions>({
if (corsHandlerConfig) {
addServerHandler({ route: (corsHandlerConfig as MiddlewareConfiguration<CorsOptions>).route, handler: resolve(runtimeDir, 'server/middleware/corsHandler') })
}

// Register allowedMethodsRestricter middleware with that will by default allow all methods
const allowedMethodsRestricterConfig = nuxt.options.security.allowedMethodsRestricter as MiddlewareConfiguration<AllowedHTTPMethods>
if (allowedMethodsRestricterConfig && allowedMethodsRestricterConfig.value !== '*') {
addServerHandler({ route: allowedMethodsRestricterConfig.route, handler: resolve(runtimeDir, 'server/middleware/allowedMethodsRestricter') })
}
}
})
11 changes: 11 additions & 0 deletions src/runtime/server/middleware/allowedMethodsRestricter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineEventHandler, createError } from 'h3'
import { useRuntimeConfig } from '#imports'

const securityConfig = useRuntimeConfig().security

export default defineEventHandler((event) => {
const allowedMethods: string[] = securityConfig.allowedMethodsRestricter.value
if (!allowedMethods.includes(event.req.method!!)) {
throw createError({ statusCode: 405, statusMessage: 'Method not allowed' })
}
})
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export type XssValidator = {
css: Record<string, any> | boolean;
} | {};

export type HTTPMethod = 'GET' | 'POST' | 'DELETE' | 'PATCH' | 'POST' | string;

export type AllowedHTTPMethods = HTTPMethod[] | '*'

export type MiddlewareConfiguration<MIDDLEWARE> = {
value: MIDDLEWARE;
route: string;
Expand Down Expand Up @@ -45,5 +49,6 @@ export interface ModuleOptions {
rateLimiter: MiddlewareConfiguration<RateLimiter> | boolean;
xssValidator: MiddlewareConfiguration<XssValidator> | boolean;
corsHandler: MiddlewareConfiguration<CorsOptions> | boolean;
allowedMethodsRestricter: MiddlewareConfiguration<AllowedHTTPMethods> | boolean;
hidePoweredBy: boolean;
}

1 comment on commit c7c975c

@vercel
Copy link

@vercel vercel bot commented on c7c975c Oct 29, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nuxt-security – ./

nuxt-security.vercel.app
nuxt-security-baroshem.vercel.app
nuxt-security-git-main-baroshem.vercel.app

Please sign in to comment.