Skip to content

fix(http-helmet): encode nonces across JavaScript runtimes - #157

Merged
mcansh merged 1 commit into
devfrom
logan/http-helmet-nonce
Oct 7, 2026
Merged

mcansh merged 1 commit into
devfrom
logan/http-helmet-nonce

Conversation

@mcansh

@mcansh mcansh commented Oct 7, 2026 •

Copy link
Copy Markdown
Owner

Summary

createNonce() currently depends on the Node.js Buffer global. Use native Base64 encoding when available and a portable fallback otherwise, preserving the Base64-encoded UUID format.

createNonce()
  randomUUID()
  toBase64 available?
    yes → TextEncoder → toBase64
    no  → btoa(uuid)

Changes include tests for both paths, a check that the fallback skips UTF-8 byte allocation, and a patch changeset for @mcansh/http-helmet.

Related stack: #150 → #151 → #152 → #153. This PR targets dev directly, so it is independent of that native stack.

Impact: Only nonce generation changes; consumers retain the existing string format and need no configuration changes.

Evidence

  • Before: nonce generation requires Buffer.from(uuid).toString("base64").
  • After: all 28 http-helmet tests pass on Node.js 24.15.0, including simulated native-method availability and absence. Typecheck, Prettier formatting, and diff checks pass using the dev branch dependencies (Vitest 3.2.4).
  • Gaps: Native-method coverage uses a test shim. Other JavaScript runtimes have not been executed for this change; CI is pending.

Merge Danger

Door: two-way. Revert this commit to restore the previous implementation; no data migration is involved.

Blast Radius: nonces. Runtime differences in native Base64 encoding could affect CSP nonce strings; tests verify equivalent output and fallback behavior.

Summary by CodeRabbit

  • Improvements
    • Nonce generation now uses native Base64 encoding when available, with a browser-compatible fallback.

Copilot AI balanced review requested due to automatic review settings October 7, 2026 14:31
@changeset-bot

changeset-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cce6c4d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@mcansh/http-helmet Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T14:34:12.312979Z cce6c4d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 40af6fc3-15f6-496b-9379-66a1db1d39df
📥 Commits

Reviewing files that changed from the base of the PR and between edfff9c and cce6c4d.

📒 Files selected for processing (3)
  • .changeset/portable-http-helmet-nonce.md
  • packages/http-helmet/src/nonce.spec.ts
  • packages/http-helmet/src/utils.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


Walkthrough

createNonce now encodes generated UUIDs with Uint8Array.prototype.toBase64 when available and uses btoa otherwise. Tests cover nonce uniqueness, UUID format, and both encoding paths. A patch changeset records the change.

Changes

Nonce encoding

Layer / File(s) Summary
Nonce generation and encoding
packages/http-helmet/src/utils.ts, packages/http-helmet/src/nonce.spec.ts, .changeset/portable-http-helmet-nonce.md
createNonce uses Uint8Array.prototype.toBase64 when available and falls back to btoa. Tests check nonce uniqueness, UUID format, and each encoding path. A patch changeset records the update.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to cce6c

The nonce encoding change appears mergeable after normal checks. Other JavaScript runtimes remain untested, but no specific failure has been identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: updating nonce encoding to work across JavaScript runtimes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 7, 2026

Copy link
Copy Markdown
More templates

npm i https://pkg.pr.new/mcansh/packages/@mcansh/create-temporary-files@157
npm i https://pkg.pr.new/mcansh/packages/@mcansh/http-helmet@157
npm i https://pkg.pr.new/mcansh/packages/@mcansh/url@157
npm i https://pkg.pr.new/mcansh/packages/@mcansh/vitest-response-matchers@157

commit: cce6c4d

@codecov

codecov Bot commented Oct 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.23%. Comparing base (edfff9c) to head (cce6c4d).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #157      +/-   ##
==========================================
+ Coverage   93.97%   94.23%   +0.26%     
==========================================
  Files          19       19              
  Lines         896      902       +6     
  Branches      221      223       +2     
==========================================
+ Hits          842      850       +8     
+ Misses         54       52       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation preserves existing output, handles both runtime paths, and includes focused tests and release metadata.

Review effort: Balanced
Findings: None

What changed in this PR

Makes createNonce() portable across JavaScript runtimes while preserving its Base64-encoded UUID format.

Changes:

  • Uses native Uint8Array.toBase64() with a btoa() fallback.
  • Tests native and fallback paths.
  • Adds a patch Changeset.
File Description
packages/​http-helmet/​src/​utils.ts Implements portable nonce encoding.
packages/​http-helmet/​src/​nonce.spec.ts Covers encoding paths and allocation behavior.
.changeset/​portable-http-helmet-nonce.md Records the patch release.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
packages cce6c4d Oct 07 2026, 02:32 PM

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

  • createNonce Buffer removal (packages/http-helmet/src/utils.ts) — Buffer.from(uuid).toString("base64") replaced with Uint8Array.prototype.toBase64 when present and a btoa(uuid) fallback otherwise; toBase64 is read once, guarded by a typeof check, and invoked with a fresh TextEncoder byte buffer.
  • Nonce tests (packages/http-helmet/src/nonce.spec.ts) — a distinctness + base64-decodable-UUID-format test, and a parameterized test that stubs Uint8Array.prototype.toBase64 presence/absence, spies TextEncoder.prototype.encode and crypto.randomUUID, and asserts both branches return btoa(uuid), the native branch calls encode exactly once, and the fallback skips UTF-8 allocation.
  • Changeset — patch for @mcansh/http-helmet.

I verified the equivalence claim directly: for an ASCII UUID, new TextEncoder().encode(uuid).toBase64() and btoa(uuid) both equal Buffer.from(uuid).toString("base64"), so the output format is unchanged on every path. The parameterized tests genuinely pin the branching (always-native or always-fallback implementations would fail the assertions), and the finally block restores the stubbed prototype cleanly. Full package suite (28 tests), tsc --noEmit, and Prettier all pass locally.

Pullfrog  | View workflow run | Using big-pickle (free) | 𝕏

@mcansh
mcansh merged commit 3d177ab into dev Oct 7, 2026
18 checks passed
@mcansh
mcansh deleted the logan/http-helmet-nonce branch October 7, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants