优化csrf_token检查

This commit is contained in:
undefined committed 2020-07-04 22:49:02 +08:00
1 parent fb5c29226b
commit c9b6412527
10 files changed
+77 -185

No files matched your search

+25 -13
View File
@@ -10,6 +10,25 @@ const hpm = require('../lib/hpm');
const loader = require('../loader');
const check = require('../check');
function set(key, value) {
if (setting.SYSTEM_SETTINGS_BY_KEY[key]) {
const s = setting.SYSTEM_SETTINGS_BY_KEY[key];
if (s.flag & setting.FLAG_DISABLED) return undefined;
if (s.type === 'boolean') {
if (value === 'on') return true;
return false;
}
if (s.type === 'number') {
if (!Number.isSafeInteger(parseInt(value, 10))) {
throw new ValidationError(key);
}
return parseInt(value, 10);
}
return value;
}
return undefined;
}
class SystemHandler extends Handler {
async prepare() {
this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
@@ -147,21 +166,14 @@ class SystemSettingHandler extends SystemHandler {
const tasks = [];
for (const key in args) {
if (typeof args[key] === 'object') {
const subtasks = [];
for (const sub in args[key]) {
const s = setting.SYSTEM_SETTINGS_BY_KEY[`${key}.${sub}`];
if (s && !((s.flag & setting.FLAG_SECRET) && args[key][sub] === '')) {
if (s.ui === 'number') args[key][sub] = Number(args[key][sub]);
subtasks.push(system.set(`${key}.${sub}`, args[key][sub]));
for (const subkey in args[key]) {
console.log(`${key}.${subkey}`, set(`${key}.${subkey}`, args[key][subkey]));
if (typeof set(`${key}.${subkey}`, args[key][subkey]) !== 'undefined') {
tasks.push(system.set(`${key}.${subkey}`, set(`${key}.${subkey}`, args[key][subkey])));
}
}
tasks.push(Promise.all(subtasks));
} else {
const s = setting.SYSTEM_SETTINGS_BY_KEY[key];
if (s && !((s.flag & setting.FLAG_SECRET) && args[key] === '')) {
if (s.ui === 'number') args[key] = Number(args[key]);
tasks.push(system.set(key, args[key]));
}
} else if (typeof set(key, args[key]) !== 'undefined') {
tasks.push(system.set(key, set(key, args[key])));
}
}
await Promise.all(tasks);
+5 -7
View File
@@ -12,7 +12,7 @@ const {
PERM_CREATE_PROBLEM, PERM_READ_PROBLEM_DATA, PERM_EDIT_PROBLEM,
PERM_VIEW_PROBLEM_SOLUTION, PERM_CREATE_PROBLEM_SOLUTION,
PERM_EDIT_PROBLEM_SOLUTION, PERM_DELETE_PROBLEM_SOLUTION, PERM_EDIT_PROBLEM_SOLUTION_REPLY,
PERM_REPLY_PROBLEM_SOLUTION,
PERM_REPLY_PROBLEM_SOLUTION, PERM_EDIT_PROBLEM_SELF,
},
PRIV: {
PRIV_USER_PROFILE, PRIV_JUDGE,
@@ -202,11 +202,8 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
const { lang, code } = this.request.body;
const rid = await record.add(domainId, {
uid: this.user._id, lang, code, pid: this.pdoc.docId,
});
await Promise.all([
record.judge(domainId, rid),
user.incDomain(domainId, this.user._id, 'nSubmit'),
]);
}, true);
await user.incDomain(domainId, this.user._id, 'nSubmit');
this.response.body = { rid };
this.response.redirect = this.url('record_detail', { rid });
}
@@ -264,6 +261,7 @@ class ProblemStatisticsHandler extends ProblemDetailHandler {
class ProblemManageHandler extends ProblemDetailHandler {
async prepare() {
if (this.pdoc.owner !== this.user._id) this.checkPerm(PERM_EDIT_PROBLEM);
else this.checkPerm(PERM_EDIT_PROBLEM_SELF);
}
}
@@ -475,7 +473,7 @@ class ProblemCreateHandler extends Handler {
pid, hidden,
});
this.response.body = { pid };
this.response.redirect = `/p/${pid}/settings`;
this.response.redirect = this.url('problem_settings', { pid });
}
}
-98
View File
@@ -1,98 +0,0 @@
const domain = require('../model/domain');
const system = require('../model/system');
const setting = require('../model/setting');
const { Route, Handler } = require('../service/server');
const { PRIV_EDIT_SYSTEM } = require('../model/builtin').PRIV;
const hpm = require('../lib/hpm');
const loader = require('../loader');
class SystemHandler extends Handler {
async prepare({ domainId }) {
this.checkPriv(PRIV_EDIT_SYSTEM);
this.domain = await domain.get(domainId);
}
}
class SystemDashboardHandler extends SystemHandler {
async get() {
const path = [
['Hydro', 'homepage'],
['manage', null],
['manage_dashboard', null],
];
this.response.template = 'manage_dashboard.html';
this.response.body = { domain: this.domain, path };
}
}
class SystemModuleHandler extends SystemHandler {
async get() {
const installed = await hpm.getInstalled();
const path = [
['Hydro', 'homepage'],
['manage', null],
['manage_module', null],
];
this.response.body = { installed, active: loader.active, path };
this.response.template = 'manage_module.html';
}
async postInstall({ url, id }) {
await hpm.install(url, id);
this.back();
}
async postDelete({ id }) {
await hpm.del(id);
this.back();
}
}
class SystemSettingHandler extends SystemHandler {
async get() {
this.response.template = 'manage_settings.html';
const current = {};
const settings = setting.SYSTEM_SETTINGS;
for (const s of settings) {
// eslint-disable-next-line no-await-in-loop
current[s.key] = await system.get(s.key);
}
this.response.body = {
current, settings,
};
}
async post(args) {
const tasks = [];
for (const key in args) {
if (typeof args[key] === 'object') {
const subtasks = [];
for (const sub in args[key]) {
if (setting.SYSTEM_SETTINGS_BY_KEY[`${key}.${sub}`]) {
const s = setting.SYSTEM_SETTINGS_BY_KEY[`${key}.${sub}`];
if (s.ui === 'checkbox') {
if (args[key][sub] === 'on') {
subtasks.push(system.set(`${key}.${sub}`, true));
} else {
subtasks.push(system.set(`${key}.${sub}`, false));
}
} else {
subtasks.push(system.set(`${key}.${sub}`, args[key][sub]));
}
}
}
tasks.push(Promise.all(subtasks));
} else tasks.push(system.set(key, args[key]));
}
await Promise.all(tasks);
this.back();
}
}
async function apply() {
Route('system_dashboard', '/system', SystemDashboardHandler);
Route('system_module', '/system/module', SystemModuleHandler);
Route('system_setting', '/system/setting', SystemSettingHandler);
}
global.Hydro.handler.manage = module.exports = apply;