Skip to content

Commit

Permalink
Removed FileSniffer
Browse files Browse the repository at this point in the history
Signed-off-by: the-pro <[email protected]>
  • Loading branch information
the-pro committed Apr 12, 2021
1 parent c45dd69 commit 42573ad
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions routes/vulnCodeSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

const challenges = require('../data/datacache').challenges
const path = require('path')
const { FileSniffer, asArray } = require('filesniffer')
const fs = require('graceful-fs')
const realFs = require('fs')

fs.gracefulify(realFs)

const cache = {}

const fileSniff = async (paths, challenge) => {
const fileSniff = async (paths, match) => {
const matches = []
const match = new RegExp(`vuln-code-snippet start.*${challenge.key}`)
for (const currPath of paths) {
if (fs.lstatSync(currPath).isDirectory()) {
const files = fs.readdirSync(currPath)
Expand Down Expand Up @@ -45,7 +43,8 @@ exports.serveCodeSnippet = () => async (req, res, next) => {
return res.json(cache[challenge.key])
} else {
const paths = ['./server.ts', './routes', './lib', './data', './frontend/src/app']
const matches = await fileSniff(paths, challenge)
const match = new RegExp(`vuln-code-snippet start.*${challenge.key}`)
const matches = await fileSniff(paths, match)
if (matches[0]) { // TODO Currently only a single source file is supported
const source = fs.readFileSync(path.resolve(matches[0].path), 'utf8')
const snippets = source.match(`[/#]{0,2} vuln-code-snippet start.*${challenge.key}([^])*vuln-code-snippet end.*${challenge.key}`)
Expand Down Expand Up @@ -82,16 +81,9 @@ exports.serveCodeSnippet = () => async (req, res, next) => {
}

exports.challengesWithCodeSnippet = () => async (req, res, next) => {
const matches = await FileSniffer
.create()
.path('./server.ts')
.path('./routes')
.path('./lib')
.path('./data')
.path('./frontend/src/app')
.depth(1)
.collect(asArray())
.find(/vuln-code-snippet start .*/)
const match = /vuln-code-snippet start .*/
const paths = ['./server.ts', './routes', './lib', './data', './frontend/src/app']
const matches = await fileSniff(paths, match)
const challenges = matches.map(m => m.match.trim().substr(26).trim()).join(' ').split(' ')
res.json({ challenges })
}

0 comments on commit 42573ad

Please sign in to comment.