cryptpad/www/checkup/main.js

627 lines
22 KiB
JavaScript
Raw Normal View History

define([
'jquery',
'/api/config',
'/assert/assertions.js',
'/common/hyperscript.js',
'/customize/messages.js',
'/common/dom-ready.js',
'/bower_components/nthen/index.js',
2021-02-24 08:23:59 +00:00
'/common/sframe-common-outer.js',
'/customize/login.js',
'/common/common-hash.js',
'/common/common-util.js',
'/common/pinpad.js',
'/common/outer/network-config.js',
'/customize/pages.js',
'/bower_components/tweetnacl/nacl-fast.min.js',
'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
2021-05-03 09:16:26 +00:00
'less!/checkup/app-checkup.less',
], function ($, ApiConfig, Assertions, h, Messages, DomReady,
nThen, SFCommonO, Login, Hash, Util, Pinpad,
NetConfig, Pages) {
var Assert = Assertions();
2021-02-24 08:23:59 +00:00
var trimSlashes = function (s) {
if (typeof(s) !== 'string') { return s; }
return s.replace(/\/+$/, '');
};
var assert = function (f, msg) {
2021-05-03 09:16:26 +00:00
Assert(f, msg || h('span.advisory-text.cp-danger'));
};
var CONFIG_PATH = function () {
return h('code', 'cryptpad/config/config.js');
};
var API_CONFIG_LINK = function () {
return h('a', {
href: '/api/config',
target: '_blank',
}, '/api/config');
};
var RESTART_WARNING = function () {
return h('span', [
'Changes to ',
CONFIG_PATH(),
' will require a server restart in order for ',
API_CONFIG_LINK(),
' to be updated.',
]);
2021-02-24 08:23:59 +00:00
};
var trimmedSafe = trimSlashes(ApiConfig.httpSafeOrigin);
var trimmedUnsafe = trimSlashes(ApiConfig.httpUnsafeOrigin);
assert(function (cb, msg) {
msg.appendChild(h('span', [
"CryptPad's sandbox requires that both ",
h('code', 'httpUnsafeOrigin'),
' and ',
h('code', 'httpSafeOrigin'),
" be configured in ",
CONFIG_PATH(),
'. ',
RESTART_WARNING(),
]));
2021-02-24 08:23:59 +00:00
//console.error(trimmedSafe, trimmedUnsafe);
cb(Boolean(trimmedSafe && trimmedUnsafe));
});
assert(function (cb, msg) {
msg.appendChild(h('span', [
h('code', 'httpUnsafeOrigin'),
' and ',
h('code', 'httpSafeOrigin'),
' are equivalent. ',
"In order for CryptPad's security features to be as effective as intended they must be different. ",
"See ",
CONFIG_PATH(),
'. ',
RESTART_WARNING(),
]));
2021-02-24 08:23:59 +00:00
return void cb(trimmedSafe !== trimmedUnsafe);
});
assert(function (cb, msg) {
msg.appendChild(h('span', [
h('code', 'httpUnsafeOrigin'),
' and ',
h('code', 'httpSafeOrigin'),
' must not contain trailing slashes. This can be configured in ',
CONFIG_PATH(),
'. ',
RESTART_WARNING(),
]));
cb(trimmedSafe === ApiConfig.httpSafeOrigin && trimmedUnsafe === ApiConfig.httpUnsafeOrigin);
});
assert(function (cb, msg) {
msg.appendChild(h("span", [
"It appears that you are trying to load this page via an origin other than its main domain (",
h('code', ApiConfig.httpUnsafeOrigin),
2021-03-19 09:50:33 +00:00
"). See the ",
h('code', 'httpUnsafeOrigin'),
" option in ",
CONFIG_PATH(),
" which is exposed via ",
API_CONFIG_LINK(),
'.',
]));
var origin = window.location.origin;
2021-03-19 09:50:33 +00:00
return void cb(ApiConfig.httpUnsafeOrigin === origin);
});
2021-02-24 08:23:59 +00:00
var checkAvailability = function (url, cb) {
$.ajax({
url: url,
data: {},
2021-02-24 08:23:59 +00:00
complete: function (xhr) {
cb(xhr.status === 200);
},
});
};
assert(function (cb, msg) {
msg.appendChild(h('span', [
"The main domain (configured via ",
h('code', 'httpUnsafeOrigin'),
' as ',
ApiConfig.httpUnsafeOrigin,
' in ',
CONFIG_PATH(),
' and exposed via ',
API_CONFIG_LINK(),
') could not be reached.',
]));
2021-02-24 08:23:59 +00:00
checkAvailability(trimmedUnsafe, cb);
});
2021-02-24 08:23:59 +00:00
// Try loading an iframe on the safe domain
assert(function (cb, msg) {
msg.appendChild(h('span', [
"Your browser was not able to load an iframe using the origin specified as ",
h('code', "httpSafeOrigin"),
" (",
ApiConfig.httpSafeOrigin,
") in ",
CONFIG_PATH(),
". This can be caused by an invalid ",
h('code', 'httpUnsafeDomain'),
', invalid CSP configuration in your reverse proxy, invalid SSL certificates, and many other factors. ',
'More information about your particular error may be found in your browser console. ',
RESTART_WARNING(),
]));
var to;
nThen(function (waitFor) {
DomReady.onReady(waitFor());
}).nThen(function (waitFor) {
to = setTimeout(function () {
2021-03-02 16:48:38 +00:00
console.error('TIMEOUT loading iframe on the safe domain');
cb(false);
}, 5000);
SFCommonO.initIframe(waitFor);
}).nThen(function () {
// Iframe is loaded
clearTimeout(to);
cb(true);
});
});
// Test Websocket
var evWSError = Util.mkEvent(true);
assert(function (_cb, msg) {
2021-04-19 13:09:00 +00:00
var timeoutErr = 'Could not connect to the websocket server within 5 seconds.';
var cb = Util.once(Util.both(_cb, function (status) {
if (status === true) { return; }
msg.appendChild(h('span#websocket', [
status || 'Unknown websocket error',
]));
}));
var ws = new WebSocket(NetConfig.getWebsocketURL());
var to = setTimeout(function () {
console.error('Websocket TIMEOUT');
evWSError.fire();
2021-04-19 13:09:00 +00:00
cb(timeoutErr);
}, 5000);
ws.onopen = function () {
clearTimeout(to);
cb(true);
};
ws.onerror = function (err) {
clearTimeout(to);
console.error('[Websocket error]', err);
evWSError.fire();
cb('Unable to connect to the websocket server. More information may be available in your browser console ([Websocket error]).');
};
});
// Test login block
2021-04-19 13:09:00 +00:00
assert(function (_cb, msg) {
var websocketErr = "No WebSocket available";
var cb = Util.once(Util.both(_cb, function (status) {
if (status === true) { return; }
if (status === websocketErr) {
msg.appendChild(h('span', [
websocketErr,
' See ',
h('a', {
href: '#websocket',
}, 'the related websocket error'),
]));
return;
}
// else
msg.appendChild(h('span', [
"Unable to create, retrieve, or remove encrypted credentials from the server. ",
"This is most commonly caused by a mismatch between the value of the ",
h('code', 'blockPath'),
' value configured in ',
CONFIG_PATH(),
" and the corresponding settings in your reverse proxy's configuration file,",
" but it can also be explained by a websocket error. ",
RESTART_WARNING(),
]));
}));
var bytes = new Uint8Array(Login.requiredBytes);
var opt = Login.allocateBytes(bytes);
var blockUrl = Login.Block.getBlockUrl(opt.blockKeys);
var blockRequest = Login.Block.serialize("{}", opt.blockKeys);
var removeRequest = Login.Block.remove(opt.blockKeys);
2021-05-12 08:48:26 +00:00
console.warn('Testing block URL (%s). One 404 is normal.', blockUrl);
var userHash = '/2/drive/edit/000000000000000000000000';
var secret = Hash.getSecrets('drive', userHash);
opt.keys = secret.keys;
opt.channelHex = secret.channel;
2021-04-26 13:01:33 +00:00
var RT, rpc, exists, restricted;
nThen(function (waitFor) {
2021-03-02 16:48:38 +00:00
Util.fetch(blockUrl, waitFor(function (err) {
if (err) { return; } // No block found
exists = true;
}));
}).nThen(function (waitFor) {
// If WebSockets aren't working, don't wait forever here
evWSError.reg(function () {
waitFor.abort();
2021-04-19 13:09:00 +00:00
cb(websocketErr);
});
// Create proxy
Login.loadUserObject(opt, waitFor(function (err, rt) {
if (err) {
waitFor.abort();
console.error("Can't create new channel. This may also be a websocket issue.");
return void cb(false);
}
RT = rt;
var proxy = rt.proxy;
proxy.edPublic = opt.edPublic;
proxy.edPrivate = opt.edPrivate;
proxy.curvePublic = opt.curvePublic;
proxy.curvePrivate = opt.curvePrivate;
rt.realtime.onSettle(waitFor());
}));
}).nThen(function (waitFor) {
// Init RPC
Pinpad.create(RT.network, RT.proxy, waitFor(function (e, _rpc) {
if (e) {
waitFor.abort();
console.error("Can't initialize RPC", e); // INVALID_KEYS
return void cb(false);
}
rpc = _rpc;
}));
}).nThen(function (waitFor) {
// Write block
if (exists) { return; }
rpc.writeLoginBlock(blockRequest, waitFor(function (e) {
2021-04-26 13:01:33 +00:00
// we should tolerate restricted registration
// and proceed to clean up after any data we've created
if (e === 'E_RESTRICTED') {
restricted = true;
return void cb(true);
}
if (e) {
waitFor.abort();
console.error("Can't write login block", e);
return void cb(false);
}
}));
}).nThen(function (waitFor) {
2021-04-26 13:01:33 +00:00
if (restricted) { return; }
// Read block
2021-03-02 16:48:38 +00:00
Util.fetch(blockUrl, waitFor(function (e) {
if (e) {
waitFor.abort();
console.error("Can't read login block", e);
return void cb(false);
}
}));
}).nThen(function (waitFor) {
// Remove block
rpc.removeLoginBlock(removeRequest, waitFor(function (e) {
2021-04-26 13:01:33 +00:00
if (restricted) { return; } // an ENOENT is expected in the case of restricted registration, but we call this anyway to clean up any mess from previous tests.
if (e) {
waitFor.abort();
console.error("Can't remove login block", e);
console.error(blockRequest);
return void cb(false);
}
}));
}).nThen(function (waitFor) {
rpc.removeOwnedChannel(secret.channel, waitFor(function (e) {
if (e) {
waitFor.abort();
console.error("Can't remove channel", e);
return void cb(false);
}
}));
}).nThen(function () {
cb(true);
});
});
2021-02-24 08:23:59 +00:00
var sheetURL = '/common/onlyoffice/v4/web-apps/apps/spreadsheeteditor/main/index.html';
assert(function (cb, msg) {
msg.innerText = "Missing HTTP headers required for .xlsx export from sheets. ";
var url = sheetURL;
var expect = {
'cross-origin-resource-policy': 'cross-origin',
'cross-origin-embedder-policy': 'require-corp',
//'cross-origin-opener-policy': 'same-origin', // FIXME this is in our nginx config but not server.js
};
$.ajax(url, {
complete: function (xhr) {
cb(!Object.keys(expect).some(function (k) {
var response = xhr.getResponseHeader(k);
if (response !== expect[k]) {
msg.appendChild(h('span', [
'A value of ',
h('code', expect[k]),
' was expected for the ',
h('code', k),
' HTTP header, but instead a value of "',
h('code', response),
'" was received.',
]));
return true; // returning true indicates that a value is incorrect
}
}));
},
});
});
assert(function (cb, msg) {
2021-05-07 12:23:15 +00:00
msg.innerText = "Missing HTTP header required to disable Google's Floc.";
$.ajax('/?'+ (+new Date()), {
complete: function (xhr) {
cb(xhr.getResponseHeader('permissions-policy') === 'interest-cohort=()');
},
});
});
assert(function (cb, msg) {
2021-05-12 08:48:26 +00:00
msg.innerText = "This test is incorrect.";
2021-04-16 14:32:38 +00:00
return void cb(true);
/*
msg.appendChild(h('span', [
"The spreadsheet editor's code was not served with the required Content-Security Policy headers. ",
"This is most often caused by incorrectly configured sandbox parameters (",
h('code', 'httpUnsafeOrigin'),
' and ',
h('code', 'httpSafeOrigin'),
' in ',
CONFIG_PATH,
"), or settings in your reverse proxy's configuration which don't match your application server's config. ",
RESTART_WARNING(),
]));
$.ajax(sheetURL, {
complete: function (xhr) {
var csp = xhr.getResponseHeader('Content-Security-Policy');
if (!/unsafe\-eval/.test(csp)) {
// OnlyOffice requires unsafe-eval
2021-04-16 14:32:38 +00:00
console.error('CSP', csp);
return cb("expected 'unsafe-eval'");
}
if (!/unsafe\-inline/.test(csp)) {
// OnlyOffice also requires unsafe-inline
2021-04-16 14:32:38 +00:00
console.error('CSP', csp);
return cb("expected 'unsafe-inline'");
}
cb(true);
},
2021-04-16 14:32:38 +00:00
}); */
});
assert(function (cb, msg) {
msg.appendChild(h('span', [
h('code', '/api/broadcast'),
" could not be loaded. This can be caused by an outdated application server or an incorrectly configured reverse proxy. ",
"Even if the most recent code has been downloaded it's possible the application server has not been restarted. ",
"Your browser console may provide more details as to why this resource could not be loaded. ",
]));
$.ajax('/api/broadcast', {
dataType: 'text',
complete: function (xhr) {
cb(xhr.status === 200);
},
});
});
var code = function (content) {
return h('code', content);
};
var checkAPIHeaders = function (url, msg, cb) {
2021-04-30 04:04:21 +00:00
$.ajax(url, {
dataType: 'text',
complete: function (xhr) {
var allHeaders = xhr.getAllResponseHeaders();
var headers = {};
2021-04-30 04:32:48 +00:00
var duplicated = allHeaders.split('\n').some(function (header) {
2021-04-30 04:04:21 +00:00
var duplicate;
header.replace(/([^:]+):(.*)/, function (all, type, value) {
type = type.trim();
if (typeof(headers[type]) !== 'undefined') {
duplicate = true;
}
headers[type] = value.trim();
});
return duplicate;
});
2021-04-30 04:32:48 +00:00
var expect = {
'cross-origin-resource-policy': 'cross-origin',
2021-05-12 08:48:26 +00:00
'cross-origin-embedder-policy': 'require-corp',
2021-04-30 04:32:48 +00:00
};
var incorrect = false;
Object.keys(expect).forEach(function (k) {
2021-04-30 04:32:48 +00:00
var response = xhr.getResponseHeader(k);
var expected = expect[k];
if (response !== expected) {
incorrect = true;
msg.appendChild(h('p', [
'The ',
code(k),
' header for ',
code(url),
" is '",
code(response),
"' instead of '",
code(expected),
"' as expected.",
]));
2021-04-30 04:32:48 +00:00
}
});
if (duplicated || incorrect) { console.debug(allHeaders); }
2021-05-03 09:16:26 +00:00
cb(!duplicated && !incorrect);
2021-04-30 04:04:21 +00:00
},
});
};
2021-04-30 04:32:48 +00:00
var INCORRECT_HEADER_TEXT = ' was served with duplicated or incorrect headers. Compare your reverse-proxy configuration against the provided example.';
2021-04-30 04:04:21 +00:00
assert(function (cb, msg) {
2021-04-30 04:32:48 +00:00
var url = '/api/config';
msg.innerText = url + INCORRECT_HEADER_TEXT;
checkAPIHeaders(url, msg, cb);
2021-04-30 04:04:21 +00:00
});
assert(function (cb, msg) {
2021-04-30 04:32:48 +00:00
var url = '/api/broadcast';
msg.innerText = url + INCORRECT_HEADER_TEXT;
checkAPIHeaders(url, msg, cb);
2021-04-30 04:04:21 +00:00
});
2021-05-03 09:16:26 +00:00
var setWarningClass = function (msg) {
$(msg).removeClass('cp-danger').addClass('cp-warning');
};
assert(function (cb, msg) {
var email = ApiConfig.adminEmail;
if (typeof(email) === 'string' && email && email !== 'i.did.not.read.my.config@cryptpad.fr') {
return void cb(true);
}
setWarningClass(msg);
msg.appendChild(h('span', [
'This instance does not provide a valid ',
h('code', 'adminEmail'),
' which can make it difficult to contact its adminstrator to report vulnerabilities or abusive content.',
' This can be configured in ', CONFIG_PATH(), '. ',
RESTART_WARNING(),
2021-05-03 09:16:26 +00:00
]));
cb(email);
});
assert(function (cb, msg) {
var support = ApiConfig.supportMailbox;
setWarningClass(msg);
msg.appendChild(h('span', [
"This instance's encrypted support ticket functionality has not been enabled. This can make it difficult for its users to safely report issues that concern sensitive information. ",
"This can be configured via the ",
h('code', 'supportMailbox'),
" attribute in ",
CONFIG_PATH(),
". ",
RESTART_WARNING(),
2021-05-03 09:16:26 +00:00
]));
cb(support && typeof(support) === 'string' && support.length === 44);
2021-05-03 09:16:26 +00:00
});
assert(function (cb, msg) {
var adminKeys = ApiConfig.adminKeys;
if (Array.isArray(adminKeys) && adminKeys.length >= 1 && typeof(adminKeys[0]) === 'string' && adminKeys[0].length === 44) {
2021-05-03 09:16:26 +00:00
return void cb(true);
}
setWarningClass(msg);
msg.appendChild(h('span', [
"This instance has not been configured to support web administration. This can be enabled by adding a registered user's public signing key to the ",
h('code', 'adminKeys'),
' array in ',
CONFIG_PATH(),
'. ',
RESTART_WARNING(),
2021-05-03 09:16:26 +00:00
]));
cb(false);
});
if (false) {
assert(function (cb, msg) {
msg.innerText = 'fake test to simulate failure';
cb(false);
});
}
2021-02-24 08:23:59 +00:00
var row = function (cells) {
return h('tr', cells.map(function (cell) {
return h('td', cell);
}));
};
var failureReport = function (obj) {
return h('div.error', [
h('h5', obj.message),
h('table', [
row(["Failed test number", obj.test + 1]),
row(["Returned value", obj.output]),
]),
]);
};
var completed = 0;
var $progress = $('#cp-progress');
var versionStatement = function () {
return h('p', [
"This instance is running ",
h('span.cp-app-checkup-version',[
"CryptPad",
' ',
Pages.versionString,
]),
'.',
]);
};
Assert.run(function (state) {
var errors = state.errors;
var failed = errors.length;
Messages.assert_numberOfTestsPassed = "{0} / {1} tests passed.";
var statusClass = failed? 'failure': 'success';
2021-02-24 08:23:59 +00:00
2021-04-19 13:09:00 +00:00
var failedDetails = "Details found below";
var successDetails = "This checkup only tests the most common configuration issues. You may still experience errors or incorrect behaviour.";
2021-04-19 13:09:00 +00:00
var details = h('p', failed? failedDetails: successDetails);
2021-02-24 08:23:59 +00:00
var summary = h('div.summary.' + statusClass, [
versionStatement(),
2021-02-24 08:23:59 +00:00
h('p', Messages._getKey('assert_numberOfTestsPassed', [
state.passed,
state.total
2021-02-24 08:23:59 +00:00
])),
2021-04-19 13:09:00 +00:00
details,
2021-02-24 08:23:59 +00:00
]);
var report = h('div.report', [
summary,
h('div.failures', errors.map(failureReport)),
]);
$progress.remove();
2021-02-24 08:23:59 +00:00
$('body').prepend(report);
}, function (i, total) {
console.log('test '+ i +' completed');
completed++;
Messages.assert_numberOfTestsCompleted = "{0} / {1} tests completed.";
$progress.html('').append(h('div.report.pending.summary', [
versionStatement(),
h('p', [
h('i.fa.fa-spinner.fa-pulse'),
h('span', Messages._getKey('assert_numberOfTestsCompleted', [completed, total]))
])
]));
});
});