home *** CD-ROM | disk | FTP | other *** search
/ Freelog 101 / FreelogNo101-JanvierFevrier2011.iso / Securite / Passter / Passter.exe / content.js < prev    next >
Text File  |  2010-10-26  |  9KB  |  251 lines

  1. /* ------------------------------------------------------------------------------------ */
  2. /* Copyright (c) 2010 PASSTER.COM.  All rights reserved.                             */
  3. /* This software and all information contained therein is confidential and              */
  4. /* proprietary and shall not be duplicated, used, disclosed or disseminated in any      */
  5. /* way except as authorized by the applicable license agreement, without the            */
  6. /* express written permission of PASSTER.COM. All authorized reproductions must be   */
  7. /* marked with this language.                                                           */
  8. /*                                                                                      */
  9. /* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE                      */
  10. /* EXTENT PERMITTED BY APPLICABLE LAW, PASSTER.COM PROVIDES THIS SOFTWARE            */
  11. /* WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY                      */
  12. /* IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR                    */
  13. /* PURPOSE.  IN NO EVENT WILL PASSTER.COM BE LIABLE TO THE END USER OR ANY           */
  14. /* THIRD PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE                     */
  15. /* USE OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, LOST PROFITS,                    */
  16. /* BUSINESS INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF PASSTER.COM IS             */
  17. /* EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE.                                            */
  18. /* ------------------------------------------------------------------------------------ */
  19.  
  20. chrome.extension.sendRequest({"init":true});
  21. //$(document).ready(function() {
  22.  
  23. function formSubmit() {
  24.     formSubmit(null);
  25. }
  26.  
  27. function formSubmit(frm) {
  28.     console.log("Submitting form: " + frm.id);
  29.     var msg = buildFormFields(frm, "pwdForm", true);
  30.     if (msg && msg.pwdForm[0].username) {
  31.         console.log("Form fields were built - submit");
  32.         chrome.extension.sendRequest(msg);
  33.     }
  34.     return true;
  35. }
  36.  
  37.  
  38. function getPasswordFields (form, skipEmptyFields) {
  39.     // Locate the password fields in the form.
  40.     var pwFields = [];
  41.     for (var i = 0; i < form.elements.length; i++) {
  42.         if (form.elements[i].type != "password")
  43.             continue;
  44.  
  45.         if (skipEmptyFields && !form.elements[i].value)
  46.             continue;
  47.  
  48.         pwFields[pwFields.length] = {
  49.             index   : i,
  50.             element : form.elements[i]
  51.         };
  52.     }
  53.  
  54.     // If too few or too many fields, bail out.
  55.     if (pwFields.length == 0) {
  56.         console.log("(form ignored -- no password fields.)");
  57.         return null;
  58.     } else if (pwFields.length > 3) {
  59.         console.log("(form ignored -- too many password fields. [got " +
  60.         pwFields.length + "])");
  61.         return null;
  62.     }
  63.  
  64.     return pwFields;
  65. }
  66.  
  67.  
  68.  
  69. function getFormFields (form, isSubmission) {
  70.     var usernameField = null;
  71.  
  72.     // Locate the password field(s) in the form. Up to 3 supported.
  73.     // If there's no password field, there's nothing for us to do.
  74.     var pwFields = getPasswordFields(form, isSubmission);
  75.     if (!pwFields)
  76.         return [null, null, null];
  77.  
  78.  
  79.     // Locate the username field in the form by searching backwards
  80.     // from the first passwordfield, assume the first text field is the
  81.     // username. We might not find a username field if the user is
  82.     // already logged in to the site.
  83.     for (var i = pwFields[0].index - 1; i >= 0; i--) {
  84.         if (form.elements[i].type == "text") {
  85.             usernameField = form.elements[i];
  86.             break;
  87.         }
  88.     }
  89.  
  90.     if (!usernameField)
  91.         console.log("(form -- no username field found)");
  92.  
  93.     // If we're not submitting a form (it's a page load), there are no
  94.     // password field values for us to use for identifying fields. So,
  95.     // just assume the first password field is the one to be filled in.
  96.     if (!isSubmission || pwFields.length == 1)
  97.         return [usernameField, pwFields[0].element, null];
  98.  
  99.     // Try to figure out WTF is in the form based on the password values.
  100.     var oldPasswordField, newPasswordField;
  101.     var pw1 = pwFields[0].element.value;
  102.     var pw2 = pwFields[1].element.value;
  103.     var pw3 = (pwFields[2] ? pwFields[2].element.value : null);
  104.     
  105.     if (pwFields.length == 3) {
  106.     // Look for two identical passwords, that's the new password
  107.  
  108.     if (pw1 == pw2 && pw2 == pw3) {
  109.         // All 3 passwords the same? Weird! Treat as if 1 pw field.
  110.         newPasswordField = pwFields[0].element;
  111.         oldPasswordField = null;
  112.     } else if (pw1 == pw2) {
  113.         newPasswordField = pwFields[0].element;
  114.         oldPasswordField = pwFields[2].element;
  115.     } else if (pw2 == pw3) {
  116.         oldPasswordField = pwFields[0].element;
  117.         newPasswordField = pwFields[2].element;
  118.     } else  if (pw1 == pw3) {
  119.         // A bit odd, but could make sense with the right page layout.
  120.         newPasswordField = pwFields[0].element;
  121.         oldPasswordField = pwFields[1].element;
  122.     } else {
  123.         // We can't tell which of the 3 passwords should be saved.
  124.         console.log("(form ignored -- all 3 pw fields differ)");
  125.             return [null, null, null];
  126.         }
  127.     } else { // pwFields.length == 2
  128.         if (pw1 == pw2) {
  129.             // Treat as if 1 pw field
  130.             newPasswordField = pwFields[0].element;
  131.             oldPasswordField = null;
  132.         } else {
  133.           // Just assume that the 2nd password is the new password
  134.             oldPasswordField = pwFields[0].element;
  135.             newPasswordField = pwFields[1].element;
  136.         }
  137.     }
  138.  
  139.     return [usernameField, newPasswordField, oldPasswordField];
  140. }
  141.  
  142.  
  143. function removeDomainPrefix(domain) {
  144.     var result = domain;
  145.     if (domain.indexOf("www.") == 0)
  146.         result = domain.substring(4);
  147.     return result;
  148. }
  149.  
  150. var domain = removeDomainPrefix(window.location.hostname);
  151. chrome.extension.sendRequest({"domain":domain});
  152.  
  153. $("link[rel='shortcut icon']").each(function () { chrome.extension.sendRequest({"domainIcon":this.href}); } );
  154.  
  155. var pwdFields = $("input:password");
  156. if (pwdFields && pwdFields.length > 0) {
  157.     // first send notification of password fields
  158.     console.log("found some password fields");
  159.     var pwdFieldsArr = buildIdNameFieldArr(pwdFields);
  160.     var domain = removeDomainPrefix(window.location.hostname);
  161.     chrome.extension.sendRequest({"password":true, "pwdFields":pwdFieldsArr, "domain":domain});
  162.     if (document.getElementsByTagName) {
  163.         elementsForms = document.getElementsByTagName("form");
  164.         console.log("found " + elementsForms.length + " forms in the page");
  165.         // send notification of form fields
  166.         $('form').each(function() {
  167.             console.log("Processing form: " + this.id);
  168.             var msg = null;
  169.             msg = buildFormFields(this, "detectedPwdForm", false);
  170.             if (msg) {
  171.                 console.log("Form fields were built - detect");
  172.                 chrome.extension.sendRequest(msg);
  173.             }
  174.             $(this).change(function() {
  175.                 var msg = buildFormFields(this, "pwdChgForm", true);
  176.                 if (msg && msg.pwdForm[0].username) {
  177.                     chrome.extension.sendRequest(msg);
  178.                 }
  179.             });
  180.             var org = this.onsubmit; 
  181.             this.onsubmit = function() { 
  182.                 formSubmit(this);
  183.                 var result = true;
  184.                 if (org) {
  185.                     result = org();    
  186.                 }
  187.                 return result;
  188.             };
  189.             $(':input').each( function () {
  190.                 $(this).submit( function () {
  191.                     $(this).attr('form').onsubmit();
  192.                 });
  193.             });
  194.         });
  195.         $("form a").each(function() {
  196.             var aObj = this;
  197.             var org = this.onclick; 
  198.             var form = $(this).parents("form"); 
  199.             form.each( function () {
  200.                 var frmSubmit = this;
  201.                 aObj.onclick = function () {
  202.                     formSubmit(frmSubmit);
  203.                     if (org) {
  204.                         org();    
  205.                     }
  206.                 }
  207.             });
  208.         });
  209.     } else {
  210.         console.log("did not find any form elements, can't continue...");
  211.     }
  212. } else {
  213.     // no password fields
  214.     console.log("did not find any password fields");
  215.     chrome.extension.sendRequest({"password":false});
  216. }
  217.  
  218. function buildIdNameFieldArr(fields) {
  219.     var arr = new Array();
  220.     for (var i = 0; i < fields.length; i++) {
  221.         var json = {"id":fields[i].id,"name":fields[i].name};
  222.         arr.push(json);
  223.     }
  224.     return arr;
  225. }
  226.  
  227. function buildFormFields(frm, type, isSubmit) {
  228.     var msg = null;
  229.     var action = frm.getAttribute("action");
  230.     if (!action) {
  231.         action = "";
  232.     }
  233.     // search for the username and password in the form
  234.     var frmFields = getFormFields(frm, isSubmit);
  235.     if (frmFields && frmFields.length == 3 && frmFields[1] != null ) {
  236.         var username = (frmFields[0]?frmFields[0].value:"");
  237.         var password1 = (frmFields[1]?frmFields[1].value:"");
  238.         var password2 = (frmFields[2]?frmFields[2].value:"");
  239.         var usernameElement = (frmFields[0] && frmFields[0].id?frmFields[0].id:(frmFields[0]?frmFields[0].name:""));
  240.         var password1Element = (frmFields[1] && frmFields[1].id?frmFields[1].id:(frmFields[1]?frmFields[1].name:""));
  241.         var password2Element = (frmFields[2] && frmFields[2].id?frmFields[2].id:(frmFields[2]?frmFields[2].name:""));
  242.         var title = document.title;
  243.         var url = document.location.href;
  244.         var domain = removeDomainPrefix(window.location.hostname);
  245.         var date = new Date();
  246.         msg = {"type" : type, "pwdForm" : [{"username" : username,  "password1" : password1, "password2" : password2, "usernameElm" : usernameElement, "password1Elm" : password1Element, "password2Elm" : password2Element, "url" : url, "domain" : domain, "title" : title, "action" : action, "touch" : date.getTime()}]};
  247.     }
  248.     return msg;
  249. }
  250.  
  251. //});