2015-01-27 21:10:55 +01:00
|
|
|
/*
|
2022-09-26 20:30:40 +02:00
|
|
|
Copyright (C) 2013-2022 Grégory Soutadé
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2015-01-27 21:10:55 +01:00
|
|
|
This file is part of gPass.
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2015-01-27 21:10:55 +01:00
|
|
|
gPass is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2015-01-27 21:10:55 +01:00
|
|
|
gPass is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2015-01-27 21:10:55 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with gPass. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
/* from misc.js */
|
|
|
|
/* Can't directly add it, because it's now a module */
|
|
|
|
var DEBUG = false;
|
|
|
|
var browser = browser || chrome;
|
|
|
|
const SERVER = {OK : 0, FAILED : 1, RESTART_REQUEST : 2};
|
|
|
|
const GPASS_ICON = {NORMAL:0, DISABLED:1, ACTIVATED:2};
|
2015-01-27 21:10:55 +01:00
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
function debug(s)
|
2015-01-27 21:10:55 +01:00
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
if (DEBUG)
|
|
|
|
console.log(s);
|
|
|
|
}
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
function notify(text, data)
|
|
|
|
{
|
|
|
|
browser.runtime.sendMessage({type: "notification", options:{"message":text, "data":data}});
|
2015-01-27 21:10:55 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
|
|
|
|
var gpass_enabled = true;
|
|
|
|
|
2015-11-05 18:16:33 +01:00
|
|
|
function _add_name(logins, name)
|
|
|
|
{
|
|
|
|
for(var i=0; i<logins.length; i++)
|
|
|
|
if (logins[i] == name) return ;
|
|
|
|
logins.push(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function try_get_name(fields, type_filters, match)
|
|
|
|
{
|
|
|
|
var user = null;
|
|
|
|
var all_logins = new Array();
|
|
|
|
|
|
|
|
for (var i=0; i<fields.length; i++)
|
|
|
|
{
|
|
|
|
var field = fields[i];
|
2021-03-02 14:53:06 +01:00
|
|
|
var to_test = !match;
|
2015-11-05 18:16:33 +01:00
|
|
|
|
|
|
|
for (var a=0; a<type_filters.length; a++)
|
|
|
|
{
|
2021-03-02 14:53:06 +01:00
|
|
|
if (match && field.getAttribute("type") == type_filters[a])
|
|
|
|
{
|
|
|
|
to_test = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!match && field.getAttribute("type") == type_filters[a])
|
|
|
|
{
|
|
|
|
to_test = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to_test)
|
|
|
|
{
|
|
|
|
if (field.hasAttribute("name") && field.value != "")
|
2015-11-05 18:16:33 +01:00
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var name = field.getAttribute("name");
|
2021-03-02 14:53:06 +01:00
|
|
|
// Subset of common user field
|
|
|
|
if (name == "user") user = field.value;
|
|
|
|
else if (name == "usr") user = field.value;
|
|
|
|
else if (name == "username") user = field.value;
|
|
|
|
else if (name == "login") user = field.value;
|
|
|
|
|
|
|
|
_add_name(all_logins, field.value);
|
2015-11-05 18:16:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
return new Array(user);
|
|
|
|
else
|
|
|
|
return all_logins;
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:53:06 +01:00
|
|
|
function get_logins(form, all_logins)
|
|
|
|
{
|
|
|
|
var fields = form.getElementsByTagName("input");
|
|
|
|
var logins = null;
|
|
|
|
|
|
|
|
debug("get_logins");
|
|
|
|
|
|
|
|
var type_filters = new Array();
|
|
|
|
|
|
|
|
// Get all <input type="text"> && <input type="email">
|
|
|
|
type_filters.push("text");
|
|
|
|
type_filters.push("email");
|
|
|
|
if (all_logins)
|
|
|
|
logins = try_get_name(fields, type_filters, true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get all other fields except text, email and password
|
|
|
|
type_filters.push("password");
|
|
|
|
type_filters.push("hidden");
|
|
|
|
logins = try_get_name(fields, type_filters, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return logins;
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
function on_focus(e)
|
|
|
|
{
|
2021-03-02 14:53:06 +01:00
|
|
|
if (!gpass_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var logins = get_logins(this.form, true);
|
|
|
|
var all_logins = get_logins(this.form, false);
|
|
|
|
|
|
|
|
if (logins.length || all_logins.length)
|
2020-01-18 15:20:13 +01:00
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2020-01-18 15:20:13 +01:00
|
|
|
type:"update_icon",
|
|
|
|
icon_id:GPASS_ICON.ACTIVATED,
|
2021-03-02 14:53:06 +01:00
|
|
|
};
|
|
|
|
browser.runtime.sendMessage(parameters, {});
|
2020-01-18 15:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function on_blur(e)
|
|
|
|
{
|
2021-03-02 14:53:06 +01:00
|
|
|
if (!gpass_enabled)
|
|
|
|
return;
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2021-03-02 14:53:06 +01:00
|
|
|
type:"update_icon",
|
|
|
|
icon_id:GPASS_ICON.NORMAL,
|
|
|
|
};
|
|
|
|
browser.runtime.sendMessage(parameters, {});
|
2020-01-18 15:20:13 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 21:10:55 +01:00
|
|
|
function on_sumbit(e)
|
|
|
|
{
|
|
|
|
var form = this;
|
|
|
|
var fields = form.getElementsByTagName("input");
|
2020-01-18 15:20:13 +01:00
|
|
|
var domain = form.ownerDocument.baseURI;
|
|
|
|
var password_computed = false;
|
2015-01-27 21:10:55 +01:00
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
debug("on_submit");
|
2015-01-27 21:10:55 +01:00
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
var logins = get_logins(form, true);
|
|
|
|
var all_logins = get_logins(form, false);
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2015-11-05 18:16:33 +01:00
|
|
|
if (!logins.length)
|
2020-01-18 15:20:13 +01:00
|
|
|
logins = all_logins;
|
|
|
|
|
|
|
|
e.preventDefault();
|
2015-01-27 21:10:55 +01:00
|
|
|
|
|
|
|
// Look for <input type="password" value="@@...">
|
|
|
|
for (var i=0; i<fields.length; i++)
|
|
|
|
{
|
|
|
|
var field = fields[i];
|
|
|
|
|
|
|
|
if (field.getAttribute("type") == "password")
|
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var password = field.value;
|
2020-01-18 15:20:13 +01:00
|
|
|
if (!password.startsWith("@@") && !password.startsWith("@_"))
|
2015-01-27 21:10:55 +01:00
|
|
|
continue;
|
|
|
|
|
2017-04-17 20:39:53 +02:00
|
|
|
// Remove current value to limit master key stealing
|
|
|
|
field.value = "";
|
2020-01-18 15:20:13 +01:00
|
|
|
password_computed = true;
|
2022-09-26 20:30:40 +02:00
|
|
|
var do_submit = !password.startsWith("@_");
|
|
|
|
var mkey = password.substring(2);
|
2015-01-27 21:10:55 +01:00
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2020-01-18 15:20:13 +01:00
|
|
|
type:"password",
|
|
|
|
logins:logins,
|
|
|
|
domain:domain,
|
|
|
|
mkey:mkey,
|
|
|
|
options:{field_id:i}
|
|
|
|
};
|
|
|
|
|
|
|
|
browser.runtime.sendMessage(parameters, {},
|
|
|
|
function (response) {
|
|
|
|
debug(response);
|
|
|
|
switch(response.value)
|
|
|
|
{
|
|
|
|
case SERVER.OK:
|
2022-09-26 20:30:40 +02:00
|
|
|
var field = fields[response.options.field_id];
|
2020-01-18 15:20:13 +01:00
|
|
|
set_password(form, field, response.password, do_submit)
|
|
|
|
notify("Password successfully replaced", "");
|
|
|
|
break;
|
|
|
|
case SERVER.FAILED:
|
2022-09-26 20:30:40 +02:00
|
|
|
if (logins.length != all_logins.length && all_logins.length != 0)
|
2020-01-18 15:20:13 +01:00
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
debug("Try with all logins");
|
|
|
|
parameters[logins] = all_logins;
|
|
|
|
browser.runtime.sendMessage(parameters);
|
2020-01-18 15:20:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SERVER.RESTART_REQUEST:
|
|
|
|
i = -1; // Restart loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 20:30:40 +02:00
|
|
|
);
|
2015-01-27 21:10:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
if (!password_computed)
|
|
|
|
{
|
|
|
|
debug("No password computed");
|
2024-04-28 09:44:52 +02:00
|
|
|
if (form.submit)
|
|
|
|
form.submit();
|
2020-01-18 15:20:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
return true;
|
2015-01-27 21:10:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
function set_password(form, field, password, do_submit)
|
|
|
|
{
|
|
|
|
field.value = password;
|
|
|
|
// Remove gPass event listener and submit again with clear password
|
|
|
|
unblock_all_forms();
|
|
|
|
if (do_submit)
|
|
|
|
{
|
|
|
|
// Propagate change
|
2022-09-26 20:30:40 +02:00
|
|
|
var change_cb = field.onchange;
|
2020-01-18 15:20:13 +01:00
|
|
|
if (change_cb)
|
|
|
|
change_cb();
|
|
|
|
// Try to type "enter"
|
|
|
|
var evt = new KeyboardEvent("keydown");
|
|
|
|
delete evt.which;
|
|
|
|
evt.which = 13;
|
|
|
|
field.dispatchEvent(evt);
|
|
|
|
// Submit form
|
2024-04-28 09:44:52 +02:00
|
|
|
if (form.submit)
|
|
|
|
form.submit();
|
2020-01-18 15:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var managed_forms = new Array();
|
|
|
|
|
|
|
|
function block_all_forms(doc, do_block)
|
2015-01-27 21:10:55 +01:00
|
|
|
{
|
2020-01-18 15:20:13 +01:00
|
|
|
var first_time = (managed_forms.length == 0);
|
|
|
|
var cur_focused_element = document.activeElement;
|
|
|
|
|
|
|
|
debug("block all forms");
|
|
|
|
|
|
|
|
gpass_enabled = do_block;
|
2017-07-08 08:43:26 +02:00
|
|
|
|
2015-01-27 21:10:55 +01:00
|
|
|
// If there is a password in the form, add a "submit" listener
|
|
|
|
for(var i=0; i<doc.forms.length; i++)
|
|
|
|
{
|
|
|
|
var form = doc.forms[i];
|
|
|
|
var fields = form.getElementsByTagName("input");
|
2020-01-18 15:20:13 +01:00
|
|
|
for (var a=0; a<fields.length; a++)
|
2015-01-27 21:10:55 +01:00
|
|
|
{
|
|
|
|
var field = fields[a];
|
|
|
|
if (field.getAttribute("type") == "password")
|
|
|
|
{
|
2020-01-18 15:20:13 +01:00
|
|
|
if (do_block)
|
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var old_cb = form.onsubmit;
|
2020-01-18 15:20:13 +01:00
|
|
|
if (old_cb)
|
|
|
|
form.removeEventListener("submit", old_cb);
|
|
|
|
form.addEventListener("submit", on_sumbit);
|
|
|
|
if (old_cb)
|
|
|
|
form.addEventListener("submit", old_cb);
|
|
|
|
field.addEventListener("focus", on_focus);
|
|
|
|
field.addEventListener("blur", on_blur);
|
|
|
|
if (cur_focused_element === field)
|
|
|
|
on_focus(null);
|
|
|
|
}
|
|
|
|
if (first_time)
|
|
|
|
managed_forms.push(form);
|
2015-01-27 21:10:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
function unblock_all_forms()
|
2015-01-27 21:10:55 +01:00
|
|
|
{
|
2020-01-18 15:20:13 +01:00
|
|
|
debug("unblock all forms");
|
|
|
|
|
|
|
|
for(var i=0; i<managed_forms.length; i++)
|
2017-04-17 20:39:53 +02:00
|
|
|
{
|
2020-01-18 15:20:13 +01:00
|
|
|
var form = managed_forms[i];
|
|
|
|
|
|
|
|
form.removeEventListener("submit", on_sumbit);
|
|
|
|
|
|
|
|
var fields = form.getElementsByTagName("input");
|
|
|
|
for (var a=0; a<fields.length; a++)
|
|
|
|
{
|
|
|
|
var field = fields[a];
|
|
|
|
if (field.getAttribute("type") == "password")
|
|
|
|
{
|
|
|
|
field.removeEventListener("focus", on_focus);
|
|
|
|
field.removeEventListener("blur", on_blur);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 20:39:53 +02:00
|
|
|
}
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
gpass_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
browser.runtime.onMessage.addListener(
|
|
|
|
function(request, sender, sendResponse) {
|
|
|
|
|
|
|
|
if (request.type == "getUsername")
|
|
|
|
{
|
|
|
|
debug("getUsername");
|
|
|
|
if (managed_forms.length == 1)
|
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var fields = managed_forms[0].getElementsByTagName("input");
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
var type_filters = new Array();
|
2020-01-18 15:20:13 +01:00
|
|
|
// Get all <input type="text"> && <input type="email">
|
|
|
|
type_filters.push("text");
|
|
|
|
type_filters.push("email");
|
2022-09-26 20:30:40 +02:00
|
|
|
var logins = try_get_name(fields, type_filters, true);
|
2020-01-18 15:20:13 +01:00
|
|
|
|
|
|
|
if (logins.length == 1)
|
|
|
|
sendResponse(logins[0]);
|
|
|
|
else
|
|
|
|
sendResponse("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sendResponse("");
|
|
|
|
}
|
|
|
|
else if (request.type == "setPassword")
|
|
|
|
{
|
|
|
|
debug("setPassword");
|
|
|
|
var response = "";
|
|
|
|
if (managed_forms.length == 1)
|
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var fields = managed_forms[0].getElementsByTagName("input");
|
|
|
|
var password_field = null;
|
2020-01-18 15:20:13 +01:00
|
|
|
|
2022-09-26 20:30:40 +02:00
|
|
|
for (var a=0; a<fields.length; a++)
|
2020-01-18 15:20:13 +01:00
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var field = fields[a];
|
2020-01-18 15:20:13 +01:00
|
|
|
if (field.getAttribute("type") == "password")
|
|
|
|
{
|
|
|
|
if (password_field == null)
|
|
|
|
password_field = field;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// More than one password field : abort
|
|
|
|
password_field = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (password_field)
|
|
|
|
{
|
|
|
|
set_password(managed_forms[0], password_field,
|
2022-09-26 20:30:40 +02:00
|
|
|
request.password, request.submit);
|
2020-01-18 15:20:13 +01:00
|
|
|
response = "ok";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sendResponse(response);
|
|
|
|
}
|
|
|
|
else if (request.type == "blockForms")
|
|
|
|
{
|
|
|
|
block_all_forms(document, true);
|
|
|
|
}
|
|
|
|
else if (request.type == "unblockForms")
|
|
|
|
{
|
|
|
|
unblock_all_forms();
|
|
|
|
}
|
2022-09-26 20:30:40 +02:00
|
|
|
|
|
|
|
return true;
|
2020-01-18 15:20:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function document_loaded()
|
|
|
|
{
|
2022-09-26 20:30:40 +02:00
|
|
|
var parameters = {
|
2020-01-18 15:20:13 +01:00
|
|
|
"type": "is_gpass_enabled",
|
|
|
|
};
|
|
|
|
|
|
|
|
browser.runtime.sendMessage(parameters, {},
|
|
|
|
function (response) {
|
|
|
|
if (response)
|
|
|
|
block_all_forms(document, response.enabled);
|
|
|
|
});
|
2015-01-27 21:10:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:20:13 +01:00
|
|
|
document_loaded();
|
|
|
|
|
|
|
|
console.log("Welcome to gPass web extension v0.9 !");
|
2017-12-05 08:06:48 +01:00
|
|
|
console.log("Privacy Policy can be found at http://indefero.soutade.fr/p/gpass/source/tree/master/PrivacyPolicy.md");
|
|
|
|
console.log("");
|