Skip to content

Commit

Permalink
Patches, Fixes #4
Browse files Browse the repository at this point in the history
* Add content to the license
* Fix typo in filename
* Rename HttpError name attr
* Change HttpError error code
  • Loading branch information
j0lvera committed Aug 10, 2020
1 parent 25c9943 commit 8f45f0b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Juan Olvera

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# next-csrf

CSRF mitigation for Next.js.

## Features

Mitigation patterns that `next-csrf` implements:

* [Synchronizer Token Pattern](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#synchronizer-token-pattern) using [`csrf`](https://github.com/pillarjs/csrf) (Also [read Understanding CSRF](https://github.com/pillarjs/understanding-csrf#csrf-tokens))
* [Double-submit cookie pattern](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie)
* [Custom request headers](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#use-of-custom-request-headers)

### Installation

With yarn:
Expand All @@ -20,29 +30,15 @@ Setup:

```js
// file: lib/csrf.js
import { nextCsrf } from "./index";
import { nextCsrf } from "next-csrf";

const options = {
secret: process.env.CSRF_SECRET // You should use a secure random number
secret: process.env.CSRF_SECRET // Long, randomly-generated, unique, and unpredictable value
}

export const { csrf, csrfToken } = nextCsrf(options);
```

Protect an API endpoint:

```js
// file: pages/api/protected.js

import { csrf } from '../lib/csrf';

const handler = (req, res) => {
return res.status(200).json({ message: "This API route is protected."})
}

export default csrf(handler);
```

When you initialize `nextCsrf` it will return the middleware, and a valid signed CSRF token. You can send it along with a custom header on your first request to a protected API route. Is not required, but recommended.

If you don't send the given CSRF token on the first request one is set up on any first request you send to a protected API route.
Expand Down Expand Up @@ -89,5 +85,16 @@ function Login({ csrfToken }) {
export default Login;
```

Protect an API endpoint:

```js
// file: pages/api/protected.js

import { csrf } from '../lib/csrf';

const handler = (req, res) => {
return res.status(200).json({ message: "This API route is protected."})
}

export default csrf(handler);
```
File renamed without changes.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaultOptions = {
cookieOptions: {
httpOnly: true,
path: "/",
secure: process.env.NODE_ENV === "production",
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/utils/httpError.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class HttpError extends Error {
private status: number;

constructor(status = 401, message: string, ...params: undefined[]) {
constructor(status = 403, message: string, ...params: undefined[]) {
super(...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, HttpError);
}

this.name = "AuthError";
this.name = "HttpError";
this.status = status;
this.message = message;
}
Expand Down

0 comments on commit 8f45f0b

Please sign in to comment.