From e334c5e56b4983b01d6b3c5d67757dd493e2d640 Mon Sep 17 00:00:00 2001 From: undefined Date: Thu, 24 Jun 2021 12:45:19 +0800 Subject: [PATCH] judge: add secret config key --- packages/hydrojudge/package.json | 2 +- packages/hydrojudge/setting.yaml | 7 ++++++- packages/hydrojudge/src/cases.ts | 2 +- packages/hydrojudge/src/config.ts | 1 + packages/hydrojudge/src/hosts/hydro.ts | 4 ++-- packages/hydrojudge/src/service.ts | 4 ++-- packages/hydrojudge/src/utils.ts | 10 ++++++++++ packages/hydrooj/package.json | 2 +- packages/ui-default/components/monaco/index.js | 1 + .../components/subjective-question/index.jsx | 10 ++++++++-- packages/ui-default/package.json | 4 ++-- 11 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/hydrojudge/package.json b/packages/hydrojudge/package.json index 0c4f07d7..6667e5bf 100644 --- a/packages/hydrojudge/package.json +++ b/packages/hydrojudge/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/hydrojudge", "bin": "bin/hydrojudge.js", - "version": "2.8.11", + "version": "2.8.12", "main": "package.json", "author": "undefined ", "repository": "https://github.com/hydro-dev/Hydro.git", diff --git a/packages/hydrojudge/setting.yaml b/packages/hydrojudge/setting.yaml index 8315b162..3df93d50 100644 --- a/packages/hydrojudge/setting.yaml +++ b/packages/hydrojudge/setting.yaml @@ -63,4 +63,9 @@ env: desc: Sandbox Env default: | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin - HOME=/w \ No newline at end of file + HOME=/w +secret: + type: text + name: secret + desc: Judge Token Secret + default: Hydro-Judge-Secret \ No newline at end of file diff --git a/packages/hydrojudge/src/cases.ts b/packages/hydrojudge/src/cases.ts index 1ebd9f21..b96da053 100644 --- a/packages/hydrojudge/src/cases.ts +++ b/packages/hydrojudge/src/cases.ts @@ -229,6 +229,6 @@ export default async function readCases(folder: string, cfg: Record result.subtasks = c.subtasks; result.count = c.count; } - isValidConfig(result); + if ((!args.key) || args.key !== result.key) isValidConfig(result); return result; } diff --git a/packages/hydrojudge/src/config.ts b/packages/hydrojudge/src/config.ts index 4ee5af4b..788f775e 100644 --- a/packages/hydrojudge/src/config.ts +++ b/packages/hydrojudge/src/config.ts @@ -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) { diff --git a/packages/hydrojudge/src/hosts/hydro.ts b/packages/hydrojudge/src/hosts/hydro.ts index 9733fbe5..30f79e29 100644 --- a/packages/hydrojudge/src/hosts/hydro.ts +++ b/packages/hydrojudge/src/hosts/hydro.ts @@ -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); diff --git a/packages/hydrojudge/src/service.ts b/packages/hydrojudge/src/service.ts index 18ea36f8..1ac55dfe 100644 --- a/packages/hydrojudge/src/service.ts +++ b/packages/hydrojudge/src/service.ts @@ -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); diff --git a/packages/hydrojudge/src/utils.ts b/packages/hydrojudge/src/utils.ts index 170fd9fe..6f741d14 100644 --- a/packages/hydrojudge/src/utils.ts +++ b/packages/hydrojudge/src/utils.ts @@ -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 extends EventEmitter { queue: T[] = []; waiting: any[] = []; diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index 24a41826..7e5f51fd 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -12,7 +12,7 @@ }, "preferUnplugged": true, "dependencies": { - "@hydrooj/utils": "workspace:packages/utils", + "@hydrooj/utils": "workspace:*", "adm-zip": "^0.5.5", "ansi_up": "^5.0.1", "cac": "^6.7.3", diff --git a/packages/ui-default/components/monaco/index.js b/packages/ui-default/components/monaco/index.js index ff36a9c5..f9cdc743 100644 --- a/packages/ui-default/components/monaco/index.js +++ b/packages/ui-default/components/monaco/index.js @@ -53,6 +53,7 @@ const problemConfigSchema = { type: 'object', def: problemConfigSchemaDef, properties: { + key: { type: 'string', pattern: '[0-9a-f]{32}' }, type: { enum: ['default', 'interactive', 'submit_answer', 'subjective'] }, checker_type: { enum: ['default', 'lemon', 'syzoj', 'testlib', 'strict', 'qduoj'] }, checker: { type: 'string', pattern: '\\.' }, diff --git a/packages/ui-default/components/subjective-question/index.jsx b/packages/ui-default/components/subjective-question/index.jsx index fecda37f..e780f1a7 100644 --- a/packages/ui-default/components/subjective-question/index.jsx +++ b/packages/ui-default/components/subjective-question/index.jsx @@ -36,7 +36,10 @@ export default class SubjectiveContainer extends React.PureComponent { Textbox(args, name) { return (