Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decoding unicode values in the config object passed to the browser #5451

Merged
merged 4 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,6 @@ linux-workflow: &linux-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- build-npm-package
- build-binary:
Expand All @@ -1014,7 +1013,6 @@ linux-workflow: &linux-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- build-binary
- test-binary-and-npm-against-other-projects:
Expand All @@ -1023,7 +1021,6 @@ linux-workflow: &linux-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- upload-npm-package
- upload-binary
Expand All @@ -1032,7 +1029,6 @@ linux-workflow: &linux-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- build-npm-package
- build-binary
Expand All @@ -1042,7 +1038,6 @@ linux-workflow: &linux-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- build-npm-package
- build-binary
Expand All @@ -1051,7 +1046,6 @@ linux-workflow: &linux-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- build-npm-package
- build-binary
Expand Down Expand Up @@ -1083,7 +1077,6 @@ mac-workflow: &mac-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- Mac NPM package

Expand All @@ -1095,7 +1088,6 @@ mac-workflow: &mac-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- Mac build

Expand Down Expand Up @@ -1148,7 +1140,6 @@ mac-workflow: &mac-workflow
branches:
only:
- develop
- use-correct-folder
requires:
- Mac NPM package upload
- Mac binary upload
Expand Down
23 changes: 23 additions & 0 deletions packages/runner/src/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import path from 'path'

export default {
/**
* Correctly decodes Unicode string in encoded in base64
* @see https://github.com/cypress-io/cypress/issues/5435
* @see https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings
*
* @example
```
Buffer.from(JSON.stringify({state: '🙂'})).toString('base64')
// 'eyJzdGF0ZSI6IvCfmYIifQ=='
// "window.atob" does NOT work
// atob('eyJzdGF0ZSI6IvCfmYIifQ==')
// "{"state":"🙂"}"
// but this function works
b64DecodeUnicode('eyJzdGF0ZSI6IvCfmYIifQ==')
'{"state":"🙂"}'
```
*/
b64DecodeUnicode (str) {
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
return decodeURIComponent(atob(str).split('').map(function (c) {
return `%${(`00${c.charCodeAt(0).toString(16)}`).slice(-2)}`
}).join(''))
},

hasSpecFile () {
return !!location.hash
},
Expand Down
14 changes: 14 additions & 0 deletions packages/runner/src/lib/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import util from './util'

describe('util', () => {
context('b64DecodeUnicode', () => {
it('decodes unicode string correctly', () => {
// https://github.com/cypress-io/cypress/issues/5435
const s = '🙂 привет 🌎'
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
const encoded = Buffer.from(s).toString('base64')
const decoded = util.b64DecodeUnicode(encoded)

expect(decoded).to.equal(s)
})
})
})
3 changes: 2 additions & 1 deletion packages/runner/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { render } from 'react-dom'

import State from './lib/state'
import Container from './app/container'
import util from './lib/util'

configure({ enforceActions: 'strict' })

const Runner = {
start (el, base64Config) {
action('started', () => {
const config = JSON.parse(atob(base64Config))
const config = JSON.parse(util.b64DecodeUnicode(base64Config))

const state = new State((config.state || {}).reporterWidth)

Expand Down
2 changes: 2 additions & 0 deletions packages/server/lib/controllers/runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = {
debug("serving runner index.html with config %o",
_.pick(config, "version", "platform", "arch", "projectName")
)
# log the env object's keys without values to avoid leaking sensitive info
debug("env object has the following keys: %s", _.keys(config.env).join(", "))

## base64 before embedding so user-supplied contents can't break out of <script>
## https://github.com/cypress-io/cypress/issues/4952
Expand Down