judge: add secret config key

This commit is contained in:
undefined committed 2021-06-24 12:45:19 +08:00
1 parent 37ce0a4f66
commit e334c5e56b
11 files changed
+35 -12

No files matched your search

+1 -1
View File
@@ -229,6 +229,6 @@ export default async function readCases(folder: string, cfg: Record<string, any>
result.subtasks = c.subtasks;
result.count = c.count;
}
isValidConfig(result);
if ((!args.key) || args.key !== result.key) isValidConfig(result);
return result;
}
+1
View File
@@ -25,6 +25,7 @@ let config = {
env: `\
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/w`,
secret: String.random(32),
};
export function getConfig(key: string) {
+2 -2
View File
@@ -9,7 +9,7 @@ import { ObjectID } from 'mongodb';
import { LangConfig } from '@hydrooj/utils/lib/lang';
import * as tmpfs from '../tmpfs';
import log from '../log';
import { compilerText, Queue } from '../utils';
import { compilerText, md5, Queue } from '../utils';
import { getConfig } from '../config';
import { FormatError, CompileError, SystemError } from '../error';
import { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } from '../status';
@@ -111,7 +111,7 @@ class JudgeTask {
this.config = await readCases(
this.folder,
{ ...this.config, detail: this.session.config.detail },
{ next: this.next },
{ next: this.next, key: md5(`${this.domainId}${this.pid}${getConfig('secret')}`) },
);
this.stat.judge = new Date();
await judge[this.config.type || 'default'].judge(this);
+2 -2
View File
@@ -11,7 +11,7 @@ import * as sysinfo from './sysinfo';
import * as tmpfs from './tmpfs';
import { FormatError, CompileError, SystemError } from './error';
import { STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR } from './status';
import { compilerText } from './utils';
import { compilerText, md5 } from './utils';
import readCases from './cases';
import { getConfig } from './config';
@@ -233,7 +233,7 @@ async function postInit() {
this.config = await readCases(
this.folder,
this.config,
{ next: this.next },
{ next: this.next, key: md5(`${this.domainId}${this.pid}${getConfig('secret')}`) },
);
this.stat.judge = new Date();
await judge[this.config.type || 'default'].judge(this);
+10
View File
@@ -1,4 +1,5 @@
import fs from 'fs-extra';
import crypto from 'crypto';
import path from 'path';
import { parse } from 'shell-quote';
import _ from 'lodash';
@@ -14,6 +15,15 @@ export function parseFilename(filePath: string) {
return t[t.length - 1];
}
const encrypt = (algorithm, content) => {
const hash = crypto.createHash(algorithm);
hash.update(content);
return hash.digest('hex');
};
export const sha1 = (content: string) => encrypt('sha1', content);
export const md5 = (content: string) => encrypt('md5', content);
export class Queue<T> extends EventEmitter {
queue: T[] = [];
waiting: any[] = [];