Skip to content

Commit a7e01b4

Browse files
authored
Merge pull request #51 from kalviumcommunity/enh/programming-mf
Enhance programming mf evaluation logic to account for integrity
2 parents d77e181 + affe276 commit a7e01b4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

services/code.service.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const axios = require('axios')
2323
const supportedLanguages = require('../enums/supportedLanguages')
2424
const { generate } = require('@builder.io/sqlgenerate')
2525
const parser = require('sqlite-parser')
26+
const crypto = require('crypto')
2627

2728
const _runScript = async (cmd, res, runMemoryCheck = false) => {
2829
let initialMemory = 0
@@ -802,6 +803,24 @@ const _preCleanUp = async () => {
802803
}
803804
}
804805

806+
const _checkIntegrity = async (non_editable_files) => {
807+
for (const [filePath, expectedHash] of Object.entries(non_editable_files)) {
808+
try {
809+
const fullPath = path.join(appConfig.multifile.workingDir, filePath)
810+
const fileContent = await fs.promises.readFile(fullPath)
811+
const actualHash = crypto.createHash('sha256').update(fileContent).digest('hex')
812+
if (actualHash !== expectedHash) {
813+
logger.warn(`Integrity check failed for file: ${filePath}`)
814+
return false
815+
}
816+
} catch (error) {
817+
logger.error(`Error reading file ${filePath}: ${error.message}`)
818+
return false
819+
}
820+
}
821+
return true
822+
}
823+
805824
const _executeMultiFile = async (req, res, response) => {
806825
logger.info(`serving ${req.type}`)
807826
try {
@@ -815,6 +834,10 @@ const _executeMultiFile = async (req, res, response) => {
815834

816835
try {
817836
let jasmineResults
837+
if(req?.non_editable_files) {
838+
const isValidSubmission = await _checkIntegrity(req.non_editable_files)
839+
if(!isValidSubmission) throw new Error(`A non editable file has been modified, exiting...`)
840+
}
818841
if (req.type === FRONTEND_STATIC_JASMINE) {
819842
const staticServerInstance = await _startStaticServer(appConfig.multifile.staticServerPath)
820843
jasmineResults = await _runTests()

validators/code.validator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const isValidForExecute = async (body) => {
3939
then: Joi.required(),
4040
otherwise: Joi.forbidden(),
4141
}),
42+
non_editable_files: Joi.object().pattern(
43+
Joi.string(),
44+
Joi.string().pattern(/^[a-fA-F0-9]{64}$/)
45+
).optional(),
4246
points: Joi.number().integer().optional(), // totalScore
4347
hasInputFiles: Joi.bool(),
4448
args: Joi.string(),

0 commit comments

Comments
 (0)