Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-11624] Fixed SecureSafe csv import fail #10875

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion libs/importer/spec/securesafe-csv-importer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { LoginView } from "@bitwarden/common/vault/models/view/login.view";

import { SecureSafeCsvImporter } from "../src/importers";

import { data_upperUrl, data_lowerUrl } from "./test-data/securesafe-csv/securesafe-example.csv";
import {
data_upperUrl,
data_lowerUrl,
data_surrounding_slashes,
} from "./test-data/securesafe-csv/securesafe-example.csv";

const CipherData = [
{
Expand Down Expand Up @@ -49,6 +53,27 @@ const CipherData = [
type: 1,
}),
},
{
title: "should change headers to english and remove surrounding slashes if present",
csv: data_surrounding_slashes,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "Gmail",
login: Object.assign(new LoginView(), {
username: "[email protected]",
password: "/test",
uris: [
Object.assign(new LoginUriView(), {
uri: "https://gmail.com",
}),
],
}),
notes: "comment/",
type: 1,
}),
},
];

describe("SecureSafe CSV Importer", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ export const data_upperUrl = `"Title","Username","Password","URL","Comment"

export const data_lowerUrl = `"Title","Username","Password","url","Comment"
"Gmail","[email protected]","test","https://gmail.com"`;

export const data_surrounding_slashes = `"/Titel/","/Benutzername/","/Passwort/","/URL/","/Kommentar/",
"/Gmail/","/[email protected]/","/test","/https://gmail.com/","comment/"`;
23 changes: 21 additions & 2 deletions libs/importer/src/importers/securesafe-csv-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,28 @@ export class SecureSafeCsvImporter extends BaseImporter implements Importer {
return Promise.resolve(result);
}

// SecureSafe currently exports values in multiple languages. - 09/05/2024
// New headers are used to ensure import success.
const newHeaders = ["Title", "Username", "Password", "URL", "Comment"];

// SecureSafe can surround values in slashes. - 09/05/2024
// This removes any surrounding slashes from the values.
const headers = Object.keys(results[0]);
const remappedResults = results.map((row) => {
const remappedRow: any = {};
newHeaders.forEach((header, index) => {
let value = row[headers[index]];
if (typeof value === "string" && value.startsWith("/") && value.endsWith("/")) {
value = value.slice(1, -1);
}
remappedRow[header] = value;
});
return remappedRow;
});
Comment on lines +15 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the headers always come in the same order, can we simply refer to them by index instead of by name? I would imagine it'd be more reliable than trying to translate the source language


// The url field can be in different case formats.
const urlField = Object.keys(results[0]).find((k) => /url/i.test(k));
results.forEach((value) => {
const urlField = Object.keys(remappedResults[0]).find((k) => /url/i.test(k));
remappedResults.forEach((value) => {
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(value.Title);
cipher.notes = this.getValueOrDefault(value.Comment);
Expand Down
Loading