fix(security): replace crypto.createHash with Web Crypto API and remove API key leak#650
Open
joelrb wants to merge 1 commit into
Open
fix(security): replace crypto.createHash with Web Crypto API and remove API key leak#650joelrb wants to merge 1 commit into
joelrb wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Problem
Three issues existed in the worker functions:
crypto.createHash('md5')incompatible with Workers —functions/lib/jina.ts:114andfunctions/lib/cf-browser.ts:164used Node.js'scrypto.createHash('md5')API. This is a Node.js-specific API that requires thenodejs_compatcompatibility flag to work in Cloudflare Workers. The project does not have this flag enabled in eitherwrangler.jsoncorwrangler.toml. At runtime, this code throwsReferenceError: crypto.createHash is not a function, causing all crawl save operations to crash silently.MD5 is a weak algorithm — Even if the
nodejs_compatflag were enabled, MD5 is considered cryptographically broken. Cloudflare's own documentation states: "MD5 is considered a weak algorithm. Do not rely upon MD5 for security."Jina API key exposed in logs —
functions/lib/jina.ts:19logged the full Jina API key to the Cloudflare Workers console on every request:console.log('Fetching from Jina API for URL:', url, apiKey). This key is visible to anyone with access to the Workers log stream.Node.js crypto API vs Web Crypto API — The Workers runtime uses the Web Crypto API (
crypto.subtle.digest) natively. The Node.jscrypto.createHash()requires thenodejs_compatflag, which adds overhead and is not enabled in this project.Fix Applied
hashContent()helper function in both files that usescrypto.subtle.digest('SHA-256', data)— the Web Crypto API native to Cloudflare Workerscrypto.createHash('md5').update(data.content).digest('hex')withawait hashContent(data.content)in bothsaveJinaContent()andsaveCFBrowserContent()apiKeyfrom theconsole.logstatement infetchJinaContent()— only the URL is now loggedFramework Change
No framework changes. This is a runtime fix for the Cloudflare Workers execution environment.
Files Changed
functions/lib/jina.ts— AddedhashContent()helper, replacedcrypto.createHashwithhashContent(), removedapiKeyfrom console.logfunctions/lib/cf-browser.ts— AddedhashContent()helper, replacedcrypto.createHashwithhashContent()Security Severity: Low
Type of Change
Related Issues
Fixes #crypto-createhash-not-available-in-workers
Fixes #api-key-exposed-in-logs
Testing
Describe the testing performed for this PR:
Include specific test scenarios or commands used.
Manual test:
crypto.subtle.digest('SHA-256', ...)is available in Cloudflare Workers runtime (per official docs)crypto.createHashis a Node.js-only API requiringnodejs_compatflag (not enabled)crypto.createHashcalls exist infunctions/lib/hashContent()helper correctly converts ArrayBuffer to hex stringChecklist
Performance & Security
Screenshots (if applicable)
N/A
Additional Notes
Why this fix is necessary:
crypto.createHash()is Node.js-only — Cloudflare Workers use the Web Crypto API. Without thenodejs_compatcompatibility flag (not enabled in this project),crypto.createHashthrows aReferenceErrorat runtime.Every crawl save operation was broken — Both
saveJinaContent()andsaveCFBrowserContent()would crash when trying to compute the content hash, meaning all crawled content persistence silently failed.MD5 is deprecated even in Workers — Cloudflare's docs state: "MD5 is considered a weak algorithm. Do not rely upon MD5 for security." The fix upgrades to SHA-256.
API key in logs is a security vulnerability — Anyone with access to the Workers log stream (Cloudflare dashboard, wrangler tail) could see the Jina API key in plaintext on every request.