-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Baroshem/feat/allowed-methods
feat: add allowedMethodsRestricter
- Loading branch information
Showing
8 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
docs/content/2.middlewares/6.allowed-methods-restricter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c7c975c
There was a problem hiding this comment.
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