How to password protect a static HTML page with no JS
I recently saw Robin Moisson's method of password protecting a statically served HTML page. It's quite neat! But it does rely on JavaScript. That got me wondering if there was a way to encrypt a static page only using CSS?
And... I think I've done it!
I'll warn you now, this is a deeply stupid way to solve the problem.
Here's a screencast of the demo in action:
Type the password and the page decrypts!!!!!
This abuses some interesting CSS features.
First, you can display the <style>
element on the page.
HTML
<style>
style {
display: block;
}
</style>
Secondly, you can make the CSS editable by the user with contenteditable
HTML
<style contenteditable="true">...</style>
As soon as a user types into the CSS, it is applied to the page. No need for JS.
So if a user types in a password...?
Let's step back a bit. How can we encrypt text using CSS? I know... WEBFONTS!
A WOFF2 webfont is a Brotli compressed file. If I've read the spec correctly (LOL!) removing a chunk of a small file should render the file too damaged to read.
It's possible to convert the WOFF2 into Base64 and use it in the CSS:
CSS
@font-face {
font-family:'encrypt_sans';
src:url('data:application/font-woff2;charset=utf-8;base64,d09GMg...') format('woff2');
}
Cut a chunk out of the middle of that font, and use that missing piece as the password. Foolproof!
But - I hear you say - how to encrypt text using a font? Well, that's easy!
That cleverclogs Eli Grey has a font which encrypts text. It's magical. Well, OK, it's ROT13. Obviously, any substitution cipher can probably be broken using frequency analysis.
Of course, it is possible to use font ligatures to obfuscate the text even more. See Bullshit Sans as an example
So, there you have it. A way to sort of encrypt a statically served HTML file without using JavaScript.
Demo and Source
Possible Improvements
This was a demo hastily put together while hungover one weekend. There's lots of room for improvement.
The UI abuses CSS to hide some of the boilerplate involved. It could be made to look nicer.
There's no way to generate an "encrypted" font. Ideally someone (not me!) would take a plaintext and generate a scrambled and ligatured font to automagically do this.
It is inaccessible to screen readers. The font doesn't change the underlying text.
Brotli compressed WOFF2 files might be recoverable even after substantial damage.
This is really really stupid.
Elegant codes says:
Kra says:
Kra says:
Code élégant says:
Kra says:
More comments on Mastodon.