Socket
Socket
Sign inDemoInstall

pagecrypt

Package Overview
Dependencies
3
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pagecrypt

Easily add client-side password-protection to your Single Page Applications and HTML files.


Version published
Maintainers
1
Install size
127 kB
Created

Changelog

Source

5.1.0 - 2021-07-30

Another major UX improvement incoming: Magic links to unlock encrypted pages with a single click!

Also slightly improved browser support for the pagecrypt/core module.

Features

  • feature(ux): Implement + add docs for magic links that allow single-click unlocks
  • feature(core): Improve crypto loading to support older browsers for the core package.
  • feature(decrypt): Convert decryption script to TypeScript

Fixes

  • docs(general): Highlight required Node.js version
  • fix(dev server): Remove sirv-cli dev dependency since we no longer need HTTPS for local dev.
  • docs(core): Clarify docstrings, improve terminology used and add link to related blog post.
  • docs(dev): Update testing and dev server instructions.
  • chore(test): Remove hardcoded test package version
  • chore(test): Improve browser encryption test

Readme

Source

🔐 PageCrypt - Password Protected Single Page Applications and HTML files

Easily add client-side password-protection to your Single Page Applications and HTML files.

Inspired by MaxLaumeister/PageCrypt, but rewritten to use native Web Crypto API and greatly improve UX + security. Thanks for sharing an excellent starting point to create this tool!

Get started

NOTE: Make sure you are using Node.js v15 or newer.

There are 4 different ways to use pagecrypt:

1. Encrypt HTML in modern browsers, Deno or Node.js using pagecrypt/core

The encryptHTML() and generatePassword() functions are using Web Crypto API and will thus be able to run in any ESM compatible environment that supports Web Crypto API.

This allows you to use the same pagecrypt API in any environment where you can run modern JavaScript.

encryptHTML(inputHTML: string, password: string): Promise<string>
import { encryptHTML } from 'pagecrypt/core'

const inputHTML = `
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
            Secret
        </body>
    </html>
`

// Encrypt a HTML string and return an encrypted HTML string.
// Write it to a file or send as an HTTPS response.
const encryptedHTML = await encryptHTML(inputHTML, 'password')
generatePassword(length: number): string
import { generatePassword, encryptHTML } from 'pagecrypt/core'

// Generate a random password without any external dependencies
const password = generatePassword(64)
const encryptedHTML = await encryptHTML(inputHTML, password)

2. Node.js API

When working in a Node.js environment, you may prefer the pagecrypt Node.js build. This also includes the encrypt() function to read and write directly from and to the file system.

encrypt(inputFile: string, outputFile: string, password: string): Promise<void>
import { encrypt } from 'pagecrypt'

// Encrypt a HTML file and write to the filesystem
await encrypt('index.html', 'encrypted.html', 'password')

NOTE: Importing pagecrypt also gives you access to generatePassword() and encryptHTML() from pagecrypt/core.

import { generatePassword, encryptHTML } from 'pagecrypt'

const password = generatePassword(48)

const encrypted = await encryptHTML(inputHTML, password)

3. CLI

Encrypt a single HTML-file with one command:

npx pagecrypt <src> <dest> [password] [options]

Encrypt using a generated password with given length:

npx pagecrypt <src> <dest> -g <length>
3.1. CLI Help
  Description
    Encrypt the <src> HTML file with [password] and save the result in the <dest> HTML file.

  Usage
    $ pagecrypt <src> <dest> [password] [options]

  Options
    -g, --generate-password    Generate a random password with given length. Must be a number if used.
    -v, --version              Displays current version
    -h, --help                 Displays this message

  Examples
    $ pagecrypt index.html encrypted.html password
    $ pagecrypt index.html encrypted.html --generate-password 64
    $ pagecrypt index.html encrypted.html -g 64

4. Automate pagecrypt in your build process

Use either the pagecrypt Node.js API or the CLI to automatically encrypt the builds for your single page applications.

npm i -D pagecrypt

package.json:

{
    "devDependencies": {
        "pagecrypt": "^5.0.0"
    },
    "scripts": {
        "build": "...",
        "postbuild": "pagecrypt index.html encrypted.html password"
    }
}

Deploying a SPA or Website Encrypted with pagecrypt

Since the output is a single HTML file, you can host it anywhere. This lets you bypass the need for server access to use HTTP basic authentication for password protection.

What this means in practice is that pagecrypt enables you to deploy private apps and websites to any static frontend hosting platform, often for free. Great for prototypes and client projects.

To make it easier for your users to access protected pages, you can create a magic link by adding # followed by your password to your deployment URL:

https://<link-to-your-page>#<password>

Then users can simply click the link to load the protected SPA or website - a really smooth UX! Just make sure to keep the link safe by sharing it via E2E-encrypted chats and emails.

  1. Deploy your encrypted HTML file to any web server and copy the URL from your browser.
  2. Create the link by starting with your URL, then writing an #, followed by your password. E.g. https://example.com#password
  3. Make sure the link starts with the https:// protocol to keep users safe.

Since this magic link feature is using the URI Fragment, it will not be sent across the internet once the user clicks the link. Only the first part before the # leaves the user's computer to fetch the HTML page, and the rest remains in the browser, used for local decryption. Additionally, the fragment is removed from the browser address field when the page loads. However, beware that the password remains as a history entry if you use magic links!

Security Considerations

  • Most importantly, think twice about what kinds of sites and apps you publish to the open internet, even if they are encrypted.
  • If you use the magic link to login, beware that the password remains as a history entry! Feel free to submit a PR if you know a workaround for this!
  • Also keep in mind that the sessionStorage saves the encryption key (which is derived from the password) until the browser is restarted. This is what allows the rapid page reloads during the same session - at the cost of decreasing the security on your local device.
  • Only share magic links via secure channels, such as E2E-encrypted chats and emails.
  • pagecrypt only encrypts the contents of a single HTML file, so try to inline as much JS, CSS and other sensitive assets into this HTML file as possible. If you're unable to inline all sensitive assets, you can hide your other assets by placing them on another server, and then only reference the external resources within the pagecrypt protected HTML file instead. Of course, these could in turn be protected or hidden if you need to. If executed correctly, this allows you to completely hide what your webpage or app is about by only deploying a single HTML file to the public web. Neat!

Development

Project structure:

  • /web - Web frontend for public webpage (decrypt-template.html). Built using Vite & Tailwind CSS.
  • /src/core.ts - pagecrypt core library.
  • /src/index.ts - pagecrypt Node.js library.
  • /src/cli.ts - pagecrypt CLI.
  • /test - simple testing setup.
  • /scripts - local scripts for development tasks.

Setup a local development environment

  1. Install Node.js >= 15.0.0
  2. Run npm install in project root.

Testing

npm test will run basic tests for JS API and CLI. See the passwords used in your terminal. Verify test results by opening the test/out-*.html files in your browser and entering the password matching the file.

To test how pagecrypt/core works with encryption in browser environments, run cd test && npm run test:browser-core. Then download the encrypted sample file and use the "actual" password that's printed to the screen to verify it works. The generated password just shows that the password generation works.


Welcome to submit issues and pull requests!

Keywords

FAQs

Last updated on 29 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc