|
|
@ -152,154 +152,227 @@ describe('socket.io access checks', function() {
|
|
|
|
await cleanUpPads();
|
|
|
|
await cleanUpPads();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Normal accesses.
|
|
|
|
describe('Normal accesses', function() {
|
|
|
|
it('!authn anonymous cookie /p/pad -> 200, ok', async function() {
|
|
|
|
it('!authn anonymous cookie /p/pad -> 200, ok', async function() {
|
|
|
|
const res = await agent.get('/p/pad').expect(200);
|
|
|
|
const res = await agent.get('/p/pad').expect(200);
|
|
|
|
// Should not throw.
|
|
|
|
// Should not throw.
|
|
|
|
socket = await connect(res);
|
|
|
|
socket = await connect(res);
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('!authn !cookie -> ok', async function() {
|
|
|
|
it('!authn !cookie -> ok', async function() {
|
|
|
|
// Should not throw.
|
|
|
|
// Should not throw.
|
|
|
|
socket = await connect(null);
|
|
|
|
socket = await connect(null);
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('!authn user /p/pad -> 200, ok', async function() {
|
|
|
|
it('!authn user /p/pad -> 200, ok', async function() {
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
// Should not throw.
|
|
|
|
// Should not throw.
|
|
|
|
socket = await connect(res);
|
|
|
|
socket = await connect(res);
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('authn user /p/pad -> 200, ok', async function() {
|
|
|
|
it('authn user /p/pad -> 200, ok', async function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
// Should not throw.
|
|
|
|
// Should not throw.
|
|
|
|
socket = await connect(res);
|
|
|
|
socket = await connect(res);
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('authz user /p/pad -> 200, ok', async function() {
|
|
|
|
it('authz user /p/pad -> 200, ok', async function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
// Should not throw.
|
|
|
|
// Should not throw.
|
|
|
|
socket = await connect(res);
|
|
|
|
socket = await connect(res);
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('supports pad names with characters that must be percent-encoded', async function() {
|
|
|
|
it('supports pad names with characters that must be percent-encoded', async function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
// requireAuthorization is set to true here to guarantee that the user's padAuthorizations
|
|
|
|
// requireAuthorization is set to true here to guarantee that the user's padAuthorizations
|
|
|
|
// object is populated. Technically this isn't necessary because the user's padAuthorizations is
|
|
|
|
// object is populated. Technically this isn't necessary because the user's padAuthorizations
|
|
|
|
// currently populated even if requireAuthorization is false, but setting this to true ensures
|
|
|
|
// is currently populated even if requireAuthorization is false, but setting this to true
|
|
|
|
// the test remains useful if the implementation ever changes.
|
|
|
|
// ensures the test remains useful if the implementation ever changes.
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
const encodedPadId = encodeURIComponent('päd');
|
|
|
|
const encodedPadId = encodeURIComponent('päd');
|
|
|
|
const res = await agent.get(`/p/${encodedPadId}`).auth('user', 'user-password').expect(200);
|
|
|
|
const res = await agent.get(`/p/${encodedPadId}`).auth('user', 'user-password').expect(200);
|
|
|
|
// Should not throw.
|
|
|
|
// Should not throw.
|
|
|
|
socket = await connect(res);
|
|
|
|
socket = await connect(res);
|
|
|
|
const clientVars = await handshake(socket, 'päd');
|
|
|
|
const clientVars = await handshake(socket, 'päd');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Abnormal access attempts.
|
|
|
|
describe('Abnormal access attempts', function() {
|
|
|
|
it('authn anonymous /p/pad -> 401, error', async function() {
|
|
|
|
it('authn anonymous /p/pad -> 401, error', async function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
const res = await agent.get('/p/pad').expect(401);
|
|
|
|
const res = await agent.get('/p/pad').expect(401);
|
|
|
|
// Despite the 401, try to create the pad via a socket.io connection anyway.
|
|
|
|
// Despite the 401, try to create the pad via a socket.io connection anyway.
|
|
|
|
await assert.rejects(connect(res), {message: /authentication required/i});
|
|
|
|
await assert.rejects(connect(res), {message: /authentication required/i});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('authn !cookie -> error', async function() {
|
|
|
|
it('authn !cookie -> error', async function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
await assert.rejects(connect(null), {message: /signed express_sid cookie is required/i});
|
|
|
|
await assert.rejects(connect(null), {message: /signed express_sid cookie is required/i});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('authorization bypass attempt -> error', async function() {
|
|
|
|
it('authorization bypass attempt -> error', async function() {
|
|
|
|
// Only allowed to access /p/pad.
|
|
|
|
// Only allowed to access /p/pad.
|
|
|
|
authorize = (req) => req.path === '/p/pad';
|
|
|
|
authorize = (req) => req.path === '/p/pad';
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
// First authenticate and establish a session.
|
|
|
|
// First authenticate and establish a session.
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
// Connecting should work because the user successfully authenticated.
|
|
|
|
// Connecting should work because the user successfully authenticated.
|
|
|
|
socket = await connect(res);
|
|
|
|
socket = await connect(res);
|
|
|
|
// Accessing /p/other-pad should fail, despite the successful fetch of /p/pad.
|
|
|
|
// Accessing /p/other-pad should fail, despite the successful fetch of /p/pad.
|
|
|
|
const message = await handshake(socket, 'other-pad');
|
|
|
|
const message = await handshake(socket, 'other-pad');
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Authorization levels via authorize hook
|
|
|
|
describe('Authorization levels via authorize hook', function() {
|
|
|
|
it("level='create' -> can create", async () => {
|
|
|
|
beforeEach(async function() {
|
|
|
|
authorize = () => 'create';
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
});
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
it("level='create' -> can create", async function() {
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
authorize = () => 'create';
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('level=true -> can create', async function() {
|
|
|
|
|
|
|
|
authorize = () => true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='modify' -> can modify", async function() {
|
|
|
|
|
|
|
|
await padManager.getPad('pad'); // Create the pad.
|
|
|
|
|
|
|
|
authorize = () => 'modify';
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='create' settings.editOnly=true -> unable to create", async function() {
|
|
|
|
|
|
|
|
authorize = () => 'create';
|
|
|
|
|
|
|
|
settings.editOnly = true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='modify' settings.editOnly=false -> unable to create", async function() {
|
|
|
|
|
|
|
|
authorize = () => 'modify';
|
|
|
|
|
|
|
|
settings.editOnly = false;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='readOnly' -> unable to create", async function() {
|
|
|
|
|
|
|
|
authorize = () => 'readOnly';
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='readOnly' -> unable to modify", async function() {
|
|
|
|
|
|
|
|
await padManager.getPad('pad'); // Create the pad.
|
|
|
|
|
|
|
|
authorize = () => 'readOnly';
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, true);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('level=true -> can create', async () => {
|
|
|
|
|
|
|
|
authorize = () => true;
|
|
|
|
describe('Authorization levels via user settings', function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
beforeEach(async function() {
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
});
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
it('user.canCreate = true -> can create and modify', async function() {
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
settings.users.user.canCreate = true;
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user.canCreate = false -> unable to create', async function() {
|
|
|
|
|
|
|
|
settings.users.user.canCreate = false;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user.readOnly = true -> unable to create', async function() {
|
|
|
|
|
|
|
|
settings.users.user.readOnly = true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user.readOnly = true -> unable to modify', async function() {
|
|
|
|
|
|
|
|
await padManager.getPad('pad'); // Create the pad.
|
|
|
|
|
|
|
|
settings.users.user.readOnly = true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, true);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user.readOnly = false -> can create and modify', async function() {
|
|
|
|
|
|
|
|
settings.users.user.readOnly = false;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('user.readOnly = true, user.canCreate = true -> unable to create', async function() {
|
|
|
|
|
|
|
|
settings.users.user.canCreate = true;
|
|
|
|
|
|
|
|
settings.users.user.readOnly = true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("level='modify' -> can modify", async () => {
|
|
|
|
|
|
|
|
await padManager.getPad('pad'); // Create the pad.
|
|
|
|
describe('Authorization level interaction between authorize hook and user settings', function() {
|
|
|
|
authorize = () => 'modify';
|
|
|
|
beforeEach(async function() {
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
});
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
it('authorize hook does not elevate level from user settings', async function() {
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
settings.users.user.readOnly = true;
|
|
|
|
assert.equal(clientVars.data.readonly, false);
|
|
|
|
authorize = () => 'create';
|
|
|
|
});
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
it("level='create' settings.editOnly=true -> unable to create", async () => {
|
|
|
|
socket = await connect(res);
|
|
|
|
authorize = () => 'create';
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
});
|
|
|
|
settings.editOnly = true;
|
|
|
|
it('user settings does not elevate level from authorize hook', async function() {
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
settings.users.user.readOnly = false;
|
|
|
|
socket = await connect(res);
|
|
|
|
settings.users.user.canCreate = true;
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
authorize = () => 'readOnly';
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
});
|
|
|
|
socket = await connect(res);
|
|
|
|
it("level='modify' settings.editOnly=false -> unable to create", async () => {
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
authorize = () => 'modify';
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
});
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
|
|
|
|
settings.editOnly = false;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='readOnly' -> unable to create", async () => {
|
|
|
|
|
|
|
|
authorize = () => 'readOnly';
|
|
|
|
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const message = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(message.accessStatus, 'deny');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it("level='readOnly' -> unable to modify", async () => {
|
|
|
|
|
|
|
|
await padManager.getPad('pad'); // Create the pad.
|
|
|
|
|
|
|
|
authorize = () => 'readOnly';
|
|
|
|
|
|
|
|
settings.requireAuthentication = true;
|
|
|
|
|
|
|
|
settings.requireAuthorization = true;
|
|
|
|
|
|
|
|
const res = await agent.get('/p/pad').auth('user', 'user-password').expect(200);
|
|
|
|
|
|
|
|
socket = await connect(res);
|
|
|
|
|
|
|
|
const clientVars = await handshake(socket, 'pad');
|
|
|
|
|
|
|
|
assert.equal(clientVars.type, 'CLIENT_VARS');
|
|
|
|
|
|
|
|
assert.equal(clientVars.data.readonly, true);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|