Skip to content

A local-first personal finance app

Fix bug in ofx2json logic (#4838)

Julian Dominguez-Schatzf1594e5bcaf7
diff --git a/packages/loot-core/src/server/transactions/import/__snapshots__/parse-file.test.ts.snap b/packages/loot-core/src/server/transactions/import/__snapshots__/parse-file.test.ts.snap
index 1fbe231b13..b2a9d70f6c 100644
--- a/packages/loot-core/src/server/transactions/import/__snapshots__/parse-file.test.ts.snap
+++ b/packages/loot-core/src/server/transactions/import/__snapshots__/parse-file.test.ts.snap
@@ -232,7 +232,7 @@ exports[`File import handles html escaped plaintext 1`] = `
"error": null,
"financial_id": "23060061032549554596530000",
"id": "id5",
- "imported_description": "TPAY <DEFTPYMT>",
+ "imported_description": "TPAY &lt;DEFTPYMT&gt;",
"isChild": 0,
"isParent": 0,
"location": null,
diff --git a/packages/loot-core/src/server/transactions/import/ofx2json.test.ts b/packages/loot-core/src/server/transactions/import/ofx2json.test.ts
new file mode 100644
index 0000000000..87c842a922
--- /dev/null
+++ b/packages/loot-core/src/server/transactions/import/ofx2json.test.ts
@@ -0,0 +1,33 @@
+import { html2Plain } from './ofx2json';
+
+describe('html2Plain', () => {
+ test('regular text works', async () => {
+ expect(html2Plain('Hello, world!')).toBe('Hello, world!');
+ expect(html2Plain('Hello, <b>world</b>!')).toBe('Hello, <b>world</b>!');
+ });
+
+ test('brackets are unescaped', async () => {
+ expect(html2Plain('Hello, &lt;world&gt;!')).toBe('Hello, <world>!');
+ });
+ test('apostrophes are unescaped', async () => {
+ expect(html2Plain('Hello, &#39;world&#39;!')).toBe("Hello, 'world'!");
+ });
+ test('quotes are unescaped', async () => {
+ expect(html2Plain('Hello, &quot;world&quot;!')).toBe('Hello, "world"!');
+ });
+ test('ampersands are unescaped', async () => {
+ expect(html2Plain('Hello, &amp;world&amp;!')).toBe('Hello, &world&!');
+ expect(html2Plain('Hello, &#038;world&#038;!')).toBe('Hello, &world&!');
+ });
+ test('no double unescaping with other entities', async () => {
+ expect(html2Plain('Hello, &amp;#038;world&amp;#038;!')).toBe(
+ 'Hello, &#038;world&#038;!',
+ );
+ expect(html2Plain('Hello, &#038;amp;world&#038;amp;!')).toBe(
+ 'Hello, &amp;world&amp;!',
+ );
+ expect(html2Plain('Hello, &amp;quot;world&amp;quot;!')).toBe(
+ 'Hello, &quot;world&quot;!',
+ );
+ });
+});
diff --git a/packages/loot-core/src/server/transactions/import/ofx2json.ts b/packages/loot-core/src/server/transactions/import/ofx2json.ts
index 91fc8abeb5..20a888ab4d 100644
--- a/packages/loot-core/src/server/transactions/import/ofx2json.ts
+++ b/packages/loot-core/src/server/transactions/import/ofx2json.ts
@@ -29,14 +29,13 @@ function sgml2Xml(sgml) {
.replace(/<\/<added>(\w+?)>(<\/\1>)?/g, '</$1>'); // Remove duplicate end-tags
}
-function html2Plain(value) {
+export function html2Plain(value) {
return value
- ?.replace(/&amp;/g, '&') // ampersands
- .replace(/&#038;/g, '&') // other ampersands
- .replace(/&lt;/g, '<') // lessthan
+ ?.replace(/&lt;/g, '<') // lessthan
.replace(/&gt;/g, '>') // greaterthan
.replace(/&#39;/g, "'") // eslint-disable-line rulesdir/typography
- .replace(/&quot;/g, '"'); // eslint-disable-line rulesdir/typography
+ .replace(/&quot;/g, '"') // eslint-disable-line rulesdir/typography
+ .replace(/(&amp;|&#038;)/g, '&'); // ampersands
}
async function parseXml(content) {
diff --git a/upcoming-release-notes/4838.md b/upcoming-release-notes/4838.md
new file mode 100644
index 0000000000..b13782d8e3
--- /dev/null
+++ b/upcoming-release-notes/4838.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [jfdoming]
+---
+
+Fix HTML bug in ofx2json logic