Installations
npm install koa-csrf
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 14
Node Version
16.15.1
NPM Version
8.11.0
Score
92.8
Supply Chain
98.6
Quality
82.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (97.31%)
Shell (2.69%)
Developer
koajs
Download Statistics
Total Downloads
12,915,946
Last Day
690
Last Week
10,686
Last Month
77,094
Last Year
1,842,786
GitHub Statistics
264 Stars
103 Commits
31 Forks
10 Watching
2 Branches
22 Contributors
Package Meta Information
Latest Version
5.0.1
Package Id
koa-csrf@5.0.1
Unpacked Size
7.71 kB
Size
3.31 kB
File Count
4
NPM Version
8.11.0
Node Version
16.15.1
Total Downloads
Cumulative downloads
Total Downloads
12,915,946
Last day
-79.1%
690
Compared to previous day
Last week
-51.2%
10,686
Compared to previous week
Last month
-29.4%
77,094
Compared to previous month
Last year
-33.8%
1,842,786
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
koa-csrf
CSRF tokens for Koa
NOTE: As of v5.0.0+
ctx.csrf
,ctx_csrf
, andctx.response.csrf
are removed – instead usectx.state._csrf
. Furthermore we have droppedinvalidTokenMessage
andinvalidTokenStatusCode
in favor of anerrorHandler
function option.
Table of Contents
Install
npm:
1npm install koa-csrf
Usage
-
Add middleware in Koa app (see options below):
1const Koa = require('koa'); 2const bodyParser = require('koa-bodyparser'); 3const session = require('koa-generic-session'); 4const convert = require('koa-convert'); 5const CSRF = require('koa-csrf'); 6 7const app = new Koa(); 8 9// set the session keys 10app.keys = [ 'a', 'b' ]; 11 12// add session support 13app.use(convert(session())); 14 15// add body parsing 16app.use(bodyParser()); 17 18// add the CSRF middleware 19app.use(new CSRF()); 20 21// your middleware here (e.g. parse a form submit) 22app.use((ctx, next) => { 23 if (![ 'GET', 'POST' ].includes(ctx.method)) 24 return next(); 25 if (ctx.method === 'GET') { 26 ctx.body = ctx.state._csrf; 27 return; 28 } 29 ctx.body = 'OK'; 30}); 31 32app.listen();
-
Add the CSRF token in your template forms:
Jade Template:
1form(action='/register', method='POST') 2 input(type='hidden', name='_csrf', value=_csrf) 3 input(type='email', name='email', placeholder='Email') 4 input(type='password', name='password', placeholder='Password') 5 button(type='submit') Register
EJS Template:
1<form action="/register" method="POST"> 2 <input type="hidden" name="_csrf" value="<%= _csrf %>" /> 3 <input type="email" name="email" placeholder="Email" /> 4 <input type="password" name="password" placeholder="Password" /> 5 <button type="submit">Register</button> 6</form>
Options
errorHandler
(Function) - defaults to a function that returnsctx.throw(403, 'Invalid CSRF token')
excludedMethods
(Array) - defaults to[ 'GET', 'HEAD', 'OPTIONS' ]
disableQuery
(Boolean) - defaults tofalse
ignoredPathGlobs
(Array) - defaults to an empty Array, but you can pass an Array of glob paths to ignore
Contributors
Name | Website |
---|---|
Nick Baugh | https://github.com/niftylettuce |
Imed Jaberi | https://www.3imed-jaberi.com/ |
License
MIT © Jonathan Ong
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/koajs/.github/SECURITY.md:1
- Info: Found linked content: github.com/koajs/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/koajs/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/koajs/.github/SECURITY.md:1
Reason
Found 2/13 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/koajs/csrf/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/koajs/csrf/ci.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:25
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Score
4.6
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More