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

Crypto: Digest hash with inputEncoding as 'binary' throws #3530

Closed
MustansirZia opened this issue Jul 5, 2023 · 2 comments · Fixed by #3587
Closed

Crypto: Digest hash with inputEncoding as 'binary' throws #3530

MustansirZia opened this issue Jul 5, 2023 · 2 comments · Fixed by #3587
Labels
bug Something isn't working node.js Compatibility with Node.js APIs

Comments

@MustansirZia
Copy link

What version of Bun is running?

0.6.13 and 0.6.14

What platform is your computer?

Darwin 20.6.0 arm64 arm

What steps can reproduce the bug?

function sha1(msg) {
    const hash = Crypto.createHash('sha1');
    hash.update(msg, 'binary');
    return hash.digest('binary');
  }
  
  console.log(sha1('abc'));

What is the expected behavior?

I should see the hash value encoded in binary.

What do you see instead?

14 |     const hash = Crypto.createHash('sha1');
15 |     hash.update(msg, 'binary');
16 |     return hash.digest('binary');
17 |   }
18 | 
19 |   sha1('abc');
      ^
TypeError: Unexpected encoding
 code: "ERR_INVALID_ARG_TYPE"

Additional information

A similar issue was fixed but it was the case of hash.update(msg, 'binary'). This is happening when we're using .digest (hash.digest('binary')).

@MustansirZia MustansirZia added the bug Something isn't working label Jul 5, 2023
@MustansirZia
Copy link
Author

MustansirZia commented Jul 5, 2023

On a side note. It is disallowing us to use TypeORM with mysql driver with Bun.

@Hanaasagi
Copy link
Collaborator

Seems like Latin1 encoding needs to be handled here.

pub fn encodeWithSize(encoding: Encoding, globalThis: *JSC.JSGlobalObject, comptime size: usize, input: *const [size]u8) JSC.JSValue {
switch (encoding) {
.base64 => {
var base64: [std.base64.standard.Encoder.calcSize(size)]u8 = undefined;
const len = bun.base64.encode(&base64, input);
return JSC.ZigString.init(base64[0..len]).toValueGC(globalThis);
},
.base64url => {
var buf: [std.base64.url_safe.Encoder.calcSize(size) + "data:;base64,".len]u8 = undefined;
var encoded = std.base64.url_safe.Encoder.encode(buf["data:;base64,".len..], input);
buf[0.."data:;base64,".len].* = "data:;base64,".*;
const result = JSC.ZigString.init(buf[0 .. "data:;base64,".len + encoded.len]).toValueGC(globalThis);
return result;
},
.hex => {
var buf: [size * 4]u8 = undefined;
var out = std.fmt.bufPrint(&buf, "{}", .{std.fmt.fmtSliceHexLower(input)}) catch unreachable;
const result = JSC.ZigString.init(out).toValueGC(globalThis);
return result;
},
else => {
globalThis.throwInvalidArguments("Unexpected encoding", .{});
return JSC.JSValue.zero;
},
}
}

diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig
index 96d04636..a8bf4fea 100644
--- a/src/bun.js/node/types.zig
+++ b/src/bun.js/node/types.zig
@@ -541,8 +541,12 @@ pub const Encoding = enum(u8) {
                 const result = JSC.ZigString.init(out).toValueGC(globalThis);
                 return result;
             },
+            .latin1 => {
+                const result = JSC.ZigString.init(input).toValueGC(globalThis);
+                return result;
+            },
             else => {
                globalThis.throwInvalidArguments("Unexpected encoding", .{});
                 return JSC.JSValue.zero;
             },
         }
@@ -571,8 +575,12 @@ pub const Encoding = enum(u8) {
                 const result = JSC.ZigString.init(out).toValueGC(globalThis);
                 return result;
             },
+            .latin1 => {
+                const result = JSC.ZigString.init(input).toValueGC(globalThis);
+                return result;
+            },
             else => {
                globalThis.throwInvalidArguments("Unexpected encoding", .{});
                 return JSC.JSValue.zero;
             },
         }

@Electroid Electroid added the node.js Compatibility with Node.js APIs label Jul 5, 2023
Jarred-Sumner added a commit that referenced this issue Jul 10, 2023
Jarred-Sumner added a commit that referenced this issue Jul 10, 2023
* Fixes #3530

* Handle OOM

* Add test

---------

Co-authored-by: Jarred Sumner <[email protected]>
Jarred-Sumner added a commit that referenced this issue Jul 13, 2023
* Fixes #3530

* Handle OOM

* Add test

---------

Co-authored-by: Jarred Sumner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants