{"id":19621302,"url":"https://github.com/commenthol/signed-token-csrf","last_synced_at":"2026-06-08T22:32:12.541Z","repository":{"id":57359445,"uuid":"97367461","full_name":"commenthol/signed-token-csrf","owner":"commenthol","description":"CSRF connect middleware using signed tokens","archived":false,"fork":false,"pushed_at":"2025-08-01T07:31:27.000Z","size":83,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-03T02:51:55.064Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/commenthol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-16T07:26:35.000Z","updated_at":"2025-08-01T07:31:32.000Z","dependencies_parsed_at":"2023-01-21T14:15:50.608Z","dependency_job_id":null,"html_url":"https://github.com/commenthol/signed-token-csrf","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/commenthol/signed-token-csrf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fsigned-token-csrf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fsigned-token-csrf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fsigned-token-csrf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fsigned-token-csrf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/commenthol","download_url":"https://codeload.github.com/commenthol/signed-token-csrf/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fsigned-token-csrf/sbom","scorecard":{"id":300845,"data":{"date":"2025-08-11","repo":{"name":"github.com/commenthol/signed-token-csrf","commit":"783a89064d46414f549441c1963d0163ae1f240c"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":4.1,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":0,"reason":"Found 0/23 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":6,"reason":"8 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 6","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":0,"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"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":2,"reason":"dependency not pinned by hash detected -- score normalized to 2","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/commenthol/signed-token-csrf/ci.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/commenthol/signed-token-csrf/ci.yml/master?enable=pin","Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:29","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned","Info:   1 out of   2 npmCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: UNLICENSE:0","Info: FSF or OSI recognized license: The Unlicense: UNLICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 8 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-17T20:35:16.569Z","repository_id":57359445,"created_at":"2025-08-17T20:35:16.569Z","updated_at":"2025-08-17T20:35:16.569Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34083848,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T11:22:18.216Z","updated_at":"2026-06-08T22:32:12.534Z","avatar_url":"https://github.com/commenthol.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# signed-token-csrf\n\n\u003e CSRF connect middlewares using signed tokens\n\n[![NPM version](https://badge.fury.io/js/signed-token-csrf.svg)](https://www.npmjs.com/package/signed-token-csrf/)\n[![Build Status](https://github.com/commenthol/signed-token-csrf/actions/workflows/ci.yml/badge.svg)](https://github.com/commenthol/signed-token-csrf/actions/workflows/ci.yml)\n\n\u003c!-- !toc (minlevel=2) --\u003e\n\n- [API](#api)\n  - [new Csrf(secret, [opts])](#new-csrfsecret-opts)\n  - [create](#create)\n  - [verify](#verify)\n  - [verifyXhr](#verifyxhr)\n  - [csrf](#csrf)\n- [Example](#example)\n  - [Forms](#forms)\n  - [XHR](#xhr)\n- [Installation](#installation)\n- [Tests](#tests)\n- [License](#license)\n- [References](#references)\n\n\u003c!-- toc! --\u003e\n\n## API\n\n### new Csrf(secret, [opts])\n\n**Parameters**\n\n| parameter              | type                | description                                                                                          |\n| ---------------------- | ------------------- | ---------------------------------------------------------------------------------------------------- |\n| `secret`               | string              | a server side secret                                                                                 |\n| `[opts]`               | Object              | _optional:_ options                                                                                  |\n| `[opts.name=csrf]`     | string              | _optional:_ header \u0026amp; cookie name of token                                                        |\n| `[opts.cookie]`        | Object              | _optional:_ cookie options - defaults to `{path: '/', httpOnly: true, secure: true, sameSite: true}` |\n| `[opts.token]`         | Object              | _optional:_ signedToken options - defaults to `{digest: 'sha256', commonlen: 24, tokenlen: 48}`      |\n| `[opts.ignoreMethods]` | Array\u0026lt;string\u0026gt; | _optional:_ ignore methods `['HEAD', 'OPTIONS']`                                                     |\n| `[opts.host]`          | string              | _optional:_ hostname of service to check against                                                     |\n\n### create\n\nConnect middleware `(req, res, next) =\u003e {}`\n\nCreates method `req.csrfToken()` to get CSRF token as well as the secret for\nsigning in `req.session.csrf` if available or sets a csrf cookie.\nName of session key and cookie name can be changed via opts.name.\nDefault name is `csrf`.\n\n**NOTE:** If used together with `verifyXhr()` only set CSRF Cookie on successful login!\n\n### verify\n\nConnect middleware `(req, res, next) =\u003e {}`\n\nObtains a token from a request using either `req.body.csrf`, `req.query.csrf` or `req.headers['x-csrf-token']` and verifies it with the secret from `req.session.csrf` if available or from the `csrf` cookie.\n`body-parser` is required to obtain the token from the request body.\nName of session key and cookie name can be changed via `opts.name`\n\n### verifyXhr\n\nConnect middleware `(req, res, next) =\u003e {}`\n\nVerifies the cookie only; For token based xhr requests.\nSee [Your API-Centric Web App Is Probably Not Safe Against XSS and CSRF][].\n\n**NOTE:** Only set CSRF Cookie with `create()` on successful login!\n\n### csrf\n\nConnect middleware `(req, res, next) =\u003e {}` which chains `create` and `verify`.\n\n## Example\n\n### Forms\n\nSee `./example/app.js`. Run with `node exampe/app.js` and open http://localhost:3000 in browser.\n\n```js\nconst Csrf = require('signed-token-csrf')\nconst bodyParser = require('body-parser')\nconst session = require('express-session')\nconst app = require('express')()\nconst csrf = new Csrf('csrfSecret', { cookie: { secure: false } })\n\n// works with or without a session\napp.use(\n  session({ secret: 'sessionSecret', resave: false, saveUninitialized: true })\n)\n\napp.use(csrf.checkOrigin)\n\napp.get(\n  '/form',\n  csrf.create, // adds CSRF protection\n  (req, res) =\u003e {\n    // render a form\n    res.end(`\n      \u003cform action=\"/form\" method=\"POST\"\u003e\n        \u003cinput type=\"hidden\" name=\"csrf\" value=\"${req.csrfToken()}\" \u003e\n        \u003cinput type=\"text\" name=\"text\" value=\"some text\"\u003e\u003cbr\u003e\n        \u003cbutton\u003eSubmit\u003c/button\u003e\n      \u003c/form\u003e\n    `)\n  }\n)\napp.post(\n  '/form', // render the submitted values or throw\n  bodyParser.urlencoded({ extended: false }),\n  csrf.verify,\n  (req, res) =\u003e {\n    res.end(`\n\u003ch2\u003eParameters\u003c/h2\u003e\n\u003cpre\u003e\ntext: ${req.body.text}\ncsrf: ${req.body.csrf}\n\u003c/pre\u003e\n    `)\n  }\n)\n\napp.use('/', (req, res) =\u003e {\n  res.redirect('/form')\n})\n\napp.listen(3000)\n```\n\n### XHR\n\nRun example and browse to http://localhost:3000/xhr\n\n```js\napp.use(csrf.checkOrigin)\n\napp.get(\n  '/api/login',\n  (req, res, next) =\u003e {\n    // prevent creating a new csrf cookie if authenticated\n    const err =\n      req.headers.authorization \u0026\u0026 httpError(403, 'already authenticated')\n    next(err)\n  },\n  csrf.create,\n  (req, res) =\u003e res.json({ token: 'your-auth-token' })\n)\n\napp.use('/api', csrf.verifyXhr, (req, res) =\u003e res.json({}))\n```\n\n## Installation\n\nRequires [nodejs](http://nodejs.org/).\n\n```sh\n$ npm install signed-token-csrf\n```\n\n## Tests\n\n```sh\n$ npm test\n```\n\n## License\n\nUnlicense \u003chttps://unlicense.org\u003e\n\n## References\n\n\u003c!-- !ref --\u003e\n\n- [Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet - OWASP][Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet - OWASP]\n- [Your API-Centric Web App Is Probably Not Safe Against XSS and CSRF][Your API-Centric Web App Is Probably Not Safe Against XSS and CSRF]\n\n\u003c!-- ref! --\u003e\n\n[Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet - OWASP]: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet\n[Your API-Centric Web App Is Probably Not Safe Against XSS and CSRF]: http://www.redotheweb.com/2015/11/09/api-security.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fsigned-token-csrf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommenthol%2Fsigned-token-csrf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fsigned-token-csrf/lists"}