Skip to content

Commit

Permalink
Merge pull request #51 from kalviumcommunity/enh/programming-mf
Browse files Browse the repository at this point in the history
Enhance programming mf evaluation logic to account for integrity
  • Loading branch information
anipnwr7777 authored Jun 25, 2024
2 parents d77e181 + affe276 commit a7e01b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions services/code.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const axios = require('axios')
const supportedLanguages = require('../enums/supportedLanguages')
const { generate } = require('@builder.io/sqlgenerate')
const parser = require('sqlite-parser')
const crypto = require('crypto')

const _runScript = async (cmd, res, runMemoryCheck = false) => {
let initialMemory = 0
Expand Down Expand Up @@ -802,6 +803,24 @@ const _preCleanUp = async () => {
}
}

const _checkIntegrity = async (non_editable_files) => {
for (const [filePath, expectedHash] of Object.entries(non_editable_files)) {
try {
const fullPath = path.join(appConfig.multifile.workingDir, filePath)
const fileContent = await fs.promises.readFile(fullPath)
const actualHash = crypto.createHash('sha256').update(fileContent).digest('hex')
if (actualHash !== expectedHash) {
logger.warn(`Integrity check failed for file: ${filePath}`)
return false
}
} catch (error) {
logger.error(`Error reading file ${filePath}: ${error.message}`)
return false
}
}
return true
}

const _executeMultiFile = async (req, res, response) => {
logger.info(`serving ${req.type}`)
try {
Expand All @@ -815,6 +834,10 @@ const _executeMultiFile = async (req, res, response) => {

try {
let jasmineResults
if(req?.non_editable_files) {
const isValidSubmission = await _checkIntegrity(req.non_editable_files)
if(!isValidSubmission) throw new Error(`A non editable file has been modified, exiting...`)
}
if (req.type === FRONTEND_STATIC_JASMINE) {
const staticServerInstance = await _startStaticServer(appConfig.multifile.staticServerPath)
jasmineResults = await _runTests()
Expand Down
4 changes: 4 additions & 0 deletions validators/code.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const isValidForExecute = async (body) => {
then: Joi.required(),
otherwise: Joi.forbidden(),
}),
non_editable_files: Joi.object().pattern(
Joi.string(),
Joi.string().pattern(/^[a-fA-F0-9]{64}$/)
).optional(),
points: Joi.number().integer().optional(), // totalScore
hasInputFiles: Joi.bool(),
args: Joi.string(),
Expand Down

0 comments on commit a7e01b4

Please sign in to comment.