-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Node-HTTP implementation library, adds new built-in body parser …
…for Express and Node-HTTP (#47)
- Loading branch information
Showing
16 changed files
with
908 additions
and
126 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
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,9 @@ | ||
{ | ||
"name": "edge-csrf-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@edge-csrf/node-http": "^2.2.0" | ||
} | ||
} |
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,76 @@ | ||
import { createServer } from 'http'; | ||
|
||
import { CsrfError, createCsrfProtect } from '@edge-csrf/node-http'; | ||
|
||
// initalize csrf protection method | ||
const csrfProtect = createCsrfProtect({ | ||
cookie: { | ||
secure: process.env.NODE_ENV === 'production', | ||
}, | ||
}); | ||
|
||
// init server | ||
const server = createServer(async (req, res) => { | ||
// apply csrf protection | ||
try { | ||
await csrfProtect(req, res); | ||
} catch (err) { | ||
if (err instanceof CsrfError) { | ||
res.writeHead(403); | ||
res.end('invalid csrf token'); | ||
return; | ||
} | ||
throw err; | ||
} | ||
|
||
// add handler | ||
if (req.url === '/') { | ||
if (req.method === 'GET') { | ||
const csrfToken = res.getHeader('X-CSRF-Token') || 'missing'; | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
res.end(` | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<p>CSRF token value: ${csrfToken}</p> | ||
<h2>HTML Form Submission Example:</h2> | ||
<form action="/" method="post"> | ||
<legend>Form without CSRF (should fail):</legend> | ||
<input type="text" name="input1" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
<br /> | ||
<form action="/" 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="/" 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> | ||
`); | ||
return; | ||
} | ||
|
||
if (req.method === 'POST') { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.end('success'); | ||
return; | ||
} | ||
} | ||
|
||
res.writeHead(404); | ||
res.end('not found'); | ||
}); | ||
|
||
// start server | ||
server.listen(3000, () => { | ||
console.log('Server is listening on port 3000'); | ||
}); |
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
Oops, something went wrong.