1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00
Files
browser/libs/common/src/importers/safeincloud-xml-importer.ts
Robyn MacCallum 7ebedbecfb [SG-998] and [SG-999] Vault and Autofill team refactor (#4542)
* Move DeprecatedVaultFilterService to vault folder

* [libs] move VaultItemsComponent

* [libs] move AddEditComponent

* [libs] move AddEditCustomFields

* [libs] move attachmentsComponent

* [libs] folderAddEditComponent

* [libs] IconComponent

* [libs] PasswordRepormptComponent

* [libs] PremiumComponent

* [libs] ViewCustomFieldsComponent

* [libs] ViewComponent

* [libs] PasswordRepromptService

* [libs] Move FolderService and FolderApiService abstractions

* [libs] FolderService imports

* [libs] PasswordHistoryComponent

* [libs] move Sync and SyncNotifier abstractions

* [libs] SyncService imports

* [libs] fix file casing for passwordReprompt abstraction

* [libs] SyncNotifier import fix

* [libs] CipherServiceAbstraction

* [libs] PasswordRepromptService abstraction

* [libs] Fix file casing for angular passwordReprompt service

* [libs] fix file casing for SyncNotifierService

* [libs] CipherRepromptType

* [libs] rename CipherRepromptType

* [libs] CipherType

* [libs] Rename CipherType

* [libs] CipherData

* [libs] FolderData

* [libs] PasswordHistoryData

* [libs] AttachmentData

* [libs] CardData

* [libs] FieldData

* [libs] IdentityData

* [libs] LocalData

* [libs] LoginData

* [libs] SecureNoteData

* [libs] LoginUriData

* [libs] Domain classes

* [libs] SecureNote

* [libs] Request models

* [libs] Response models

* [libs] View part 1

* [libs] Views part 2

* [libs] Move folder services

* [libs] Views fixes

* [libs] Move sync services

* [libs] cipher service

* [libs] Types

* [libs] Sync file casing

* [libs] Fix folder service import

* [libs] Move spec files

* [libs] casing fixes on spec files

* [browser] Autofill background, clipboard, commands

* [browser] Fix ContextMenusBackground casing

* [browser] Rename fix

* [browser] Autofill content

* [browser] autofill.js

* [libs] enpass importer spec fix

* [browser] autofill models

* [browser] autofill manifest path updates

* [browser] Autofill notification files

* [browser] autofill services

* [browser] Fix file casing

* [browser] Vault popup loose components

* [browser] Vault components

* [browser] Manifest fixes

* [browser] Vault services

* [cli] vault commands and models

* [browser] File capitilization fixes

* [desktop] Vault components and services

* [web] vault loose components

* [web] Vault components

* [browser] Fix misc-utils import

* [libs] Fix psono spec imports

* [fix] Add comments to address lint rules
2023-01-31 16:08:37 -05:00

133 lines
4.8 KiB
TypeScript

import { FieldType } from "../enums/fieldType";
import { SecureNoteType } from "../enums/secureNoteType";
import { ImportResult } from "../models/domain/import-result";
import { CipherType } from "../vault/enums/cipher-type";
import { CipherView } from "../vault/models/view/cipher.view";
import { FieldView } from "../vault/models/view/field.view";
import { FolderView } from "../vault/models/view/folder.view";
import { SecureNoteView } from "../vault/models/view/secure-note.view";
import { BaseImporter } from "./base-importer";
import { Importer } from "./importer";
export class SafeInCloudXmlImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const doc = this.parseXml(data);
if (doc == null) {
result.success = false;
return Promise.resolve(result);
}
const db = doc.querySelector("database");
if (db == null) {
result.errorMessage = "Missing `database` node.";
result.success = false;
return Promise.resolve(result);
}
const foldersMap = new Map<string, number>();
Array.from(doc.querySelectorAll("database > label")).forEach((labelEl) => {
const name = labelEl.getAttribute("name");
const id = labelEl.getAttribute("id");
if (!this.isNullOrWhitespace(name) && !this.isNullOrWhitespace(id)) {
foldersMap.set(id, result.folders.length);
const folder = new FolderView();
folder.name = name;
result.folders.push(folder);
}
});
Array.from(doc.querySelectorAll("database > card")).forEach((cardEl) => {
if (cardEl.getAttribute("template") === "true" || cardEl.getAttribute("deleted") === "true") {
return;
}
const labelIdEl = this.querySelectorDirectChild(cardEl, "label_id");
if (labelIdEl != null) {
const labelId = labelIdEl.textContent;
if (!this.isNullOrWhitespace(labelId) && foldersMap.has(labelId)) {
result.folderRelationships.push([result.ciphers.length, foldersMap.get(labelId)]);
}
}
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(cardEl.getAttribute("title"), "--");
if (cardEl.getAttribute("star") === "true") {
cipher.favorite = true;
}
const cardType = cardEl.getAttribute("type");
if (cardType === "note") {
cipher.type = CipherType.SecureNote;
cipher.secureNote = new SecureNoteView();
cipher.secureNote.type = SecureNoteType.Generic;
} else {
Array.from(this.querySelectorAllDirectChild(cardEl, "field")).forEach((fieldEl) => {
const text = fieldEl.textContent;
if (this.isNullOrWhitespace(text)) {
return;
}
const name = fieldEl.getAttribute("name");
const fieldType = this.getValueOrDefault(fieldEl.getAttribute("type"), "").toLowerCase();
if (fieldType === "login") {
cipher.login.username = text;
} else if (fieldType === "password" || fieldType === "secret") {
// safeInCloud allows for more than one password. we just insert them here and find the one used as password later
this.processKvp(cipher, name, text, FieldType.Hidden);
} else if (fieldType === "one_time_password") {
cipher.login.totp = text;
} else if (fieldType === "notes") {
cipher.notes += text + "\n";
} else if (fieldType === "weblogin" || fieldType === "website") {
cipher.login.uris = this.makeUriArray(text);
} else {
this.processKvp(cipher, name, text);
}
});
}
Array.from(this.querySelectorAllDirectChild(cardEl, "notes")).forEach((notesEl) => {
cipher.notes += notesEl.textContent + "\n";
});
this.setPassword(cipher);
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
});
if (this.organization) {
this.moveFoldersToCollections(result);
}
result.success = true;
return Promise.resolve(result);
}
// Choose a password from all passwords. Take one that has password in its name, or the first one if there is no such entry
// if its name is password, we can safely remove it form the fields. otherwise, it would maybe be best to keep it as a hidden field
setPassword(cipher: CipherView) {
const candidates = cipher.fields.filter((field) => field.type === FieldType.Hidden);
if (!candidates.length) {
return;
}
let choice: FieldView;
for (const field of candidates) {
if (this.passwordFieldNames.includes(field.name.toLowerCase())) {
choice = field;
cipher.fields = cipher.fields.filter((f) => f !== choice);
break;
}
}
if (!choice) {
choice = candidates[0];
}
cipher.login.password = choice.value;
}
}