-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,554 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
This is an [Express](https://expressjs.com) example app. | ||
|
||
## Getting Started | ||
|
||
First, install dependencies: | ||
|
||
```bash | ||
npm install | ||
# or | ||
pnpm install | ||
# or | ||
yarn install | ||
``` | ||
|
||
Next, run the server: | ||
|
||
```bash | ||
node app.js | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createCsrfMiddleware } from '@edge-csrf/express'; | ||
import express from 'express'; | ||
|
||
// initalize csrf protection middleware | ||
const csrfMiddleware = createCsrfMiddleware({ | ||
cookie: { | ||
secure: process.env.NODE_ENV === 'production', | ||
}, | ||
}); | ||
|
||
// init app | ||
const app = express(); | ||
const port = 3000; | ||
|
||
// add body parsing middleware | ||
app.use(express.urlencoded({ extended: false })); | ||
|
||
// add csrf middleware | ||
app.use(csrfMiddleware); | ||
|
||
// define handlers | ||
app.get('/', (req, res) => { | ||
const csrfToken = res.getHeader('X-CSRF-Token') || 'missing'; | ||
res.send(` | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<p>CSRF token value: ${csrfToken}</p> | ||
<h2>HTML Form Submission Example:</h2> | ||
<form action="/form-handler" method="post"> | ||
<legend>Form without CSRF (should fail):</legend> | ||
<input type="text" name="input1" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
<br /> | ||
<form action="/form-handler" method="post"> | ||
<legend>Form with incorrect CSRF (should fail):</legend> | ||
<input type="hidden" name="csrf_token" value="notvalid" /> | ||
<input type="text" name="input1" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
<br /> | ||
<form action="/form-handler" method="post"> | ||
<legend>Form with CSRF (should succeed):</legend> | ||
<input type="hidden" name="csrf_token" value="${csrfToken}" /> | ||
<input type="text" name="input1" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> | ||
`); | ||
}); | ||
|
||
app.post('/form-handler', (req, res) => { | ||
res.send('success'); | ||
}); | ||
|
||
// start server | ||
app.listen(port, () => { | ||
console.log(`Example app listening on port ${port}`) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "edge-csrf-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@edge-csrf/express": "^2.1.0", | ||
"express": "^4.19.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,6 @@ | |
"vite": "^5.2.8", | ||
"vitest": "^1.4.0", | ||
"vitest-environment-miniflare": "^2.14.2" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.98a80fd11c2e7096747762304106432b3ddc67dcf54b5a8c01c93f68a2cd5e05e6821849522a06fb76284d41a2660d5e334f2ee3bbf29183bf2e739b1dafa771" | ||
} |
Oops, something went wrong.