summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_getPasswordFields.js
blob: eee38c30d86c053b36d1f1a8b00b382b2a10b87d (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * Test for LoginFormState._getPasswordFields using LoginFormFactory.
 */

/* globals todo_check_eq */
"use strict";

const { LoginFormFactory } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginFormFactory.sys.mjs"
);
const { LoginFormState } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginManagerChild.sys.mjs"
);
const TESTCASES = [
  {
    description: "Empty document",
    document: ``,
    returnedFieldIDsByFormLike: [],
    minPasswordLength: undefined,
  },
  {
    description: "Non-password input with no <form> present",
    document: `<input>`,
    // Only the IDs of password fields should be in this array
    returnedFieldIDsByFormLike: [[]],
    minPasswordLength: undefined,
  },
  {
    description: "1 password field outside of a <form>",
    document: `<input id="pw1" type=password>`,
    returnedFieldIDsByFormLike: [["pw1"]],
    minPasswordLength: undefined,
  },
  {
    description: "5 empty password fields outside of a <form>",
    document: `<input id="pw1" type=password>
      <input id="pw2" type=password>
      <input id="pw3" type=password>
      <input id="pw4" type=password>
      <input id="pw5" type=password>`,
    returnedFieldIDsByFormLike: [["pw1", "pw2", "pw3", "pw4", "pw5"]],
    minPasswordLength: undefined,
  },
  {
    description: "6 empty password fields outside of a <form>",
    document: `<input id="pw1" type=password>
      <input id="pw2" type=password>
      <input id="pw3" type=password>
      <input id="pw4" type=password>
      <input id="pw5" type=password>
      <input id="pw6" type=password>`,
    returnedFieldIDsByFormLike: [[]],
    minPasswordLength: undefined,
  },
  {
    description:
      "4 password fields outside of a <form> (1 empty, 3 full) with minPasswordLength=2",
    document: `<input id="pw1" type=password>
      <input id="pw2" type=password value="pass2">
      <input id="pw3" type=password value="pass3">
      <input id="pw4" type=password value="pass4">`,
    returnedFieldIDsByFormLike: [["pw2", "pw3", "pw4"]],
    minPasswordLength: 2,
  },
  {
    description: "Form with 1 password field",
    document: `<form><input id="pw1" type=password></form>`,
    returnedFieldIDsByFormLike: [["pw1"]],
    minPasswordLength: undefined,
  },
  {
    description: "Form with 2 password fields",
    document: `<form><input id="pw1" type=password><input id='pw2' type=password></form>`,
    returnedFieldIDsByFormLike: [["pw1", "pw2"]],
    minPasswordLength: undefined,
  },
  {
    description: "1 password field in a form, 1 outside",
    document: `<form><input id="pw1" type=password></form><input id="pw2" type=password>`,
    returnedFieldIDsByFormLike: [["pw1"], ["pw2"]],
    minPasswordLength: undefined,
  },
  {
    description:
      "2 password fields outside of a <form> with 1 linked via @form",
    document: `<input id="pw1" type=password><input id="pw2" type=password form='form1'>
      <form id="form1"></form>`,
    returnedFieldIDsByFormLike: [["pw1"], ["pw2"]],
    minPasswordLength: undefined,
  },
  {
    description:
      "2 password fields outside of a <form> with 1 linked via @form + minPasswordLength",
    document: `<input id="pw1" type=password><input id="pw2" type=password form="form1">
      <form id="form1"></form>`,
    returnedFieldIDsByFormLike: [[], []],
    minPasswordLength: 2,
  },
  {
    description: "minPasswordLength should also skip white-space only fields",
    /* eslint-disable no-tabs */
    document: `<input id="pw-space" type=password value=" ">
               <input id="pw-tab" type=password value="	">
               <input id="pw-newline" type=password form="form1" value="
                                                                        ">
      <form id="form1"></form>`,
    /* eslint-enable no-tabs */
    returnedFieldIDsByFormLike: [[], []],
    minPasswordLength: 2,
  },
  {
    description: "minPasswordLength should skip too-short field values",
    document: `<form>
               <input id="pw-empty" type=password>
               <input id="pw-tooshort" type=password value="p">
               <input id="pw" type=password value="pz">
               </form>`,
    returnedFieldIDsByFormLike: [["pw"]],
    minPasswordLength: 2,
  },
  {
    description: "minPasswordLength should allow matching-length field values",
    document: `<form>
               <input id="pw-empty" type=password>
               <input id="pw-matchlen" type=password value="pz">
               <input id="pw" type=password value="pazz">
               </form>`,
    returnedFieldIDsByFormLike: [["pw-matchlen", "pw"]],
    minPasswordLength: 2,
  },
  {
    description:
      "2 password fields outside of a <form> with 1 linked via @form + minPasswordLength with 1 empty",
    document: `<input id="pw1" type=password value=" pass1 "><input id="pw2" type=password form="form1">
      <form id="form1"></form>`,
    returnedFieldIDsByFormLike: [["pw1"], []],
    minPasswordLength: 2,
    fieldOverrideRecipe: {
      // Ensure a recipe without `notPasswordSelector` doesn't cause a problem.
      hosts: ["localhost:8080"],
    },
  },
  {
    description:
      "3 password fields outside of a <form> with 1 linked via @form + minPasswordLength",
    document: `<input id="pw1" type=password value="pass1"><input id="pw2" type=password form="form1" value="pass2"><input id="pw3" type=password value="pass3">
      <form id="form1"><input id="pw4" type=password></form>`,
    returnedFieldIDsByFormLike: [["pw3"], ["pw2"]],
    minPasswordLength: 2,
    fieldOverrideRecipe: {
      hosts: ["localhost:8080"],
      notPasswordSelector: "#pw1",
    },
  },
  {
    beforeGetFunction(doc) {
      doc.getElementById("pw1").remove();
    },
    description:
      "1 password field outside of a <form> which gets removed/disconnected",
    document: `<input id="pw1" type=password>`,
    returnedFieldIDsByFormLike: [[]],
    minPasswordLength: undefined,
  },
];

for (let tc of TESTCASES) {
  info("Sanity checking the testcase: " + tc.description);

  (function () {
    let testcase = tc;
    add_task(async function () {
      info("Starting testcase: " + testcase.description);
      let document = MockDocument.createTestDocument(
        "http://localhost:8080/test/",
        testcase.document
      );

      let mapRootElementToFormLike = new Map();
      for (let input of document.querySelectorAll("input")) {
        let formLike = LoginFormFactory.createFromField(input);
        let existingFormLike = mapRootElementToFormLike.get(
          formLike.rootElement
        );
        if (!existingFormLike) {
          mapRootElementToFormLike.set(formLike.rootElement, formLike);
          continue;
        }

        // If the formLike is already present, ensure that the properties are the same.
        info(
          "Checking if the new FormLike for the same root has the same properties"
        );
        formLikeEqual(formLike, existingFormLike);
      }

      if (testcase.beforeGetFunction) {
        await testcase.beforeGetFunction(document);
      }

      Assert.strictEqual(
        mapRootElementToFormLike.size,
        testcase.returnedFieldIDsByFormLike.length,
        "Check the correct number of different formLikes were returned"
      );

      let formLikeIndex = -1;
      for (let formLikeFromInput of mapRootElementToFormLike.values()) {
        formLikeIndex++;
        let pwFields = LoginFormState._getPasswordFields(formLikeFromInput, {
          fieldOverrideRecipe: testcase.fieldOverrideRecipe,
          minPasswordLength: testcase.minPasswordLength,
        });

        if (
          ChromeUtils.getClassName(formLikeFromInput.rootElement) ===
          "HTMLFormElement"
        ) {
          let formLikeFromForm = LoginFormFactory.createFromForm(
            formLikeFromInput.rootElement
          );
          info(
            "Checking that the FormLike created for the <form> matches" +
              " the one from a password field"
          );
          formLikeEqual(formLikeFromInput, formLikeFromForm);
        }

        if (testcase.returnedFieldIDsByFormLike[formLikeIndex].length === 0) {
          Assert.strictEqual(
            pwFields,
            null,
            "If no password fields were found null should be returned"
          );
        } else {
          Assert.strictEqual(
            pwFields.length,
            testcase.returnedFieldIDsByFormLike[formLikeIndex].length,
            "Check the # of password fields for formLike #" + formLikeIndex
          );
        }

        for (
          let i = 0;
          i < testcase.returnedFieldIDsByFormLike[formLikeIndex].length;
          i++
        ) {
          let expectedID =
            testcase.returnedFieldIDsByFormLike[formLikeIndex][i];
          Assert.strictEqual(
            pwFields[i].element.id,
            expectedID,
            "Check password field " + i + " ID"
          );
        }
      }
    });
  })();
}

const EMOJI_TESTCASES = [
  {
    description:
      "Single characters composed of 2 code units should ideally fail minPasswordLength of 2",
    document: `<form>
               <input id="pw" type=password value="💩">
               </form>`,
    returnedFieldIDsByFormLike: [["pw"]],
    minPasswordLength: 2,
  },
  {
    description:
      "Single characters composed of multiple code units should ideally fail minPasswordLength of 2",
    document: `<form>
               <input id="pw" type=password value="👪">
               </form>`,
    minPasswordLength: 2,
  },
];

// Note: Bug 780449 tracks our handling of emoji and multi-code-point characters in password fields
// and the .length we should expect when a password value includes them
for (let tc of EMOJI_TESTCASES) {
  info("Sanity checking the testcase: " + tc.description);

  (function () {
    let testcase = tc;
    add_task(async function () {
      info("Starting testcase: " + testcase.description);
      let document = MockDocument.createTestDocument(
        "http://localhost:8080/test/",
        testcase.document
      );
      let input = document.querySelector("input[type='password']");
      Assert.ok(input, "Found the password field");
      let formLike = LoginFormFactory.createFromField(input);
      let pwFields = LoginFormState._getPasswordFields(formLike, {
        minPasswordLength: testcase.minPasswordLength,
      });
      info("Got password fields: " + pwFields.length);
      todo_check_eq(
        pwFields.length,
        0,
        "Check a single-character (emoji) password is excluded from the password fields collection"
      );
    });
  })();
}