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

build(deps-dev): replace standard with neostandard #172

Merged
merged 3 commits into from
Dec 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/fastify/csrf-protection/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/csrf-protection/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/csrf-protection.svg?style=flat)](https://www.npmjs.com/package/@fastify/csrf-protection)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

This plugin helps developers protect their Fastify server against [CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) attacks.
In order to fully protect against CSRF, developers should study [Cross-Site Request Forgery Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html)
Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit",
"test:unit": "tap",
"test:typescript": "tsd"
Expand Down Expand Up @@ -40,9 +41,9 @@
"@fastify/session": "^11.0.0",
"@types/node": "^22.0.0",
"fastify": "^5.0.0",
"neostandard": "^0.11.9",
"proxyquire": "^2.1.3",
"sinon": "^19.0.2",
"standard": "^17.1.0",
"tap": "^18.7.2",
"tsd": "^0.31.0"
},
Expand Down
27 changes: 13 additions & 14 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types="node" />

import { FastifyPluginAsync, FastifyRequest } from 'fastify';
import { Options as CSRFOptions } from "@fastify/csrf";
import { CookieSerializeOptions as FastifyCookieSerializeOptions } from "@fastify/cookie";
import { FastifyPluginAsync, FastifyRequest } from 'fastify'
import { Options as CSRFOptions } from '@fastify/csrf'
import { CookieSerializeOptions as FastifyCookieSerializeOptions } from '@fastify/cookie'

declare module 'fastify' {
interface FastifyInstance {
Expand All @@ -20,12 +20,12 @@ declare module 'fastify' {
}
}

type FastifyCsrfProtection = FastifyPluginAsync<fastifyCsrfProtection.FastifyCsrfOptions>;
type FastifyCsrfProtection = FastifyPluginAsync<fastifyCsrfProtection.FastifyCsrfOptions>

declare namespace fastifyCsrfProtection {
export type CookieSerializeOptions = FastifyCookieSerializeOptions

export type GetTokenFn = (req: FastifyRequest) => string | void;
export type GetTokenFn = (req: FastifyRequest) => string | void

interface FastifyCsrfProtectionOptionsBase {
cookieKey?: string;
Expand All @@ -38,15 +38,15 @@ declare namespace fastifyCsrfProtection {
interface FastifyCsrfProtectionOptionsFastifyCookie {
sessionPlugin?: '@fastify/cookie';
csrfOpts?: | ({
[k in keyof CSRFOptions]: k extends "userInfo"
[k in keyof CSRFOptions]: k extends 'userInfo'
? true
: CSRFOptions[k];
} & Required<Pick<CSRFOptions, "hmacKey">>)
} & Required<Pick<CSRFOptions, 'hmacKey'>>)
| ({
[k in keyof CSRFOptions]: k extends "userInfo"
? false
: CSRFOptions[k];
});
[k in keyof CSRFOptions]: k extends 'userInfo'
? false
: CSRFOptions[k];
});
}

interface FastifyCsrfProtectionOptionsFastifySession {
Expand All @@ -68,12 +68,11 @@ declare namespace fastifyCsrfProtection {
/**
* @deprecated Use FastifyCsrfProtectionOptions instead
*/
export type FastifyCsrfOptions = FastifyCsrfProtectionOptions;
export type FastifyCsrfOptions = FastifyCsrfProtectionOptions

export const fastifyCsrfProtection: FastifyCsrfProtection
export { fastifyCsrfProtection as default }
}


declare function fastifyCsrfProtection(...params: Parameters<FastifyCsrfProtection>): ReturnType<FastifyCsrfProtection>
declare function fastifyCsrfProtection (...params: Parameters<FastifyCsrfProtection>): ReturnType<FastifyCsrfProtection>
export = fastifyCsrfProtection
16 changes: 8 additions & 8 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ declare module 'fastify' {
}
}

async function run() {
async function run () {
await fastify.register(FastifyCookie)
await fastify.register(FastifyCsrfProtection)

Expand All @@ -37,17 +37,17 @@ async function run() {

fastify.addHook('onRequest', fastify.csrfProtection)
}

run()

fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 'sha1', hmacKey: 'hmac' } })
expectError(fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 1 } }))

fastify.register(FastifySession)
fastify.register(FastifySession, { secret: 'a secret with minimum length of 32 characters' })
fastify.register(FastifyCsrfProtection, {
csrfOpts: {
hmacKey: '123'
},
getUserInfo(req) {
getUserInfo (req) {
const info = req.session.get('username')
if (info) {
return info
Expand All @@ -61,10 +61,10 @@ expectError(fastify.register(FastifyCsrfProtection, { getUserInfo: 'invalid' }))
fastify.register(FastifyCsrfProtection, { csrfOpts: { hmacKey: 'hmac' }, sessionPlugin: '@fastify/cookie' })
fastify.register(FastifyCsrfProtection, { csrfOpts: { hmacKey: 'hmac' } })
fastify.register(FastifyCsrfProtection, { })
fastify.register(FastifyCsrfProtection, { csrfOpts: { }})
expectError(fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie', csrfOpts: { userInfo: true}}))
fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie', csrfOpts: { userInfo: true, hmacKey: 'key'}})
fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie'})
fastify.register(FastifyCsrfProtection, { csrfOpts: { } })
expectError(fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie', csrfOpts: { userInfo: true } }))
fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie', csrfOpts: { userInfo: true, hmacKey: 'key' } })
fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/cookie' })
fastify.register(FastifyCsrfProtection, { csrfOpts: { }, sessionPlugin: '@fastify/session' })
fastify.register(FastifyCsrfProtection, { csrfOpts: { }, sessionPlugin: '@fastify/secure-session' })
fastify.register(FastifyCsrfProtection, { sessionPlugin: '@fastify/session' })
Expand Down
Loading