Skip to content

Commit d6c7cb3

Browse files
committed
ammended w Simon feedback 🙏
1 parent c7c4712 commit d6c7cb3

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/http/csrf/create.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
let crypto = require('node:crypto')
1+
const crypto = require('node:crypto')
2+
const fiveMinutes = 300000
23

34
/** creates a signed token [rando].[timestamp].[sig] */
4-
module.exports = function create (data) {
5+
module.exports = function create (data, ts) {
56
data = data || Buffer.from(crypto.randomUUID().replace(/-/g, ''))
6-
const secret = 'changeme' || process.env.ARC_APP_SECRET
7-
const ts = Date.now()
7+
ts = ts || Date.now() + fiveMinutes
8+
const secret = process.env.ARC_APP_SECRET || process.env.ARC_APP_NAME || 'fallback'
89
return `${data}.${ts}.${crypto.createHmac('sha256', secret).update(data).digest('hex').toString()}`
910
}

src/http/csrf/verify.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ let create = require('./create')
33
/** ensures payload is valid token that hasn't expired */
44
module.exports = function verify (payload) {
55
const [ data, ts, sig ] = payload.split('.')
6-
const elapsed = Date.now() - ts
7-
const fiveMinutes = 300000
8-
if (elapsed > fiveMinutes) return false
9-
const gen = create(data)
6+
if (Date.now() > ts) return false
7+
const gen = create(data, ts)
108
const sig2 = gen.split('.').pop()
119
return sig2 === sig
1210
}

0 commit comments

Comments
 (0)