Skip to content

Allow caching of password #116

Closed

Description

@edcous

I have a Hugo website, using Netlify, and would like to know how to not require all pages on the site to ask for the password if it has already been entered on another page. Any help would be greatly appreciated. The following is my Netlify build command
hugo && npm install && find public -type f -name '*.html' -exec staticrypt {} ****** \\;
Thanks,
Eli

Activity

  1. Embraser01 commented on Jul 4, 2019

    @Embraser01

    Hey, I needed the same thing!

    I rewrited a part of the password_template.html file to save the pass in the sessionstorage once the user entered the good password.

    You can look here: https://github.com/Embraser01/staticrypt/blob/gh-pages/password_template.html

    Then for netlify it's really easy!
    My netlify.toml:

    [build]
    publish = "public"
    command = "hugo --gc --minify && ./crypt.sh"
    
    [context.production.environment]
    HUGO_VERSION = "0.54.0"
    HUGO_ENV = "production"
    
    
    [context.deploy-preview]
    command = "hugo  --gc --minify --buildFuture -b $DEPLOY_PRIME_URL && ./crypt.sh"
    
    [context.deploy-preview.environment]
    HUGO_VERSION = "0.54.0"
    
    
    [context.branch-deploy]
    command = "hugo  --gc --minify -b $DEPLOY_PRIME_URL && ./crypt.sh"
    
    [context.branch-deploy.environment]
    HUGO_VERSION = "0.54.0"
    

    With the following ./crypt.sh:

    #!/usr/bin/env bash
    set -e
    
    npm install -g https://github.com/Embraser01/staticrypt
    
    find ./public -type f -name "*.html" -exec staticrypt {} $SITE_PASSWORD -o {} \;
    

    It will replace all your html files by the encrypted ones. There is still a flash on each pages because I only decrypt after onload but I'll try to improve that quickly.

    Also, it's not ideal to save the password as is in the sessionstorage... It would be good to secure it (no idea for now)

  2. Embraser01 commented on Jul 5, 2019

    @Embraser01

    It seems to be an error related to your hugo site (otherwise, it would have printed the results of hugo). Does it compile on your local machine?

  3. edcous commented on Jul 12, 2019

    @edcous
    Author

    Re:

    Hey, I needed the same thing!

    I rewrited a part of the password_template.html file to save the pass in the sessionstorage once the user entered the good password.

    You can look here: https://github.com/Embraser01/staticrypt/blob/gh-pages/password_template.html

    Then for netlify it's really easy!
    My netlify.toml:

    [build]
    publish = "public"
    command = "hugo --gc --minify && ./crypt.sh"
    
    [context.production.environment]
    HUGO_VERSION = "0.54.0"
    HUGO_ENV = "production"
    
    
    [context.deploy-preview]
    command = "hugo  --gc --minify --buildFuture -b $DEPLOY_PRIME_URL && ./crypt.sh"
    
    [context.deploy-preview.environment]
    HUGO_VERSION = "0.54.0"
    
    
    [context.branch-deploy]
    command = "hugo  --gc --minify -b $DEPLOY_PRIME_URL && ./crypt.sh"
    
    [context.branch-deploy.environment]
    HUGO_VERSION = "0.54.0"
    

    With the following ./crypt.sh:

    #!/usr/bin/env bash
    set -e
    
    npm install -g https://github.com/Embraser01/staticrypt
    
    find ./public -type f -name "*.html" -exec staticrypt {} $SITE_PASSWORD -o {} \;
    

    It will replace all your html files by the encrypted ones. There is still a flash on each pages because I only decrypt after onload but I'll try to improve that quickly.

    Also, it's not ideal to save the password as is in the sessionstorage... It would be good to secure it (no idea for now)

    I'm not 100 percent sure on how to set it up, but this is what I did:
    added a crypt.sh with the following code:

    #!/usr/bin/env bash
    set -e
    
    npm install -g https://github.com/Embraser01/staticrypt
    
    find ./public -type f -name "*.html" -exec staticrypt {} **** -o {} \\;
    

    And, this is my netlify.toml:

    [build]
      publish = "public"
      command = "hugo && npm install && ./crypt.sh"
    [build.environment]
      HUGO_VERSION = "0.55.6"
      HUGO_THEME = "hugo-hero-theme"
      HUGO_BASEURL = "/"
    
  4. kevincolten commented on Sep 5, 2019

    @kevincolten

    I got it working using this command
    set -e && find ./_site -type f -name '*.html' -exec staticrypt -f password_template.html {} $PASSWORD -o {} \\;

    and by tweaking the password template script, replacing

    document.getElementById('staticrypt-form').addEventListener('submit', function(e) {
        e.preventDefault();
        var passphrase = document.getElementById('staticrypt-password').value,
            encryptedMsg = '{encrypted}',
            encryptedHMAC = encryptedMsg.substring(0, 64),
            encryptedHTML = encryptedMsg.substring(64),
            decryptedHMAC = CryptoJS.HmacSHA256(encryptedHTML, CryptoJS.SHA256(passphrase).toString()).toString();
        if (decryptedHMAC !== encryptedHMAC) {
            alert('Bad passphrase!');
            return;
        }
        var plainHTML = decrypt(encryptedHTML, passphrase);
        document.write(plainHTML);
        document.close();
    });

    with

    function insertPlainHTML(password) {
        encryptedMsg = '{encrypted}',
        encryptedHMAC = encryptedMsg.substring(0, 64),
        encryptedHTML = encryptedMsg.substring(64),
        decryptedHMAC = CryptoJS.HmacSHA256(encryptedHTML, CryptoJS.SHA256(password).toString()).toString();
    
        if (decryptedHMAC !== encryptedHMAC) {
            // alert('Bad password!');
            localStorage.removeItem('password');
            return;
        }
        localStorage.setItem('password', btoa(password));
        document.write(decrypt(encryptedHTML, password));
        document.close();
    }
    
    window.addEventListener('DOMContentLoaded', function() {
        if (localStorage.getItem('password')) {
            insertPlainHTML(atob(localStorage.getItem('password')))
        } else {
            document.getElementById('staticrypt-form').addEventListener('submit', function(e) {
                e.preventDefault();
                insertPlainHTML(document.getElementById('staticrypt-password').value);
            });
        }
    });
  5. bsde1234 commented on Sep 14, 2019

    @bsde1234

    Stupid question do I need to run ./crypt.sh locally or somewahere in netlify or in hugo template?

  6. imshuffling commented on Feb 4, 2020

    @imshuffling

    Any examples of this working for gatsby?

    Asking for a friend :)

  7. imshuffling commented on Feb 4, 2020

    @imshuffling

    ^ Got it working

    find ./public -type f -name \"*.html\" -exec npx staticrypt -f \"./src/components/password_template.html\" {} **** -o {} \\;
    
  8. edcous commented on Apr 14, 2020

    @edcous
    Author

    Stupid question do I need to run ./crypt.sh locally or somewahere in netlify or in hugo template?

    In netlify, in your build command

  9. staticman-cosmos commented on Nov 28, 2020

    @staticman-cosmos

    This is very awesome @Embraser01 -- has anybody made any progress in removing the flash of the password box before decryption? I've just start looking at the code a bit but hoping to find some way to replace that with a loading spinner or similar.

  10. robinmoisson commented on Feb 10, 2022

    @robinmoisson
    Owner

    Hey all, thanks for the issue and discussion. This is closed in #124 , which adds a "Remember me" checkbox. The passphrase is stored hashed + salted in localStorage, meaning it's a slightly bigger attack surface for decryting the encrypted page, but should mitigate password reuse leakage (see discussion in #120 ).

    @staticman-cosmos regarding the flash of the password box - you could modify the password_template file to show a spinner by default and hide the whole password box with a hidden class, and in the onload function you check:

    • if there is a stored passphrase, decrypt and show that
    • if there isn't (or it's wrong), show the password box instead

    This means that you might have very quick flash of spinner before the password box shows for a new user.

  11. linked a pull request that will close this issueAdd "Remember me" button #124on Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions