Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit e439bfd

Browse files
shickscopybara-github
authored andcommitted
Change goog.crypt.stringToByteArray to throw asynchronously when it encounters a multi-byte character in goog.DEBUG mode. We will flip the default to true in the near future.
RELNOTES: `goog.crypt.stringToByteArray` (along with `base64.encodeString` and `hash32.encodeString`) will now throw asynchronously on multi-byte characters in development. PiperOrigin-RevId: 477778082 Change-Id: I968b138e02424f76ef7af65c32c7c474d79879ff
1 parent aea7db1 commit e439bfd

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

closure/goog/crypt/crypt.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
goog.provide('goog.crypt');
1212

1313
goog.require('goog.asserts');
14+
goog.require('goog.async.throwException');
15+
16+
17+
/**
18+
* Whether to async-throw on unicode input to the legacy versions of
19+
* `goog.crypt.stringToByteArray` (i.e. when `throwSync` is false).
20+
* NOTE: The default will change to `true` soon, after notifying users.
21+
* @define {boolean}
22+
*/
23+
goog.crypt.ASYNC_THROW_ON_UNICODE_TO_BYTE =
24+
goog.define('goog.crypt.ASYNC_THROW_ON_UNICODE_TO_BYTE', goog.DEBUG);
25+
26+
27+
/**
28+
* Test-only stub to make our use of async.throwException more testable.
29+
* @const
30+
*/
31+
goog.crypt.TEST_ONLY = {};
32+
33+
34+
/** Remappable alias. */
35+
goog.crypt.TEST_ONLY.throwException = goog.async.throwException;
1436

1537

1638
/**
@@ -43,6 +65,8 @@ goog.crypt.stringToByteArray = function(str, throwSync) {
4365
var err = new Error('go/unicode-to-byte-error');
4466
if (throwSync) {
4567
throw err;
68+
} else if (goog.crypt.ASYNC_THROW_ON_UNICODE_TO_BYTE) {
69+
goog.crypt.TEST_ONLY.throwException(err);
4670
}
4771
output[p++] = c & 0xff;
4872
c >>= 8;

closure/goog/crypt/crypt_test.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
goog.module('goog.cryptTest');
88
goog.setTestOnly();
99

10+
const PropertyReplacer = goog.require('goog.testing.PropertyReplacer');
1011
const crypt = goog.require('goog.crypt');
1112
const googString = goog.require('goog.string');
13+
const recordFunction = goog.require('goog.testing.recordFunction');
1214
const testSuite = goog.require('goog.testing.testSuite');
1315

1416
const UTF8_RANGES_BYTE_ARRAY =
@@ -194,13 +196,24 @@ testSuite({
194196
},
195197

196198
testStringToByteArray() {
197-
assertArrayEquals([], crypt.stringToByteArray(''));
198-
assertArrayEquals([97, 98, 99], crypt.stringToByteArray('abc'));
199-
assertArrayEquals(
200-
[0xa0, 0x12, 0xa1], crypt.stringToByteArray('\xa0\x12\xa1'));
201-
202-
// NOTE: Currently allowed, but will become an async throw soon.
203-
assertArrayEquals([2, 1], crypt.stringToByteArray('\u0102'));
199+
const stubs = new PropertyReplacer();
200+
const stubThrowException = recordFunction();
201+
stubs.replace(crypt.TEST_ONLY, 'throwException', stubThrowException);
202+
try {
203+
assertArrayEquals([], crypt.stringToByteArray(''));
204+
assertArrayEquals([97, 98, 99], crypt.stringToByteArray('abc'));
205+
assertArrayEquals(
206+
[0xa0, 0x12, 0xa1], crypt.stringToByteArray('\xa0\x12\xa1'));
207+
if (stubThrowException.getCallCount() > 0) {
208+
throw stubThrowException.getLastCall().getArgument(0);
209+
}
210+
211+
// Test async throwing behavior.
212+
assertArrayEquals([2, 1], crypt.stringToByteArray('\u0102'));
213+
assertEquals(1, stubThrowException.getCallCount());
214+
} finally {
215+
stubs.reset();
216+
}
204217
},
205218

206219
testBinaryStringToByteArray() {

0 commit comments

Comments
 (0)