File tree 2 files changed +14
-5
lines changed
npm-packages/udf-tests/convex/js_builtins
2 files changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -32,16 +32,21 @@ pub fn op_atob<'b, P: OpProvider<'b>>(
32
32
encoded : String ,
33
33
) -> anyhow:: Result < JsonValue > {
34
34
let mut encoded = encoded;
35
+ // https://infra.spec.whatwg.org/#forgiving-base64
35
36
encoded. retain ( |c| !c. is_ascii_whitespace ( ) ) ;
36
- let bytes = match base64:: decode ( encoded) {
37
+ // Per forgiving-base64 we need to allow trailing bits.
38
+ // This is a bit *too* forgiving since this version of base64 allows
39
+ // improper padding like in "39=", whereas the specification says that
40
+ // should be an error.
41
+ let bytes = match base64:: decode_config (
42
+ encoded,
43
+ base64:: STANDARD_NO_PAD . decode_allow_trailing_bits ( true ) ,
44
+ ) {
37
45
Ok ( bytes) => bytes,
38
46
Err ( err) => return Ok ( json ! ( { "error" : err. to_string( ) } ) ) ,
39
47
} ;
40
48
41
- let decoded: String = bytes
42
- . into_iter ( )
43
- . map ( |c| std:: char:: from_u32 ( c as u32 ) . expect ( "all u8s are valid characters" ) )
44
- . collect ( ) ;
49
+ let decoded: String = bytes. into_iter ( ) . map ( char:: from) . collect ( ) ;
45
50
Ok ( json ! ( { "decoded" : decoded } ) )
46
51
}
47
52
Original file line number Diff line number Diff line change @@ -17,6 +17,10 @@ function atobSuccess() {
17
17
assert . strictEqual ( decoded , "hello world" ) ;
18
18
const latin1 = String . fromCharCode ( 151 ) ;
19
19
assert . strictEqual ( atob ( "lw==" ) , latin1 ) ;
20
+ // allow non-canonical encodings (proper encoding of "C" should be "Qw==")
21
+ assert . strictEqual ( atob ( "Qx==" ) , "C" ) ;
22
+ // allow improper padding (even though the spec says this should error)
23
+ assert . strictEqual ( atob ( "Qx=" ) , "C" ) ;
20
24
}
21
25
22
26
function atobWithAsciiWhitespace ( ) {
You can’t perform that action at this time.
0 commit comments