Skip to content

fix(utils): encode and decode base64 without Buffer - #282

Merged
allanbowe merged 1 commit into
mainfrom
fix/base64-without-buffer
Oct 15, 2026
Merged

allanbowe merged 1 commit into
mainfrom
fix/base64-without-buffer

Conversation

@sasjs-dev

@sasjs-dev sasjs-dev Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Closes #276.

encodeToBase64 and decodeFromBase64 used Buffer, which does not exist in a browser. utils/index.ts re-exports both from the package root, so every consumer importing @sasjs/utils carried the reference into its browser bundle. While those bundles shipped a Node polyfill tree the reference was silently shimmed; now that consumers are dropping that tree - the adapter's browser bundle no longer carries one - it is a ReferenceError waiting for the first caller.

Changes

  • TextEncoder / TextDecoder carry the UTF-8 step. btoa is latin1, so encoding a password through it directly corrupts anything non-ASCII, and these values are stored.
  • The binary string is built in chunks rather than spreading a whole byte array into String.fromCharCode, which overflows the call stack on long input.
  • Whitespace is stripped before atob: Buffer ignores it when decoding and atob does not, so a wrapped or padded stored value still decodes.

The stored format is unchanged - the {sasjs_encoded} prefix, 200 hex characters of padding, and the fixed 200-character offset decodeFromBase64 strips - so credentials written by earlier releases keep decoding.

Tests

The failure mode is invisible in Node, so:

  • a non-ASCII password round-trips, which is what a naive swap to btoa would break
  • a round trip with the Buffer global deleted, which is the browser case
  • the module code contains no Buffer reference, comments aside

Proven by reintroducing the Buffer implementation:

with Buffer:   ✕ should round-trip without a Buffer global
               ✕ should reference no Buffer in the module code
               ReferenceError: Buffer is not defined
               Tests: 2 failed, 6 passed, 8 total
with platform: Tests: 8 passed, 8 total

Full suite: 40 suites, 448 tests passing. tsc and prettier clean.

Why now

This is the last unguarded host global in a browser-facing module of this package - the same class as the process.platform reads fixed in #281. With both gone, a browser consumer of @sasjs/utils needs no polyfill for anything this package does itself.

Closes #276.

`encodeToBase64` and `decodeFromBase64` used `Buffer`, which does not exist
in a browser. `utils/index.ts` re-exports both from the package root, so
every consumer that imports `@sasjs/utils` carried the reference into its
browser bundle. While those bundles shipped a Node polyfill tree the
reference was silently shimmed; now that consumers are dropping that tree -
the adapter's browser bundle no longer carries one - it is a ReferenceError
waiting for the first caller.

Both functions now use the platform globals:

- `TextEncoder` / `TextDecoder` carry the UTF-8 step. `btoa` is latin1, so
  encoding a password through it directly corrupts anything non-ASCII, and
  these values are stored.
- The binary string is built in chunks rather than spreading a whole byte
  array into `String.fromCharCode`, which overflows the call stack on long
  input.
- Whitespace is stripped before `atob`, because Buffer ignores it when
  decoding and `atob` does not - a wrapped or padded stored value still
  decodes.

The stored format is unchanged: the `{sasjs_encoded}` prefix, 200 hex
characters of obfuscation padding, and the fixed 200-character offset that
`decodeFromBase64` strips. Credentials written by earlier releases keep
decoding.

Tests, because the failure mode is invisible in Node:

- a non-ASCII password round-trips, which is what a naive swap to `btoa`
  would break
- a round trip with the Buffer global deleted, which is the browser case
- the module code contains no `Buffer` reference, comments aside

Proven by reintroducing the Buffer implementation: two of them fail with
`ReferenceError: Buffer is not defined`, and pass with the platform APIs.

Full suite: 40 suites, 448 tests passing. tsc and prettier clean.

@sasjs-dev sasjs-dev Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reviewed the full diff at e8e93df (base64 encode/decode moved off Buffer to TextEncoder/btoa/atob). The UTF-8 step is preserved on both sides, so the output stays byte-identical to the old Buffer path and previously stored values still decode; the chunked String.fromCharCode avoids the call-stack overflow on long input.

No blocking issues.

@allanbowe
allanbowe merged commit 79ef9c8 into main Oct 15, 2026
2 checks passed
@allanbowe
allanbowe deleted the fix/base64-without-buffer branch October 15, 2026 21:08
sasjs-gha Bot pushed a commit that referenced this pull request Oct 15, 2026
## [3.7.4](v3.7.3...v3.7.4) (2026-10-15)

### Bug Fixes

* **utils:** encode and decode base64 without Buffer ([#282](#282)) ([79ef9c8](79ef9c8))
@sasjs-gha

sasjs-gha Bot commented Oct 15, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 3.7.4 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@sasjs-gha sasjs-gha Bot added the released label Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

encodeToBase64 / decodeFromBase64 depend on Buffer, which forces a polyfill in browser bundles

1 participant