server: fix runaway regexp fails parsing a massive JS file (#1331)

* server: fix runaway regexp fails parsing a massive JS file

* server: fix linting errors, better debug logs

* server: lint fix again
This commit is contained in:
Brian Mann
2018-02-17 00:26:38 -05:00
committed by GitHub
parent 302a25f34f
commit 2f8da7a7f0
5 changed files with 61 additions and 9 deletions

1
.gitignore vendored
View File

@@ -32,6 +32,7 @@ packages/example/cypress/fixtures/users.json
packages/server/.cy
packages/server/.projects
packages/server/support
packages/server/test/support/fixtures/server/huge_app.js
# CLI tool
cli/build

View File

@@ -143,7 +143,11 @@ module.exports = {
## turn off __cypress.initial by setting false here
setCookies(false, wantsInjection)
debug("received request response for #{remoteUrl} %o", { headers })
debug("received response for %o", {
url: remoteUrl
headers,
statusCode
})
encoding = headers["content-encoding"]

View File

@@ -1,22 +1,23 @@
stream = require("stream")
pumpify = require("pumpify")
replacestream = require("replacestream")
topOrParentRe = /.*(top|parent).*/g
topOrParentEqualityBeforeRe = /((?:window|self).*[!=][=]\s*(?:(?:window|self)(?:\.|\[['"]))?)(top|parent)/g
topOrParentEqualityBeforeRe = /((?:window|self)(?:\.|\[['"](?:top|self)['"]\])?\s*[!=][=]\s*(?:(?:window|self)(?:\.|\[['"]))?)(top|parent)/g
topOrParentEqualityAfterRe = /(top|parent)((?:["']\])?\s*[!=][=].*(?:window|self))/g
topOrParentLocationOrFramesRe = /([^\da-zA-Z])(top|parent)([.])(location|frames)/g
replacer = (match, p1, offset, string) ->
match
strip = (html) ->
html
.replace(topOrParentEqualityBeforeRe, "$1self")
.replace(topOrParentEqualityAfterRe, "self$2")
.replace(topOrParentLocationOrFramesRe, "$1self$3$4")
strip = (html) ->
html.replace(topOrParentRe, replacer)
stripStream = ->
replacestream(topOrParentRe, replacer)
pumpify(
replacestream(topOrParentEqualityBeforeRe, "$1self")
replacestream(topOrParentEqualityAfterRe, "self$2")
replacestream(topOrParentLocationOrFramesRe, "$1self$3$4")
)
module.exports = {
strip

View File

@@ -135,6 +135,7 @@
"pluralize": "^3.0.0",
"pretty-error": "^2.1.0",
"progress": "^1.1.8",
"pumpify": "^1.4.0",
"ramda": "^0.24.0",
"randomstring": "^1.1.5",
"replacestream": "^4.0.3",

View File

@@ -2377,6 +2377,51 @@ describe "Routes", ->
"if (self !== self) { }"
)
it "does not die rewriting a huge JS file", ->
pathToHugeAppJs = Fixtures.path("server/huge_app.js")
getHugeFileGist = ->
rp("https://s3.amazonaws.com/assets.cypress.io/huge_app.js")
.then (resp) ->
fs
.writeFileAsync(pathToHugeAppJs, resp)
.return(resp)
fs
.readFileAsync(pathToHugeAppJs, "utf8")
.catch(getHugeFileGist)
.then (hugeJsFile) =>
nock(@server._remoteOrigin)
.get("/app.js")
.reply 200, hugeJsFile, {
"Content-Type": "application/javascript"
}
reqTime = new Date()
@rp("http://www.google.com/app.js")
.then (res) ->
expect(res.statusCode).to.eq(200)
reqTime = new Date() - reqTime
## shouldn't be more than 500ms
expect(reqTime).to.be.lt(500)
# b = res.body
#
# console.time("1")
# b.replace(topOrParentEqualityBeforeRe, "$self")
# console.timeEnd("1")
#
# console.time("2")
# b.replace(topOrParentEqualityAfterRe, "self$2")
# console.timeEnd("2")
#
# console.time("3")
# b.replace(topOrParentLocationOrFramesRe, "$1self$3$4")
# console.timeEnd("3")
describe "off with config", ->
beforeEach ->
@setup("http://www.google.com", {