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
|
/* 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/. */
/**
* Provides a class to import login-related data CSV files.
*/
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
CSV: "resource://gre/modules/CSV.sys.mjs",
LoginHelper: "resource://gre/modules/LoginHelper.sys.mjs",
ResponsivenessMonitor: "resource://gre/modules/ResponsivenessMonitor.sys.mjs",
});
/**
* All the CSV column names will be converted to lower case before lookup
* so they must be specified here in lower case.
*/
const FIELD_TO_CSV_COLUMNS = {
origin: ["url", "login_uri"],
username: ["username", "login_username"],
password: ["password", "login_password"],
httpRealm: ["httprealm"],
formActionOrigin: ["formactionorigin"],
guid: ["guid"],
timeCreated: ["timecreated"],
timeLastUsed: ["timelastused"],
timePasswordChanged: ["timepasswordchanged"],
};
export const ImportFailedErrorType = Object.freeze({
CONFLICTING_VALUES_ERROR: "CONFLICTING_VALUES_ERROR",
FILE_FORMAT_ERROR: "FILE_FORMAT_ERROR",
FILE_PERMISSIONS_ERROR: "FILE_PERMISSIONS_ERROR",
UNABLE_TO_READ_ERROR: "UNABLE_TO_READ_ERROR",
});
export class ImportFailedException extends Error {
constructor(errorType, message) {
super(message != null ? message : errorType);
this.errorType = errorType;
}
}
/**
* Provides an object that has a method to import login-related data CSV files
*/
export class LoginCSVImport {
/**
* Returns a map that has the csv column name as key and the value the field name.
*
* @returns {Map} A map that has the csv column name as key and the value the field name.
*/
static _getCSVColumnToFieldMap() {
let csvColumnToField = new Map();
for (let [field, columns] of Object.entries(FIELD_TO_CSV_COLUMNS)) {
for (let column of columns) {
csvColumnToField.set(column.toLowerCase(), field);
}
}
return csvColumnToField;
}
/**
* Builds a vanilla JS object containing all the login fields from a row of CSV cells.
*
* @param {object} csvObject
* An object created from a csv row. The keys are the csv column names, the values are the cells.
* @param {Map} csvColumnToFieldMap
* A map where the keys are the csv properties and the values are the object keys.
* @returns {object} Representing login object with only properties, not functions.
*/
static _getVanillaLoginFromCSVObject(csvObject, csvColumnToFieldMap) {
let vanillaLogin = Object.create(null);
for (let columnName of Object.keys(csvObject)) {
let fieldName = csvColumnToFieldMap.get(columnName.toLowerCase());
if (!fieldName) {
continue;
}
if (
typeof vanillaLogin[fieldName] != "undefined" &&
vanillaLogin[fieldName] !== csvObject[columnName]
) {
// Differing column values map to one property.
// e.g. if two headings map to `origin` we won't know which to use.
return {};
}
vanillaLogin[fieldName] = csvObject[columnName];
}
// Since `null` can't be represented in a CSV file and the httpRealm header
// cannot be an empty string, assume that an empty httpRealm means this is
// a form login and therefore null-out httpRealm.
if (vanillaLogin.httpRealm === "") {
vanillaLogin.httpRealm = null;
}
return vanillaLogin;
}
static _recordHistogramTelemetry(histogram, report) {
for (let reportRow of report) {
let { result } = reportRow;
if (result.includes("error")) {
histogram.add("error");
} else {
histogram.add(result);
}
}
}
/**
* Imports logins from a CSV file (comma-separated values file).
* Existing logins may be updated in the process.
*
* @param {string} filePath
* @returns {Object[]} An array of rows where each is mapped to a row in the CSV and it's import information.
*/
static async importFromCSV(filePath) {
TelemetryStopwatch.start("PWMGR_IMPORT_LOGINS_FROM_FILE_MS");
let responsivenessMonitor;
try {
responsivenessMonitor = new lazy.ResponsivenessMonitor();
let csvColumnToFieldMap = LoginCSVImport._getCSVColumnToFieldMap();
let csvFieldToColumnMap = new Map();
let csvString;
try {
csvString = await IOUtils.readUTF8(filePath, { encoding: "utf-8" });
} catch (ex) {
console.error(ex);
throw new ImportFailedException(
ImportFailedErrorType.FILE_PERMISSIONS_ERROR
);
}
let headerLine;
let parsedLines;
try {
let delimiter = filePath.toUpperCase().endsWith(".CSV") ? "," : "\t";
[headerLine, parsedLines] = lazy.CSV.parse(csvString, delimiter);
} catch {
throw new ImportFailedException(
ImportFailedErrorType.FILE_FORMAT_ERROR
);
}
if (parsedLines && headerLine) {
for (const columnName of headerLine) {
const fieldName = csvColumnToFieldMap.get(
columnName.toLocaleLowerCase()
);
if (fieldName) {
if (!csvFieldToColumnMap.has(fieldName)) {
csvFieldToColumnMap.set(fieldName, columnName);
} else {
throw new ImportFailedException(
ImportFailedErrorType.CONFLICTING_VALUES_ERROR
);
}
}
}
}
if (csvFieldToColumnMap.size === 0) {
throw new ImportFailedException(
ImportFailedErrorType.FILE_FORMAT_ERROR
);
}
if (
parsedLines[0] &&
(!csvFieldToColumnMap.has("origin") ||
!csvFieldToColumnMap.has("username") ||
!csvFieldToColumnMap.has("password"))
) {
// The username *value* can be empty but we require a username column to
// ensure that we don't import logins without their usernames due to the
// username column not being recognized.
throw new ImportFailedException(
ImportFailedErrorType.FILE_FORMAT_ERROR
);
}
let loginsToImport = parsedLines.map(csvObject => {
return LoginCSVImport._getVanillaLoginFromCSVObject(
csvObject,
csvColumnToFieldMap
);
});
let report = await lazy.LoginHelper.maybeImportLogins(loginsToImport);
for (const reportRow of report) {
if (reportRow.result === "error_missing_field") {
reportRow.field_name = csvFieldToColumnMap.get(reportRow.field_name);
}
}
// Record quantity, jank, and duration telemetry.
try {
let histogram = Services.telemetry.getHistogramById(
"PWMGR_IMPORT_LOGINS_FROM_FILE_CATEGORICAL"
);
this._recordHistogramTelemetry(histogram, report);
let accumulatedDelay = responsivenessMonitor.finish();
Services.telemetry
.getHistogramById("PWMGR_IMPORT_LOGINS_FROM_FILE_JANK_MS")
.add(accumulatedDelay);
TelemetryStopwatch.finish("PWMGR_IMPORT_LOGINS_FROM_FILE_MS");
} catch (ex) {
console.error(ex);
}
LoginCSVImport.lastImportReport = report;
return report;
} finally {
if (TelemetryStopwatch.running("PWMGR_IMPORT_LOGINS_FROM_FILE_MS")) {
TelemetryStopwatch.cancel("PWMGR_IMPORT_LOGINS_FROM_FILE_MS");
}
responsivenessMonitor.abort();
}
}
}
|