{"id":14979076,"url":"https://github.com/frankie567/starlette-csrf","last_synced_at":"2025-10-30T00:08:21.413Z","repository":{"id":37391244,"uuid":"367691713","full_name":"frankie567/starlette-csrf","owner":"frankie567","description":"Starlette middleware implementing Double Submit Cookie technique to mitigate CSRF","archived":false,"fork":false,"pushed_at":"2025-03-15T10:27:13.000Z","size":76,"stargazers_count":80,"open_issues_count":6,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-10-28T14:48:17.007Z","etag":null,"topics":["asgi","csrf","fastapi","fastapi-middleware","starlette","starlette-middleware"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frankie567.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"frankie567"}},"created_at":"2021-05-15T17:26:13.000Z","updated_at":"2025-09-02T10:29:29.000Z","dependencies_parsed_at":"2024-11-15T05:02:27.722Z","dependency_job_id":"68750963-a7ef-446e-9482-bf767bb6e785","html_url":"https://github.com/frankie567/starlette-csrf","commit_stats":{"total_commits":51,"total_committers":2,"mean_commits":25.5,"dds":"0.019607843137254943","last_synced_commit":"48c62c47b0d5a9393894d1207f7f605ceac52a34"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/frankie567/starlette-csrf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frankie567%2Fstarlette-csrf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frankie567%2Fstarlette-csrf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frankie567%2Fstarlette-csrf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frankie567%2Fstarlette-csrf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frankie567","download_url":"https://codeload.github.com/frankie567/starlette-csrf/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frankie567%2Fstarlette-csrf/sbom","scorecard":{"id":409568,"data":{"date":"2025-08-11","repo":{"name":"github.com/frankie567/starlette-csrf","commit":"eae8ba784dba29703b0bf741b4014a0373ada0e9"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.1,"checks":[{"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":"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":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 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":"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":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/build.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":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/frankie567/starlette-csrf/build.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/frankie567/starlette-csrf/build.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:60: update your workflow using https://app.stepsecurity.io/secureworkflow/frankie567/starlette-csrf/build.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/frankie567/starlette-csrf/build.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/frankie567/starlette-csrf/build.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/frankie567/starlette-csrf/build.yml/main?enable=pin","Warn: pipCommand not pinned by hash: .github/workflows/build.yml:21","Warn: pipCommand not pinned by hash: .github/workflows/build.yml:22","Warn: pipCommand not pinned by hash: .github/workflows/build.yml:50","Warn: pipCommand not pinned by hash: .github/workflows/build.yml:51","Info:   0 out of   4 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   2 third-party GitHubAction dependencies pinned","Info:   0 out of   4 pipCommand 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: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE: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":0,"reason":"Project has not signed or included provenance with any releases.","details":["Warn: release artifact v3.0.0 not signed: https://api.github.com/repos/frankie567/starlette-csrf/releases/110061929","Warn: release artifact v2.0.0 not signed: https://api.github.com/repos/frankie567/starlette-csrf/releases/93036646","Warn: release artifact v1.4.5 not signed: https://api.github.com/repos/frankie567/starlette-csrf/releases/83719913","Warn: release artifact v3.0.0 does not have provenance: https://api.github.com/repos/frankie567/starlette-csrf/releases/110061929","Warn: release artifact v2.0.0 does not have provenance: https://api.github.com/repos/frankie567/starlette-csrf/releases/93036646","Warn: release artifact v1.4.5 does not have provenance: https://api.github.com/repos/frankie567/starlette-csrf/releases/83719913"],"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 'main'"],"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"}}]},"last_synced_at":"2025-08-18T22:18:27.155Z","repository_id":37391244,"created_at":"2025-08-18T22:18:27.158Z","updated_at":"2025-08-18T22:18:27.158Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281720206,"owners_count":26549941,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-29T02:00:06.901Z","response_time":59,"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":["asgi","csrf","fastapi","fastapi-middleware","starlette","starlette-middleware"],"created_at":"2024-09-24T13:59:11.469Z","updated_at":"2025-10-30T00:08:21.396Z","avatar_url":"https://github.com/frankie567.png","language":"Python","funding_links":["https://github.com/sponsors/frankie567"],"categories":[],"sub_categories":[],"readme":"# Starlette CSRF Middleware\n\nStarlette middleware implementing Double Submit Cookie technique to mitigate CSRF.\n\n[![build](https://github.com/frankie567/starlette-csrf/workflows/Build/badge.svg)](https://github.com/frankie567/starlette-csrf/actions)\n[![codecov](https://codecov.io/gh/frankie567/starlette-csrf/branch/main/graph/badge.svg?token=fL49kIvrj6)](https://codecov.io/gh/frankie567/starlette-csrf)\n[![PyPI version](https://badge.fury.io/py/starlette-csrf.svg)](https://badge.fury.io/py/starlette-csrf)\n[![Downloads](https://pepy.tech/badge/starlette-csrf)](https://pepy.tech/project/starlette-csrf)\n\n## How it works?\n\n1. The user makes a first request with a method considered safe (by default `GET`, `HEAD`, `OPTIONS`, `TRACE`).\n2. It receives in response a cookie (named by default `csrftoken`) which contains a secret value.\n3. When the user wants to make an unsafe request, the server expects them to send the same secret value in a header (named by default `x-csrftoken`).\n4. The middleware will then compare the secret value provided in the cookie and the header.\n    - If they match, the request is processed.\n    - Otherwise, a `403 Forbidden` error response is given.\n\nThis mechanism is necessary if you rely on cookie authentication in a browser. You can have more information about CSRF and Double Submit Cookie in the [OWASP Cheat Sheet Series](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html).\n\n## Installation\n\n```bash\npip install starlette-csrf\n```\n\n## Usage with Starlette\n\n```py\nfrom starlette.applications import Starlette\nfrom starlette.middleware import Middleware\nfrom starlette_csrf import CSRFMiddleware\n\nroutes = ...\n\nmiddleware = [\n    Middleware(CSRFMiddleware, secret=\"__CHANGE_ME__\")\n]\n\napp = Starlette(routes=routes, middleware=middleware)\n```\n\n## Usage with FastAPI\n\n```py\nfrom fastapi import FastAPI\nfrom starlette_csrf import CSRFMiddleware\n\napp = FastAPI()\n\napp.add_middleware(CSRFMiddleware, secret=\"__CHANGE_ME__\")\n```\n\n## Arguments\n\n- `secret` (`str`): Secret to sign the CSRF token value. **Be sure to choose a strong passphrase and keep it SECRET**.\n- `required_urls` (`Optional[List[re.Pattern]]` - `None`): List of URL regexes that the CSRF check should **always** be enforced, no matter the method or the cookies present.\n- `exempt_urls` (`Optional[List[re.Pattern]]` - `None`): List of URL regexes that the CSRF check should be skipped on. Useful if you have any APIs that you know do not need CSRF protection.\n- `sensitive_cookies` (`Set[str]` - `None`): Set of cookie names that should trigger the CSRF check if they are present in the request. Useful if you have other authentication methods that don't rely on cookies and don't need CSRF enforcement. If this parameter is `None`, the default, CSRF is **always** enforced.\n- `safe_methods` (`Set[str]` - `{\"GET\", \"HEAD\", \"OPTIONS\", \"TRACE\"}`): HTTP methods considered safe which don't need CSRF protection.\n- `cookie_name` (`str` - `csrftoken`): Name of the cookie.\n- `cookie_path` `str` - `/`): Cookie path.\n- `cookie_domain` (`Optional[str]` - `None`): Cookie domain. If your frontend and API lives in different sub-domains, be sure to set this argument with your root domain to allow your frontend sub-domain to read the cookie on the JavaScript side.\n- `cookie_secure` (`bool` - `False`): Whether to only send the cookie to the server via SSL request.\n- `cookie_samesite` (`str` - `lax`): Samesite strategy of the cookie.\n- `header_name` (`str` - `x-csrftoken`): Name of the header where you should set the CSRF token.\n\n## Customize error response\n\nBy default, a plain text response with the status code 403 is returned when the CSRF verification is failing. You can customize it by overloading the middleware class and implementing the `_get_error_response` method. It accepts in argument the original `Request` object and expects a `Response`. For example:\n\n```py\nfrom starlette.requests import Request\nfrom starlette.responses import JSONResponse, Response\nfrom starlette_csrf import CSRFMiddleware\n\nclass CustomResponseCSRFMiddleware(CSRFMiddleware):\n    def _get_error_response(self, request: Request) -\u003e Response:\n        return JSONResponse(\n            content={\"code\": \"CSRF_ERROR\"}, status_code=403\n        )\n```\n\n## Development\n\n### Setup environment\n\nWe use [Hatch](https://hatch.pypa.io/latest/install/) to manage the development environment and production build. Ensure it's installed on your system.\n\n### Run unit tests\n\nYou can run all the tests with:\n\n```bash\nhatch run test\n```\n\n### Format the code\n\nExecute the following command to apply linting and check typing:\n\n```bash\nhatch run lint\n```\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrankie567%2Fstarlette-csrf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrankie567%2Fstarlette-csrf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrankie567%2Fstarlette-csrf/lists"}