Why is crypto.subtle undefined over HTTP, even for a SHA-256 hash?

April 29, 2025

I opened AssetMan, my asset management app, at the server’s IP address over HTTP. The page loaded, but when I tried to log in I got Login error: TypeError: crypto.subtle is undefined. The form was supposed to hash my password and send the result to the server. It stopped at crypto.subtle.digest(). The server hadn’t even received a login request.1

A Stack Overflow question described a clue that fit my error: the same browser exposed crypto.subtle on some pages and returned undefined on others. MDN explained what those pages could differ in. The browser checks how the page was delivered before it exposes this part of the Web Crypto API.23

Why does the browser expose the API on localhost but hide it on an HTTP server?

For a top-level page, HTTPS authenticates the server and protects the page in transit. A page delivered that way qualifies as a secure context. Browsers also generally trust http://localhost and http://127.0.0.1, because those addresses lead back to the same machine. Opening the app over HTTP at another machine’s IP address takes it outside that exception, even on an office LAN. To check which case applies, you can run these two expressions in the console of the page that failed.4

window.isSecureContext       // false on an ordinary remote HTTP page
typeof window.crypto?.subtle // "undefined" on that page

That explained the error. What still puzzled me was why the rule covered a hash. SHA-256 takes bytes as input and produces a fixed-length value called a digest. It produces the same digest for the same bytes regardless of how the page arrived.

Browser implementers wanted a common rule for access to Web Crypto. In the standards discussion, they considered checking the context when a method was called. The 2017 Recommendation put [SecureContext] on the subtle property itself. By the time my code asked for digest(), the browser had already decided whether the page could access the whole interface, including signing and encryption. The working group kept that design when developers asked for exceptions.567

Did my login form need a secure context to calculate the password hash?

When AssetMan created its default administrator, the backend added a fixed prefix to the password, calculated its SHA-256 digest, and stored the result. At login, the browser performed the same calculation and sent the digest in the request’s password field. The server compared the incoming value directly with the stored one.1

I wanted to keep the original password out of the request body. Either a browser-native hash or a JavaScript implementation could do that. Someone who passively recorded my HTTP login request would see the digest. That helped with my narrow goal, but the digest itself was enough to log in. The server accepted the same value each time.1

What if someone on the network could also change the login page on its way to my browser? This was the part of the password-theft example that puzzled me. The altered form could read my password as I typed it, before it called any hashing code. It could do that even with crypto.subtle disabled. HTTPS addresses that risk by protecting the form and its script as they travel from the server to my browser.8

On May 9, I looked at crypto-js and opened the SHA-256 implementation in sha.js. That library computes the digest entirely in JavaScript, so it can hash the same bytes even when the page has no access to crypto.subtle.9

On June 28, I switched AssetMan’s login form, profile editor, and user administration page to crypto-js for the local-network deployment. They could then produce the expected digest on an HTTP page.10

What if the hash only names a cache entry?

Brad Isbell described another use in the standards discussion: “In my case, I'm hashing to calculate cache keys for an optimization.”11 Imagine a thumbnail generator that uses the digest of an image as the name of its cached thumbnail. Given the same image again, it can find the thumbnail and skip a resize. A JavaScript implementation that produces the same digest will find the same entry.

That example goes a step further than my login form. My digest became a credential as soon as the server agreed to accept it. The thumbnail generator uses its digest only to find work it has already done. For that lookup, it needs a consistent answer to “Have I seen these bytes before?” It can answer that question on an HTTP page.

When does the security of the page matter to the result?

Take the same SHA-256 call and use it to verify a download from a mirror. A publisher supplies a checking page with the expected digest of a release. The browser hashes the downloaded file, compares the two values, and tells the user whether the file matches. Unlike the thumbnail generator, this page already needs one trustworthy piece of information: what digest did the publisher say to expect? If someone can alter the checking page in transit, they can change the expected value or simply make the page display “verified.” Serving the checker over HTTPS gives the browser an authenticated copy of the publisher’s code and its reference value.7

Signing a document makes the same concern visible from the user’s side. I read a document and click Sign. The browser may hold a private key that the page cannot export, yet the page still chooses which bytes to send to the signing operation. A page altered in transit could show me one document and ask the browser to sign another. Requiring a secure context for native signing makes trustworthy delivery of that page a prerequisite for using the key through this API. I still have to trust the site to show me what I’m signing.126

That is the distinction I was looking for when AssetMan failed. I could move the SHA-256 calculation into a library and get exactly the value my server expected. For a download checker or a signing page, getting the arithmetic right is only part of the promise to the user. The promise also depends on who supplied the expected digest or chose the document to sign.

Footnotes

  1. Cai, B. (2025, April 24). Add login feature (AssetMan commit b94e7c7). See the login form and the server’s comparison. GitHub. ↩ ↩2 ↩3

  2. guest. (2017, September 28). How to use SubtleCrypto in chrome (window.crypto.subtle is undefined). Stack Overflow. See the question and Zmart’s answer. ↩

  3. MDN contributors. (n.d.). Crypto: subtle property. MDN Web Docs. ↩

  4. West, M. (Ed.). (2023, November 10). Secure Contexts, §3.1, “Is origin potentially trustworthy?”. W3C. ↩

  5. mwatson2. (2016, May 23). Please require a secure origin (Bug 25972), issue #28. W3C Web Crypto Working Group, GitHub. See the opening proposal and the discussion of a method guard versus [SecureContext]. ↩

  6. Watson, M. (Ed.). (2017, January 26). Web Cryptography API, §10, “Crypto interface”. W3C. ↩ ↩2

  7. andrewmd5. (2017, January 23). Do not mandate secure context for SubtleCrypto, issue #170. W3C Web Crypto Working Group, GitHub. See plehegar’s May 16, 2018 closure and twiss’s February 11, 2022 file-integrity example. ↩ ↩2

  8. Watson, M. (Ed.). (2017, January 26). Web Cryptography API, §6.2, “Security considerations for authors”. W3C. See paragraphs 3–4 on hostile scripts and TLS. ↩

  9. Browserify. (n.d.). sha.js. GitHub. The README shows how to produce a hexadecimal SHA-256 digest. ↩

  10. Cai, B. (2025, June 28). Replace crypto subtle with crypto js for easier deployment in local network (AssetMan commit ac510f6, branch sqlite-deploy-1). GitHub. The frontend imports SHA256 from the crypto-js package. ↩

  11. bradisbell. (2022, February 11). Comment on “Do not mandate secure context for SubtleCrypto,” issue #170. GitHub. Paragraph beginning “In my case.” The thumbnail example illustrates the use case. ↩

  12. Watson, M. (Ed.). (2017, January 26). Web Cryptography API, §2.4, “Document Signing”. W3C. See also §6.2 on hostile scripts and key usage. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-04-29). Why is crypto.subtle undefined over HTTP, even for a SHA-256 hash?. https://caiborui.com/blog/2025-04-29-en-crypto-subtle-undefined-http

@misc{borui2025-c4c96d70,
  author = {borui},
  title = {{Why is crypto.subtle undefined over HTTP, even for a SHA-256 hash?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-04-29-en-crypto-subtle-undefined-http}
}