<commit_before>/*
 * Copyright (c) 2014-2021 <NAME>.
 * SPDX-License-Identifier: MIT
 */

const challenges = require('../data/datacache').challenges
const fs = require('fs')

module.exports = function serveCodeSnippet () {
  return (req, res, next) => {
    const challenge = challenges[req.params.challenge]
    if (challenge) {
      const source = fs.readFileSync('routes/login.js', 'utf8') // TODO Instead find file(s) containing "vuln-code-snippet start [challengeKey, otherChallengeKey, ...]"
      let snippet = source.match(`// vuln-code-snippet start [.*${challenge}.*](.|\\r\\n)*vuln-code-snippet end [.*${challenge}.*]`)
      if (snippet) {
        snippet = snippet[0]

        const lines = snippet.split('\r\n')
        let vulnLine
        for (let i = 0; i < lines.length; i++) {
          if (/vuln-code-snippet vuln-line/.exec(lines[i])) {
            vulnLine = i
            break
          }
        }

        snippet = snippet.replace(/.*\/\/ vuln-code-snippet hide-lin1e/g, '')
        snippet = snippet.replace(/\/\/ vuln-code-snippet .*/g, '')
        snippet = snippet.trim()

        return res.json({ snippet, vulnLine })
      } else {
        res.status(404).json({ status: 'error', error: 'No code snippet available for: ' + req.params.challenge })
      }
    } else {
      res.status(412).json({ status: 'error', error: 'Unknown challenge key: ' + req.params.challenge })
    }
  }
}<commit_msg>Access demo source file from root folder<commit_after>/*
 * Copyright (c) 2014-2021 <NAME>.
 * SPDX-License-Identifier: MIT
 */

const challenges = require('../data/datacache').challenges
const fs = require('fs')

module.exports = function serveCodeSnippet () {
  return (req, res, next) => {
    const challenge = challenges[req.params.challenge]
    if (challenge) {
      const source = fs.readFileSync('./routes/login.js', 'utf8') // TODO Instead find file(s) containing "vuln-code-snippet start [challengeKey, otherChallengeKey, ...]"
      let snippet = source.match(`// vuln-code-snippet start [.*${challenge}.*](.|\\r\\n)*vuln-code-snippet end [.*${challenge}.*]`)
      if (snippet) {
        snippet = snippet[0]

        const lines = snippet.split('\r\n')
        let vulnLine
        for (let i = 0; i < lines.length; i++) {
          if (/vuln-code-snippet vuln-line/.exec(lines[i])) {
            vulnLine = i
            break
          }
        }

        snippet = snippet.replace(/.*\/\/ vuln-code-snippet hide-line/g, '')
        snippet = snippet.replace(/\/\/ vuln-code-snippet .*/g, '')
        snippet = snippet.trim()

        return res.json({ snippet, vulnLine })
      } else {
        res.status(404).json({ status: 'error', error: 'No code snippet available for: ' + req.params.challenge })
      }
    } else {
      res.status(412).json({ status: 'error', error: 'Unknown challenge key: ' + req.params.challenge })
    }
  }
}
{'fraction_non_alphanumeric': 0.17301038062283736, 'fraction_numerical': 0.012110726643598616, 'mean_word_length': 2.4375743162901307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2979722', 'n_tokens_mistral': 997, 'n_tokens_neox': 917, 'n_words': 210}