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

feat: #114 migrate cors #119

Merged
merged 1 commit into from Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions docs/content/2.security/5.cors-handler.md
Expand Up @@ -3,16 +3,12 @@ title: CORS Handler
description: ''
---

::alert{type="info"}
This functionality will change in the next major release as h3-cors package was merged to core H3 package. The interface will most probably change but a proper migration process is being developed.
::

This middleware will help you set your CORS options. Based on <https://github.com/NozomuIkuta/h3-cors>
This middleware will help you set your CORS options and it is based on built in [H3 CORS](https://github.com/unjs/h3) functionality

This middleware will help you solve [this](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) security problem.

```ts
export interface CorsOptions {
export interface H3CorsOptions {
origin?: '*' | 'null' | (string | RegExp)[] | ((origin: string) => boolean);
methods?: '*' | HTTPMethod[];
allowHeaders?: '*' | string[];
Expand All @@ -34,7 +30,6 @@ export default defineNuxtConfig({
value: {
origin: '*',
methods: '*',

},
route: '/my-custom-route'
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -45,7 +45,6 @@
"stackblitz": "cd .stackblitz && yarn && yarn dev"
},
"dependencies": {
"@nozomuikuta/h3-cors": "^0.1.5",
"@nuxt/kit": "^3.2.2",
"basic-auth": "^2.0.1",
"defu": "^6.1.1",
Expand Down
1 change: 0 additions & 1 deletion src/defaultConfig.ts
Expand Up @@ -97,7 +97,6 @@ export const defaultSecurityConfig: ModuleOptions = {
...defaultMiddlewareRoute,
...defaultThrowErrorValue,
},
// TODO: migrate to native H3 CORS and rename this to `cors`
corsHandler: {
// Options by CORS middleware for Express https://github.com/expressjs/cors#configuration-options
value: {
Expand Down
6 changes: 2 additions & 4 deletions src/module.ts
Expand Up @@ -3,7 +3,7 @@ import { resolve, normalize } from 'pathe'
import { defineNuxtModule, addServerHandler, installModule } from '@nuxt/kit'
import defu, { createDefu } from 'defu'
import { RuntimeConfig } from '@nuxt/schema'
import { CorsOptions } from '@nozomuikuta/h3-cors'
import { H3CorsOptions } from 'h3'
import {
AllowedHTTPMethods,
BasicAuth,
Expand Down Expand Up @@ -127,13 +127,11 @@ export default defineNuxtModule<ModuleOptions>({
})
}

// TODO: refactor when migrating to H3 native cors support
// Register corsHandler middleware with default config that will add CORS setup
// Based on '@nozomuikuta/h3-cors' package
const corsHandlerConfig = nuxt.options.security.corsHandler
if (corsHandlerConfig) {
addServerHandler({
route: (corsHandlerConfig as MiddlewareConfiguration<CorsOptions>)
route: (corsHandlerConfig as MiddlewareConfiguration<H3CorsOptions>)
.route,
handler: normalize(
resolve(runtimeDir, 'server/middleware/corsHandler')
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/server/middleware/corsHandler.ts
@@ -1,8 +1,8 @@
// TODO: remove when migrating to native H3 cors functionality
import { defineEventHandler } from 'h3'
import { defineCorsEventHandler } from '@nozomuikuta/h3-cors'
import { defineEventHandler, handleCors } from 'h3'
import { useRuntimeConfig } from '#imports'

const securityConfig = useRuntimeConfig().security

export default defineEventHandler(defineCorsEventHandler(securityConfig.corsHandler.value))
export default defineEventHandler((event) => {
handleCors(event, securityConfig.corsHandler.value)
})
9 changes: 2 additions & 7 deletions src/types.ts
@@ -1,5 +1,4 @@
// TODO: remove when migrating to native H3 cors functionality
import { CorsOptions } from '@nozomuikuta/h3-cors'
import { H3CorsOptions } from 'h3'
import { ModuleOptions as CsrfOptions } from 'nuxt-csurf'

export type RequestSizeLimiter = {
Expand Down Expand Up @@ -155,11 +154,7 @@ export interface ModuleOptions {
requestSizeLimiter: MiddlewareConfiguration<RequestSizeLimiter> | false;
rateLimiter: MiddlewareConfiguration<RateLimiter> | false;
xssValidator: MiddlewareConfiguration<XssValidator> | false;
/**
* @deprecated `h3-cors` package was merged to core H3. Migrate to a new interface and functionality and change the name to `cors`.
* This works for now, but will be changed with the next major release.
*/
corsHandler: MiddlewareConfiguration<CorsOptions> | false;
corsHandler: MiddlewareConfiguration<H3CorsOptions> | false;
allowedMethodsRestricter: MiddlewareConfiguration<AllowedHTTPMethods> | false;
hidePoweredBy: boolean;
basicAuth: MiddlewareConfiguration<BasicAuth> | boolean;
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Expand Up @@ -761,11 +761,6 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@nozomuikuta/h3-cors@^0.1.5":
version "0.1.8"
resolved "https://registry.npmjs.org/@nozomuikuta/h3-cors/-/h3-cors-0.1.8.tgz"
integrity sha512-qWe2mUfnjxaBwTYKNM/9GrGK0nciyynn5Lk/sv8wYpKbloaoJFB98onBWMTotV5e/l8CCoelsr1oz4qYb7FdHg==

"@nuxt/devalue@^2.0.0":
version "2.0.0"
resolved "https://registry.npmjs.org/@nuxt/devalue/-/devalue-2.0.0.tgz"
Expand Down