Commit 8c8c08ac authored by Arunoda Susiripala's avatar Arunoda Susiripala

Apply the fix for the path traversal security issue.

More info: https://github.com/zeit/next.js/releases/tag/4.2.3
parent aa096257
...@@ -55,6 +55,7 @@ export default class Server { ...@@ -55,6 +55,7 @@ export default class Server {
dev, dev,
staticMarkup, staticMarkup,
dir: this.dir, dir: this.dir,
dist: this.dist,
hotReloader: this.hotReloader, hotReloader: this.hotReloader,
buildStats: this.buildStats, buildStats: this.buildStats,
buildId: this.buildId, buildId: this.buildId,
......
import { join } from 'path' import { join, sep } from 'path'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { createElement } from 'react' import { createElement } from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server' import { renderToString, renderToStaticMarkup } from 'react-dom/server'
...@@ -122,18 +122,37 @@ async function doRender (req, res, pathname, query, { ...@@ -122,18 +122,37 @@ async function doRender (req, res, pathname, query, {
return '<!DOCTYPE html>' + renderToStaticMarkup(doc) return '<!DOCTYPE html>' + renderToStaticMarkup(doc)
} }
function isServeableUrl (path, opts) {
if (
path.indexOf(join(opts.dir, opts.dist) + sep) !== 0 &&
path.indexOf(join(opts.dir, 'static') + sep) !== 0
) {
// Seems like the user is trying to traverse the filesystem.
return false
}
return true
}
export async function renderScript (req, res, page, opts) { export async function renderScript (req, res, page, opts) {
try { try {
const dist = getConfig(opts.dir).distDir const dist = getConfig(opts.dir).distDir
const path = join(opts.dir, dist, 'bundles', 'pages', page) const path = join(opts.dir, dist, 'bundles', 'pages', page)
const realPath = await resolvePath(path) const realPath = await resolvePath(path)
if (!isServeableUrl(realPath, opts)) {
res.statusCode = 404
res.end('404')
return
}
await serveStatic(req, res, realPath) await serveStatic(req, res, realPath)
} catch (err) { } catch (err) {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
renderScriptError(req, res, page, err, {}, opts) renderScriptError(req, res, page, err, {}, opts)
return return
} }
throw err throw err
} }
} }
......
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import dynamicImportTests from '../../basic/test/dynamic' import dynamicImportTests from '../../basic/test/dynamic'
import { readFileSync } from 'fs'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
let appPort let appPort
...@@ -110,6 +111,21 @@ describe('Production Usage', () => { ...@@ -110,6 +111,21 @@ describe('Production Usage', () => {
const data = await renderViaHTTP(appPort, '/static/data/item.txt') const data = await renderViaHTTP(appPort, '/static/data/item.txt')
expect(data).toBe('item') expect(data).toBe('item')
}) })
it('should only access files inside .next directory', async () => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
const pathsToCheck = [
`/_next/${buildId}/page/../../../info`,
`/_next/${buildId}/page/../../../info.js`,
`/_next/${buildId}/page/../../../info.json`
]
for (const path of pathsToCheck) {
const data = await renderViaHTTP(appPort, path)
expect(data.includes('cool-version')).toBeFalsy()
}
})
}) })
describe('X-Powered-By header', () => { describe('X-Powered-By header', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment