Skip to content

Commit

Permalink
Merge pull request #13 from curveball/csrf-support
Browse files Browse the repository at this point in the history
CSRF token checking support
  • Loading branch information
evert authored Feb 2, 2021
2 parents b13d115 + 60f6ca6 commit 00b7ebe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

0.2.0 (2020-02-02)
------------------

* Now supports submitting HTML form. This was blocked due to CSRF problems,
but we now validate CSRF tokens.
* This is considered a BC break, as this package requires curveball/session
0.6, which itself has introduced a BC breka.


0.1.4 (2020-10-27)
------------------

Expand Down
23 changes: 18 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
},
"homepage": "https://github.com/curveball/browser-to-bearer#readme",
"devDependencies": {
"@curveball/core": "^0.14.3",
"@curveball/core": "^0.16.1",
"@curveball/session": "^0.6.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.0",
"@types/node": "^10.17.51",
Expand All @@ -58,7 +59,8 @@
]
},
"peerDependencies": {
"@curveball/core": ">=0.9.0 <1.0.0"
"@curveball/core": ">=0.9.0 <1.0.0",
"@curveball/session": "^0.6.0"
},
"dependencies": {
"node-fetch": "^2.6.1"
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Context, Middleware } from '@curveball/core';
import { BadRequest, Forbidden, Unauthorized } from '@curveball/http-errors';
import '@curveball/session';
import { BadRequest, Unauthorized } from '@curveball/http-errors';
import { default as fetch, Response } from 'node-fetch';
import querystring from 'querystring';
import * as querystring from 'querystring';
import { resolve } from 'url';

type OAuth2Options = {
Expand Down Expand Up @@ -38,9 +39,9 @@ export default function(options: OAuth2Options): Middleware {
return handleInnerRequest(ctx, next, options);
}

if (!['GET', 'HEAD', 'OPTIONS'].includes(ctx.method)) {
// For now we only support safe methods.
throw new Forbidden('When using cookie-based authentication, only safe methods are supported');
if (!['GET', 'HEAD', 'OPTIONS', 'SEARCH'].includes(ctx.method)) {
// This is an unsafe method. We will check if there's a CSRF token.
ctx.validateCsrf();
}

ctx.request.headers.set('Authorization', 'Bearer ' + oauth2Tokens.accessToken);
Expand Down

0 comments on commit 00b7ebe

Please sign in to comment.