-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at adding basic csrf tokens
- Loading branch information
1 parent
a025100
commit c7c4712
Showing
6 changed files
with
62 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let crypto = require('node:crypto') | ||
|
||
/** creates a signed token [rando].[timestamp].[sig] */ | ||
module.exports = function create (data) { | ||
data = data || Buffer.from(crypto.randomUUID().replace(/-/g, '')) | ||
const secret = 'changeme' || process.env.ARC_APP_SECRET | ||
const ts = Date.now() | ||
return `${data}.${ts}.${crypto.createHmac('sha256', secret).update(data).digest('hex').toString()}` | ||
} |
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,12 @@ | ||
let create = require('./create') | ||
|
||
/** ensures payload is valid token that hasn't expired */ | ||
module.exports = function verify (payload) { | ||
const [ data, ts, sig ] = payload.split('.') | ||
const elapsed = Date.now() - ts | ||
const fiveMinutes = 300000 | ||
if (elapsed > fiveMinutes) return false | ||
const gen = create(data) | ||
const sig2 = gen.split('.').pop() | ||
return sig2 === sig | ||
} |
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,34 @@ | ||
let http = require('../../../../../src/http') | ||
let test = require('tape') | ||
|
||
test('exists', t => { | ||
t.plan(2) | ||
t.ok(http.csrf.create, 'create') | ||
t.ok(http.csrf.verify, 'verify') | ||
}) | ||
|
||
test('create a value', t => { | ||
t.plan(1) | ||
let val = http.csrf.create() | ||
t.ok(val, 'created value') | ||
}) | ||
|
||
test('verify a value', t => { | ||
t.plan(1) | ||
let val = http.csrf.create() | ||
t.ok(http.csrf.verify(val), 'value verified') | ||
}) | ||
|
||
test('tampered token is falsy', t => { | ||
t.plan(1) | ||
let tamperedToken = "3d879d515ab241429c97dfea6d1e1927.1584118407000.b0b34563d569030cbe9a4ea63312f23729813b838478420e3811c0bfeaf3add1" | ||
t.ok(http.csrf.verify(tamperedToken) === false, 'value falsy') | ||
}) | ||
|
||
test('token expired is falsy', t => { | ||
t.plan(1) | ||
let expiredToken = "3d879d515ab241419c97dfea6d1e1927.1584118407000.b0b34563d569030cbe9a4ea63312f23729813b838478420e3811c0bfeaf3add1" | ||
t.ok(http.csrf.verify(expiredToken) === false, 'value falsy') | ||
}) | ||
|
||
|