summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/formSubmitListener.js
blob: ec2c18f6c7b607d3c8af41ecd049958c7c5fef3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

(function() {

var Cc = Components.classes;
var Ci = Components.interfaces;

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");

var satchelFormListener = {
    QueryInterface : XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver,
                                            Ci.nsIDOMEventListener,
                                            Ci.nsIObserver,
                                            Ci.nsISupportsWeakReference]),

    debug          : true,
    enabled        : true,
    saveHttpsForms : true,

    init : function() {
        Services.obs.addObserver(this, "earlyformsubmit", false);
        Services.prefs.addObserver("browser.formfill.", this, false);
        this.updatePrefs();
        addEventListener("unload", this, false);
    },

    updatePrefs : function () {
        this.debug          = Services.prefs.getBoolPref("browser.formfill.debug");
        this.enabled        = Services.prefs.getBoolPref("browser.formfill.enable");
        this.saveHttpsForms = Services.prefs.getBoolPref("browser.formfill.saveHttpsForms");
    },

    // Implements the Luhn checksum algorithm as described at
    // http://wikipedia.org/wiki/Luhn_algorithm
    isValidCCNumber : function (ccNumber) {
        // Remove dashes and whitespace
        ccNumber = ccNumber.replace(/[\-\s]/g, '');

        let len = ccNumber.length;
        if (len != 9 && len != 15 && len != 16)
            return false;

        if (!/^\d+$/.test(ccNumber))
            return false;

        let total = 0;
        for (let i = 0; i < len; i++) {
            let ch = parseInt(ccNumber[len - i - 1]);
            if (i % 2 == 1) {
                // Double it, add digits together if > 10
                ch *= 2;
                if (ch > 9)
                    ch -= 9;
            }
            total += ch;
        }
        return total % 10 == 0;
    },

    log : function (message) {
        if (!this.debug)
            return;
        dump("satchelFormListener: " + message + "\n");
        Services.console.logStringMessage("satchelFormListener: " + message);
    },

    /* ---- dom event handler ---- */

    handleEvent: function(e) {
        switch (e.type) {
            case "unload":
                Services.obs.removeObserver(this, "earlyformsubmit");
                Services.prefs.removeObserver("browser.formfill.", this);
                break;

            default:
                this.log("Oops! Unexpected event: " + e.type);
                break;
        }
    },

    /* ---- nsIObserver interface ---- */

    observe : function (subject, topic, data) {
        if (topic == "nsPref:changed")
            this.updatePrefs();
        else
            this.log("Oops! Unexpected notification: " + topic);
    },

    /* ---- nsIFormSubmitObserver interfaces ---- */

    notify : function(form, domWin, actionURI, cancelSubmit) {
        try {
            // Even though the global context is for a specific browser, we
            // can receive observer events from other tabs! Ensure this event
            // is about our content.
            if (domWin.top != content)
                return;
            if (!this.enabled)
                return;

            if (PrivateBrowsingUtils.isContentWindowPrivate(domWin))
                return;

            this.log("Form submit observer notified.");

            if (!this.saveHttpsForms) {
                if (actionURI.schemeIs("https"))
                    return;
                if (form.ownerDocument.documentURIObject.schemeIs("https"))
                    return;
            }

            if (form.hasAttribute("autocomplete") &&
                form.getAttribute("autocomplete").toLowerCase() == "off")
                return;

            let entries = [];
            for (let i = 0; i < form.elements.length; i++) {
                let input = form.elements[i];
                if (!(input instanceof Ci.nsIDOMHTMLInputElement))
                    continue;

                // Only use inputs that hold text values (not including type="password")
                if (!input.mozIsTextField(true))
                    continue;

                // Bug 394612: If Login Manager marked this input, don't save it.
                // The login manager will deal with remembering it.

                // Don't save values when autocomplete=off is present.
                if (input.hasAttribute("autocomplete") &&
                    input.getAttribute("autocomplete").toLowerCase() == "off")
                    continue;

                let value = input.value.trim();

                // Don't save empty or unchanged values.
                if (!value || value == input.defaultValue.trim())
                    continue;

                // Don't save credit card numbers.
                if (this.isValidCCNumber(value)) {
                    this.log("skipping saving a credit card number");
                    continue;
                }

                let name = input.name || input.id;
                if (!name)
                    continue;

                if (name == 'searchbar-history') {
                    this.log('addEntry for input name "' + name + '" is denied')
                    continue;
                }

                // Limit stored data to 200 characters.
                if (name.length > 200 || value.length > 200) {
                    this.log("skipping input that has a name/value too large");
                    continue;
                }

                // Limit number of fields stored per form.
                if (entries.length >= 100) {
                    this.log("not saving any more entries for this form.");
                    break;
                }

                entries.push({ name: name, value: value });
            }

            if (entries.length) {
                this.log("sending entries to parent process for form " + form.id);
                sendAsyncMessage("FormHistory:FormSubmitEntries", entries);
            }
        }
        catch (e) {
            this.log("notify failed: " + e);
        }
    }
};

satchelFormListener.init();

})();