Bug 1913520 - Make filled fields data for FormAutofill use Map instead of Object. r=credential-management-reviewers,dimi

My main motivation is that it is easier to type Maps than Objects-as-maps, but
using Objects as maps can have some odd behavior if you aren't careful, so I
think it is probably a better idea to use anyways for this data that might
be coming from a compromised child process.

This slightly alters the behavior of autofillFields because it modifies
the existing map in place rather than creating a new one from scratch,
but I think that is preferable because nothing seems to have an external
reference so there's no need to preserve the old map.

Differential Revision: https://phabricator.services.mozilla.com/D219361
This commit is contained in:
Andrew McCreight
2024-08-19 13:18:48 +00:00
parent 6aae4f1658
commit a378e69577
6 changed files with 14 additions and 16 deletions

View File

@@ -886,7 +886,7 @@ function verifySectionAutofillResult(section, result, expectedSection) {
fieldDetails.forEach((field, fieldIndex) => {
const expected = expectedFieldDetails[fieldIndex];
Assert.equal(
result[field.elementId].value,
result.get(field.elementId).value,
expected.autofill ?? "",
`Autofilled value for element(identifier:${field.identifier}, field name:${field.fieldName}) should be equal`
);

View File

@@ -435,7 +435,7 @@ export class FormAutofillChild extends JSWindowActorChild {
}
async fillFields(focusedElementId, elementIds, profile) {
let result = {};
let result = new Map();
try {
Services.obs.notifyObservers(null, "autofill-fill-starting");
const handler = this.#getHandlerByElementId(elementIds[0]);

View File

@@ -822,7 +822,7 @@ export class FormAutofillParent extends JSWindowActorParent {
const ids = section.fieldDetails.map(detail => detail.elementId);
let filledResult = {};
let filledResult = new Map();
try {
filledResult = await this.sendQuery("FormAutofill:FillFields", {
focusedElementId: elementId,
@@ -830,10 +830,8 @@ export class FormAutofillParent extends JSWindowActorParent {
profile,
});
this.filledResult = {
...(this.filledResult ?? {}),
...filledResult,
};
this.filledResult = this.filledResult ?? new Map();
filledResult.forEach((value, key) => this.filledResult.set(key, value));
section.onFilled(filledResult);
} catch (e) {
@@ -845,11 +843,11 @@ export class FormAutofillParent extends JSWindowActorParent {
}
onFieldFilledModified(elementId) {
if (!this.filledResult?.[elementId]) {
if (!this.filledResult?.get(elementId)) {
return;
}
this.filledResult[elementId].filledState = FIELD_STATES.NORMAL;
this.filledResult.get(elementId).filledState = FIELD_STATES.NORMAL;
const section = this.getSectionByElementId(elementId);
@@ -865,7 +863,7 @@ export class FormAutofillParent extends JSWindowActorParent {
if (
inputs.every(
field =>
this.filledResult[field.elementId].filledState ==
this.filledResult.get(field.elementId).filledState ==
FIELD_STATES.NORMAL
)
) {

View File

@@ -122,7 +122,7 @@ class AutofillTelemetryBase {
const extra = this.#initFormEventExtra("unavailable");
for (const fieldDetail of fieldDetails) {
let { filledState, value } = data[fieldDetail.elementId];
let { filledState, value } = data.get(fieldDetail.elementId);
switch (filledState) {
case FIELD_STATES.AUTO_FILLED:
filledState = "filled";
@@ -152,7 +152,7 @@ class AutofillTelemetryBase {
const extra = this.#initFormEventExtra("unavailable");
for (const fieldDetail of fieldDetails) {
let { filledState, value } = data[fieldDetail.elementId];
let { filledState, value } = data.get(fieldDetail.elementId);
switch (filledState) {
case FIELD_STATES.AUTO_FILLED:
filledState = "autofilled";

View File

@@ -945,15 +945,15 @@ export class FormAutofillHandler {
* value: The value of the element
*/
collectFormFilledData() {
const filledData = {};
const filledData = new Map();
for (const fieldDetail of this.fieldDetails) {
const element = fieldDetail.element;
filledData[fieldDetail.elementId] = {
filledData.set(fieldDetail.elementId, {
filledState: element.autofillState,
filledValue: this.computeFillingValue(fieldDetail),
value: element.value,
};
});
}
return filledData;
}

View File

@@ -294,7 +294,7 @@ export class FormAutofillSection {
};
for (const detail of this.fieldDetails) {
const { filledValue } = formFilledData[detail.elementId];
const { filledValue } = formFilledData.get(detail.elementId);
if (
!filledValue ||