From da5a2e41c972ac61a48d697b5f5f0d6ac375129a Mon Sep 17 00:00:00 2001
From: groovecoder
Date: Fri, 1 Jun 2018 12:37:38 -0500
Subject: [PATCH 1/4] fix #143: hash email client-side before scanning
---
hibp.js | 4 +--
public/js/test.js | 39 ++++++++++++-----------------
views/home.hbs | 2 +-
views/partials/protect_yourself.hbs | 2 +-
views/partials/what_to_do.hbs | 2 +-
5 files changed, 20 insertions(+), 29 deletions(-)
diff --git a/hibp.js b/hibp.js
index bfef85f15..6036f31d6 100644
--- a/hibp.js
+++ b/hibp.js
@@ -5,17 +5,15 @@ const got = require("got");
const AppConstants = require("./app-constants");
const DBUtils = require("./db/utils");
const pkg = require("./package.json");
-const getSha1 = require("./sha1-utils");
const HIBP_USER_AGENT = `${pkg.name}/${pkg.version}`;
const HIBP = {
- async getBreachesForEmail(email) {
+ async getBreachesForEmail(sha1) {
let foundBreaches = [];
- const sha1 = getSha1(email);
const sha1Prefix = sha1.slice(0, 6);
const url = `${AppConstants.HIBP_STAGE_API_ROOT}/breachedaccount/range/${sha1Prefix}?code=${encodeURIComponent(AppConstants.HIBP_STAGE_API_TOKEN)}`;
const headers = {
diff --git a/public/js/test.js b/public/js/test.js
index 1f15de46c..2d216142e 100644
--- a/public/js/test.js
+++ b/public/js/test.js
@@ -25,26 +25,6 @@ function doXHR(aURL, aBodyObj, aAlertText, aDebug=true) {
});
}
-// eslint-disable-next-line no-unused-vars
-function addUser() {
- doXHR("/user/add",
- { email: document.getElementById("addUserField").value })
- .then(function() {
- alert("A verification link has been emailed to the specified address.");
- });
-}
-
-// eslint-disable-next-line no-unused-vars
-function removeUser() {
- doXHR("/user/remove",
- { email: document.getElementById("removeUserField").value });
-}
-
-// eslint-disable-next-line no-unused-vars
-// function doOauth() {
-// window.open("/oauth/init");
-// }
-
// function isValidEmail(val) {
// // https://stackoverflow.com/a/46181
// const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
@@ -60,8 +40,6 @@ function removeUser() {
// }
// }
-
-
function showFalseDoor(){
const falseDoorBlurb = "Thank you for trying Firefox Monitor
FireFox Monitor is a concept we are testing. We hope to provide the service to everyone soon.
Stay up-to-date with Firefox Monitor and other new features when you sign up for the Firefox newsletter.
";
const falseDoor = document.createElement("div");
@@ -75,9 +53,24 @@ function showFalseDoor(){
}
+async function sha1(message) {
+ const msgBuffer = new TextEncoder("utf-8").encode(message);
+ const hashBuffer = await crypto.subtle.digest("SHA-1", msgBuffer);
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
+ const hashHex = hashArray.map(b => ("00" + b.toString(16)).slice(-2)).join("");
+ return hashHex;
+}
+async function hashEmailAndSend(emailFormSubmitEvent) {
+ emailFormSubmitEvent.preventDefault();
+ const emailForm = emailFormSubmitEvent.target;
+ for (const emailInput of emailForm.querySelectorAll("input[type=email]")) {
+ emailInput.value = await sha1(emailInput.value);
+ }
+ emailForm.submit();
+}
-
+document.querySelector(".email-scan").addEventListener("submit", hashEmailAndSend);
$(document).foundation();
document.querySelector("#sign-up").addEventListener("click", showFalseDoor);
diff --git a/views/home.hbs b/views/home.hbs
index 1713a7fcb..d0da88877 100644
--- a/views/home.hbs
+++ b/views/home.hbs
@@ -13,7 +13,7 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec volutpat hendrerit !nibus.
-