-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/fastify/fastify-secure-se…
…ssion into next
- Loading branch information
Showing
8 changed files
with
517 additions
and
13 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,62 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const fastify = require('fastify')({ logger: false }) | ||
const sodium = require('sodium-native') | ||
const FakeTimers = require('@sinonjs/fake-timers') | ||
const clock = FakeTimers.install({ | ||
shouldAdvanceTime: true, | ||
now: Date.now() | ||
}) | ||
|
||
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) | ||
sodium.randombytes_buf(key) | ||
|
||
fastify.register(require('../'), { | ||
key, | ||
expiry: 15 * 60 // 15 minutes | ||
}) | ||
|
||
fastify.post('/', (request, reply) => { | ||
request.session.set('some', request.body.some) | ||
request.session.set('some2', request.body.some2) | ||
reply.send('hello world') | ||
}) | ||
|
||
t.teardown(fastify.close.bind(fastify)) | ||
t.plan(5) | ||
|
||
fastify.get('/', (request, reply) => { | ||
const some = request.session.get('some') | ||
const some2 = request.session.get('some2') | ||
reply.send({ some, some2 }) | ||
}) | ||
|
||
fastify.inject({ | ||
method: 'POST', | ||
url: '/', | ||
payload: { | ||
some: 'someData', | ||
some2: { a: 1, b: undefined, c: 3 } | ||
} | ||
}, (error, response) => { | ||
t.error(error) | ||
t.equal(response.statusCode, 200) | ||
t.ok(response.headers['set-cookie']) | ||
|
||
clock.jump('00:15:01') // default validity is 24 hours | ||
|
||
fastify.inject({ | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: response.headers['set-cookie'] | ||
} | ||
}, (error, response) => { | ||
t.error(error) | ||
t.same(JSON.parse(response.payload), {}) | ||
clock.reset() | ||
clock.uninstall() | ||
fastify.close() | ||
}) | ||
}) |
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,62 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const fastify = require('fastify')({ logger: false }) | ||
const sodium = require('sodium-native') | ||
const FakeTimers = require('@sinonjs/fake-timers') | ||
const clock = FakeTimers.install({ | ||
shouldAdvanceTime: true, | ||
now: Date.now() | ||
}) | ||
|
||
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) | ||
|
||
sodium.randombytes_buf(key) | ||
|
||
fastify.register(require('../'), { | ||
key | ||
}) | ||
|
||
fastify.post('/', (request, reply) => { | ||
request.session.set('some', request.body.some) | ||
request.session.set('some2', request.body.some2) | ||
reply.send('hello world') | ||
}) | ||
|
||
t.teardown(fastify.close.bind(fastify)) | ||
t.plan(6) | ||
|
||
fastify.get('/', (request, reply) => { | ||
const some = request.session.get('some') | ||
const some2 = request.session.get('some2') | ||
reply.send({ some, some2 }) | ||
}) | ||
|
||
fastify.inject({ | ||
method: 'POST', | ||
url: '/', | ||
payload: { | ||
some: 'someData', | ||
some2: { a: 1, b: undefined, c: 3 } | ||
} | ||
}, (error, response) => { | ||
t.error(error) | ||
t.equal(response.statusCode, 200) | ||
t.ok(response.headers['set-cookie']) | ||
t.equal(response.headers['set-cookie'].split(';')[1].trim(), 'HttpOnly') | ||
|
||
clock.jump('24:01:00') // default validity is 24 hours | ||
|
||
fastify.inject({ | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: response.headers['set-cookie'] | ||
} | ||
}, (error, response) => { | ||
t.error(error) | ||
t.same(JSON.parse(response.payload), {}) | ||
clock.reset() | ||
clock.uninstall() | ||
}) | ||
}) |
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,96 @@ | ||
'use strict' | ||
|
||
const tap = require('tap') | ||
const Fastify = require('fastify') | ||
const SecureSessionPlugin = require('../') | ||
const sodium = require('sodium-native') | ||
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) | ||
sodium.randombytes_buf(key) | ||
|
||
tap.test('http-only override', async t => { | ||
const fastify = Fastify({ logger: false }) | ||
t.teardown(fastify.close.bind(fastify)) | ||
t.plan(3) | ||
|
||
await fastify.register(SecureSessionPlugin, { | ||
key, | ||
cookie: { | ||
path: '/', | ||
httpOnly: false | ||
} | ||
}) | ||
|
||
fastify.post('/login', (request, reply) => { | ||
request.session.set('user', request.body.email) | ||
reply.send('Welcome back!') | ||
}) | ||
|
||
const loginResponse = await fastify.inject({ | ||
method: 'POST', | ||
url: '/login', | ||
payload: { | ||
email: 'me@here.fine' | ||
} | ||
}) | ||
|
||
t.equal(loginResponse.statusCode, 200) | ||
t.ok(loginResponse.headers['set-cookie']) | ||
t.not(loginResponse.headers['set-cookie'].split(';')[1].trim(), 'HttpOnly') | ||
}) | ||
|
||
tap.test('Override global options does not change httpOnly default', t => { | ||
t.plan(8) | ||
const fastify = Fastify() | ||
fastify.register(SecureSessionPlugin, { | ||
key, | ||
cookieOptions: { | ||
maxAge: 42, | ||
path: '/' | ||
} | ||
}) | ||
|
||
fastify.post('/', (request, reply) => { | ||
request.session.set('data', request.body) | ||
request.session.options({ maxAge: 1000 * 60 * 60 }) | ||
reply.send('hello world') | ||
}) | ||
|
||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.get('/', (request, reply) => { | ||
const data = request.session.get('data') | ||
|
||
if (!data) { | ||
reply.code(404).send() | ||
return | ||
} | ||
reply.send(data) | ||
}) | ||
|
||
fastify.inject({ | ||
method: 'POST', | ||
url: '/', | ||
payload: { | ||
some: 'data' | ||
} | ||
}, (error, response) => { | ||
t.error(error) | ||
t.equal(response.statusCode, 200) | ||
t.ok(response.headers['set-cookie']) | ||
const { maxAge, path } = response.cookies[0] | ||
t.equal(maxAge, 1000 * 60 * 60) | ||
t.equal(response.headers['set-cookie'].split(';')[3].trim(), 'HttpOnly') | ||
t.equal(path, '/') | ||
|
||
fastify.inject({ | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: response.headers['set-cookie'] | ||
} | ||
}, (error, response) => { | ||
t.error(error) | ||
t.same(JSON.parse(response.payload), { some: 'data' }) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.