fix path traversal attack

This commit is contained in:
Shiwei Wang 2021-08-21 22:56:18 +08:00
parent 88c2f1bd9e
commit d8e8394147

View File

@ -1,10 +1,12 @@
import axios from 'axios'
import type { NextApiRequest, NextApiResponse } from 'next'
import { posix as pathPosix } from 'path'
import apiConfig from '../../config/api.json'
const basePath = pathPosix.resolve('/', apiConfig.base)
const encodePath = (path: string) => {
const encodedPath = `${apiConfig.base}${path === '/' ? '' : path}`
const encodedPath = pathPosix.join(basePath, pathPosix.resolve('/', path))
if (encodedPath === '/' || encodedPath === '') {
return ''
}