Unverified Commit 8dd71851 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Check for the user's home directory in the build output (#6661)

* Check for the user's home directory in the build output

* Output files on windows for debugging

* add process cwd

* try more paths

* Add branch for winblows

* Print info on error

* set proper env

* try fix
parent 0ae34c9a
...@@ -5,7 +5,7 @@ import { join } from 'path' ...@@ -5,7 +5,7 @@ import { join } from 'path'
import { import {
pkg, pkg,
nextServer, nextServer,
nextBuild, runNextCommand,
startApp, startApp,
stopApp, stopApp,
renderViaHTTP, renderViaHTTP,
...@@ -28,7 +28,9 @@ const context = {} ...@@ -28,7 +28,9 @@ const context = {}
describe('Production Usage', () => { describe('Production Usage', () => {
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) await runNextCommand(['build', appDir], {
spawnOptions: { env: { ...process.env, NODE_ENV: 'production' } }
})
app = nextServer({ app = nextServer({
dir: join(__dirname, '../'), dir: join(__dirname, '../'),
dev: false, dev: false,
......
/* eslint-env jest */ /* eslint-env jest */
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import { join } from 'path' import { join, resolve as resolvePath } from 'path'
import { renderViaHTTP, getBrowserBodyText, waitFor } from 'next-test-utils' import { renderViaHTTP, getBrowserBodyText, waitFor } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { homedir } from 'os'
// Does the same evaluation checking for INJECTED for 15 seconds, triggering every 500ms // Does the same evaluation checking for INJECTED for 15 seconds, triggering every 500ms
async function checkInjected (browser) { async function checkInjected (browser) {
...@@ -37,6 +39,38 @@ module.exports = (context) => { ...@@ -37,6 +39,38 @@ module.exports = (context) => {
} }
}) })
it("should not leak the user's home directory into the build", async () => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
const readPath = join(__dirname, `../.next/static/${buildId}`)
const buildFiles = await recursiveReadDir(readPath, /\.js$/)
if (buildFiles.length < 1) {
throw new Error('Could not locate any build files')
}
const homeDir = homedir()
buildFiles.forEach(buildFile => {
const content = readFileSync(join(readPath, buildFile), 'utf8')
if (content.includes(homeDir)) {
throw new Error(
`Found the user's home directory in: ${buildFile}, ${homeDir}\n\n${content}`
)
}
const checkPathProject = resolvePath(__dirname, ...Array(5).fill('..'))
if (
content.includes(checkPathProject) ||
(process.platform.match(/win/) &&
content.includes(checkPathProject.replace(/\\/g, '\\\\')))
) {
throw new Error(
`Found the project path in: ${buildFile}, ${checkPathProject}\n\n${content}`
)
}
})
})
it('should prevent URI based XSS attacks', async () => { it('should prevent URI based XSS attacks', async () => {
const browser = await webdriver(context.appPort, '/\',document.body.innerHTML="INJECTED",\'') const browser = await webdriver(context.appPort, '/\',document.body.innerHTML="INJECTED",\'')
await checkInjected(browser) await checkInjected(browser)
......
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