2020-08-05 12:05:52 +02:00
|
|
|
import useApp, {TestApp} from "./_app";
|
2020-07-08 10:49:29 +02:00
|
|
|
import Controller from "../src/Controller";
|
|
|
|
import CsrfProtectionComponent from "../src/components/CsrfProtectionComponent";
|
2020-08-05 12:05:52 +02:00
|
|
|
import supertest from "supertest";
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
let app: TestApp;
|
2020-09-17 21:15:37 +02:00
|
|
|
useApp((addr, port) => {
|
2020-08-05 12:05:52 +02:00
|
|
|
return app = new class extends TestApp {
|
2020-07-08 10:49:29 +02:00
|
|
|
protected async init(): Promise<void> {
|
|
|
|
this.use(new class extends Controller {
|
2020-09-25 23:42:15 +02:00
|
|
|
public routes(): void {
|
|
|
|
this.get('/', (req, res) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
res.render('test/csrf.njk');
|
|
|
|
}, 'csrf_test');
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
this.post('/', (req, res) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
res.json({
|
|
|
|
status: 'ok',
|
|
|
|
});
|
|
|
|
}, 'csrf_test');
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
await super.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected registerComponents() {
|
|
|
|
super.registerComponents();
|
|
|
|
this.use(new CsrfProtectionComponent());
|
|
|
|
}
|
2020-09-17 21:15:37 +02:00
|
|
|
}(addr, port);
|
2020-07-08 10:49:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Test CSRF protection', () => {
|
2020-08-05 12:05:52 +02:00
|
|
|
let cookies: string[];
|
|
|
|
let csrf: string;
|
|
|
|
|
|
|
|
test('no csrf token should be in session at first', (done) => {
|
|
|
|
const agent = supertest(app.getExpressApp());
|
|
|
|
agent.post('/')
|
|
|
|
.expect(401, (err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
expect(res.text).toContain(`You weren't assigned any CSRF token.`);
|
|
|
|
cookies = res.get('Set-Cookie');
|
|
|
|
|
|
|
|
agent.get('/')
|
|
|
|
.set('Cookie', cookies)
|
|
|
|
.expect(200, (err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
csrf = res.text;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2020-07-08 10:49:29 +02:00
|
|
|
});
|
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
test('sending no csrf token should fail', (done) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
expect(cookies).toBeDefined();
|
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
const agent = supertest(app.getExpressApp());
|
|
|
|
agent.post('/')
|
|
|
|
.set('Cookie', cookies)
|
|
|
|
.expect(401)
|
|
|
|
.end((err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
expect(res.text).toContain(`You didn't provide any CSRF token.`);
|
|
|
|
done();
|
|
|
|
});
|
2020-07-08 10:49:29 +02:00
|
|
|
});
|
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
test('sending an invalid csrf token should fail', (done) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
expect(cookies).toBeDefined();
|
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
const agent = supertest(app.getExpressApp());
|
|
|
|
agent.post('/')
|
|
|
|
.set('Cookie', cookies)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({csrf: 'not_a_valid_csrf'})
|
|
|
|
.expect(401, (err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
expect(res.text).toContain(`Tokens don't match.`);
|
|
|
|
done();
|
|
|
|
});
|
2020-07-08 10:49:29 +02:00
|
|
|
});
|
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
test('sending a valid csrf token should success', (done) => {
|
2020-07-08 10:49:29 +02:00
|
|
|
expect(cookies).toBeDefined();
|
|
|
|
|
2020-08-05 12:05:52 +02:00
|
|
|
const agent = supertest(app.getExpressApp());
|
|
|
|
agent.post('/')
|
|
|
|
.set('Cookie', cookies)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({csrf: csrf})
|
|
|
|
.expect(200, done);
|
2020-07-08 10:49:29 +02:00
|
|
|
});
|
2020-09-25 23:42:15 +02:00
|
|
|
});
|