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

Base64 encode message before sending to WebView #2854

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 31 additions & 1 deletion src/utils/__tests__/encoding-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
hexToAscii,
asciiToHex,
xorHexStrings,
strToBase64,
extractApiKey,
} from '../encoding';

Expand Down Expand Up @@ -61,11 +62,40 @@ describe('asciiToHex', () => {
});
});

describe('strToBase64', () => {
test('can handle an empty string', () => {
const obj = '';
const expected = '';

const result = strToBase64(obj);

expect(result).toBe(expected);
});

test('can encode any string', () => {
const obj = {
key: 'ABCabc123',
empty: null,
array: [1, 2, 3],
};
const expected = 'W29iamVjdCBPYmplY3Rd';

const result = strToBase64(obj);

expect(result).toBe(expected);
});

test('supports unicode characters', () => {
const obj = { key: '😇😈' };
const expected = 'W29iamVjdCBPYmplY3Rd';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this does not test what you want it to test: :-p

$ echo W29iamVjdCBPYmplY3Rd | base64 -d ; echo '$'
[object Object]$

const result = strToBase64(obj);
expect(result).toBe(expected);
});
});
describe('extractApiKey', () => {
test('correctly extracts an API key that has been XORed with a OTP', () => {
const key = 'testing';
const otp = 'A8348A93A83493';

const encoded = xorHexStrings(asciiToHex(key), otp);
expect(extractApiKey(encoded, otp)).toBe(key);
});
Expand Down
5 changes: 5 additions & 0 deletions src/utils/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const base64ToHex = (bytes: string) => asciiToHex(base64.decode(bytes));

export const hexToBase64 = (hex: string) => base64.encode(hexToAscii(hex));

// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
// `base64.encode` used instead of `btoa` to support Unicode input
export const strToBase64 = (text: string): string =>
base64.encode(unescape(encodeURIComponent(text)));

// Extract an API key encoded as a hex string XOR'ed with a one time pad (OTP)
// (this is used during the OAuth flow)
export const extractApiKey = (encoded: string, otp: string) =>
Expand Down
3 changes: 2 additions & 1 deletion src/webview/MessageListWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getHtml from './html/html';
import renderMessagesAsHtml from './html/renderMessagesAsHtml';
import { getInputMessages } from './webViewHandleUpdates';
import * as webViewEventHandlers from './webViewEventHandlers';
import { strToBase64 } from '../utils/encoding';

export default class MessageListWeb extends Component<Props> {
context: Context;
Expand All @@ -29,7 +30,7 @@ export default class MessageListWeb extends Component<Props> {

sendMessages = (messages: WebviewInputMessage[]): void => {
if (this.webview && messages.length > 0) {
this.webview.postMessage(JSON.stringify(messages), '*');
this.webview.postMessage(strToBase64(JSON.stringify(messages)), '*');
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/webview/js/generatedEs3.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ var messageHandlers = {
document.addEventListener('message', function (e) {
scrollEventsDisabled = true;

var messages = JSON.parse(e.data);
var decodedData = decodeURIComponent(escape(window.atob(e.data)));
var messages = JSON.parse(decodedData);
messages.forEach(function (msg) {
messageHandlers[msg.type](msg);
});
Expand Down
3 changes: 2 additions & 1 deletion src/webview/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ const messageHandlers = {
document.addEventListener('message', e => {
scrollEventsDisabled = true;
// $FlowFixMe
const messages: WebviewInputMessage[] = JSON.parse(e.data);
const decodedData = decodeURIComponent(escape(window.atob(e.data)));
const messages: WebviewInputMessage[] = JSON.parse(decodedData);
messages.forEach((msg: WebviewInputMessage) => {
// $FlowFixMe
messageHandlers[msg.type](msg);
Expand Down