Skip to content

Incorrect result from Buffer.from(...).buffer when using base64url encoding #55564

@Foair

Description

@Foair

Version

v20.18.0

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

When attempting to create an ArrayBuffer using Buffer.from(..., 'base64url').buffer, the output is incorrect. Instead of the expected ArrayBuffer, the function produces an unexpectedly larger ArrayBuffer.

node -p "Buffer.from('G4Zmayf9a4KOkKHBFeHHQREzRMW_6MPR', 'base64url').buffer"

Result:

ArrayBuffer {
  [Uint8Contents]: <5c 00 00 00 00 00 00 00 1b 86 66 6b 27 fd 6b 82 8e 90 a1 c1 15 e1 c7 41 11 33 44 c5 bf e8 c3 d1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 8092 more bytes>,
  byteLength: 8192
}

FYI: The string G4Zmayf9a4KOkKHBFeHHQREzRMW_6MPR is produced by Buffer#toString('base64url') containing some random bytes.

How often does it reproduce? Is there a required condition?

Stable

What is the expected behavior? Why is that the expected behavior?

Should get a correctly-sized ArrayBuffer representing the base64url decoded string, like Deno does:

deno eval "import { Buffer } from 'node:buffer';
  console.log(Buffer.from('G4Zmayf9a4KOkKHBFeHHQREzRMW_6MPR', 'base64url').buffer)"
ArrayBuffer {
  [Uint8Contents]: <1b 86 66 6b 27 fd 6b 82 8e 90 a1 c1 15 e1 c7 41 11 33 44 c5 bf e8 c3 d1>,
  byteLength: 24
}

What do you see instead?

ArrayBuffer {
  [Uint8Contents]: <5c 00 00 00 00 00 00 00 1b 86 66 6b 27 fd 6b 82 8e 90 a1 c1 15 e1 c7 41 11 33 44 c5 bf e8 c3 d1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 8092 more bytes>,
  byteLength: 8192
}

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions