Просмотр исходного кода

Security hardening (maziggy/bambuddy-security #22)

- Spreadsheet exports keep typed text as text.
maziggy 3 дней назад
Родитель
Сommit
f4a544d7fe

+ 0 - 16
.basedpyright/baseline.json

@@ -2195,14 +2195,6 @@
                     "lineCount": 1
                 }
             },
-            {
-                "code": "reportOptionalMemberAccess",
-                "range": {
-                    "startColumn": 19,
-                    "endColumn": 23,
-                    "lineCount": 1
-                }
-            },
             {
                 "code": "reportOptionalMemberAccess",
                 "range": {
@@ -2226,14 +2218,6 @@
                     "endColumn": 16,
                     "lineCount": 1
                 }
-            },
-            {
-                "code": "reportOptionalMemberAccess",
-                "range": {
-                    "startColumn": 26,
-                    "endColumn": 30,
-                    "lineCount": 1
-                }
             }
         ],
         "./backend/app/services/external_camera.py": [

+ 31 - 4
backend/app/services/export.py

@@ -11,6 +11,33 @@ from backend.app.core.printer_scope import PrinterScope
 from backend.app.models.archive import PrintArchive
 from backend.app.models.print_log import INCLUDE_OPEN_RUNS, PrintLogEntry
 
+# A cell starting with one of these is read as a formula by spreadsheet apps,
+# including the full-width forms (= + - @) some normalise to ASCII.
+_FORMULA_PREFIXES = ("=", "+", "-", "@", "=", "+", "-", "@")
+
+
+def _spreadsheet_text(value: Any) -> Any:
+    """A CSV cell as typed: text that starts like a formula gets a leading quote, so it stays text.
+
+    Leading whitespace is looked past (spreadsheets trim it on import), and a
+    value starting with a tab or line break is quoted too. Anything that isn't
+    text is left alone.
+    """
+    if not isinstance(value, str) or not value:
+        return value
+    if value[0] in "\t\r\n" or value.lstrip().startswith(_FORMULA_PREFIXES):
+        return f"'{value}"
+    return value
+
+
+def _xlsx_cell(ws: Any, row: int, column: int, value: Any) -> Any:
+    """Write a worksheet cell; text that starts with "=" is stored as text, not as a formula."""
+    cell = ws.cell(row=row, column=column, value=value)
+    if isinstance(value, str) and cell.data_type == "f":
+        cell.data_type = "s"
+    return cell
+
+
 # What one print of an archive has of its own (archive redesign): in the
 # export, its run's. Everything else is the file's, the archive's.
 _PER_PRINT_FIELDS = frozenset(
@@ -368,14 +395,14 @@ class ExportService:
         output = io.StringIO()
         writer = csv.writer(output)
         writer.writerow(headers)
-        writer.writerows(rows)
+        writer.writerows([[_spreadsheet_text(value) for value in row] for row in rows])
         return output.getvalue().encode("utf-8")
 
     def _generate_csv_simple(self, rows: list[list]) -> bytes:
         """Generate CSV file content from simple rows (no separate headers)."""
         output = io.StringIO()
         writer = csv.writer(output)
-        writer.writerows(rows)
+        writer.writerows([[_spreadsheet_text(value) for value in row] for row in rows])
         return output.getvalue().encode("utf-8")
 
     def _generate_xlsx(self, headers: list[str], rows: list[list], fields: list[str]) -> bytes:
@@ -406,7 +433,7 @@ class ExportService:
         # Write data
         for row_idx, row in enumerate(rows, 2):
             for col_idx, value in enumerate(row, 1):
-                ws.cell(row=row_idx, column=col_idx, value=value)
+                _xlsx_cell(ws, row_idx, col_idx, value)
 
         # Auto-adjust column widths
         for col_idx, _field in enumerate(fields, 1):
@@ -441,7 +468,7 @@ class ExportService:
 
         for row_idx, row in enumerate(rows, 1):
             for col_idx, value in enumerate(row, 1):
-                cell = ws.cell(row=row_idx, column=col_idx, value=value)
+                cell = _xlsx_cell(ws, row_idx, col_idx, value)
                 # Bold section headers
                 if col_idx == 1 and value and isinstance(value, str) and value.endswith(":"):
                     cell.font = bold_font

+ 82 - 0
backend/tests/unit/test_export_spreadsheet_cells.py

@@ -0,0 +1,82 @@
+"""Archive and statistics exports show typed text as it was typed in a spreadsheet.
+
+Print names, notes, tags, project names and failure reasons are typed by
+people, and a spreadsheet reads a cell that starts like a formula as one. In
+CSV such a cell gets a leading quote; in Excel it is stored as text. Numbers
+and empty cells are written as they are.
+"""
+
+from __future__ import annotations
+
+import csv
+import io
+
+import pytest
+from openpyxl import load_workbook
+
+from backend.app.services.export import ExportService, _spreadsheet_text
+
+LINK = '=HYPERLINK("https://elsewhere.example","open")'
+
+
+@pytest.mark.parametrize(
+    ("value", "expected"),
+    [
+        ("Benchy", "Benchy"),
+        ("a = b", "a = b"),
+        (LINK, "'" + LINK),
+        ("+1 extra", "'+1 extra"),
+        ("-x", "'-x"),
+        ("@SUM(A1)", "'@SUM(A1)"),
+        ("  =1+1", "'  =1+1"),
+        ("\t=1+1", "'\t=1+1"),
+        ("\nnote", "'\nnote"),
+        ("=1+1", "'=1+1"),
+        ("", ""),
+        (None, None),
+        (-5, -5),
+        (2.5, 2.5),
+    ],
+)
+def test_cell_values(value, expected):
+    assert _spreadsheet_text(value) == expected
+
+
+def _service() -> ExportService:
+    return ExportService(db=None)  # type: ignore[arg-type]
+
+
+def test_archive_csv_keeps_typed_text_as_text():
+    data = _service()._generate_csv(["print_name", "notes", "cost"], [["Benchy", LINK, -1.5]])
+
+    rows = list(csv.reader(io.StringIO(data.decode("utf-8"))))
+
+    assert rows[1] == ["Benchy", "'" + LINK, "-1.5"]
+
+
+def test_stats_csv_keeps_typed_text_as_text():
+    data = _service()._generate_csv_simple([["Failures by Reason", ""], ["+spaghetti", 3]])
+
+    rows = list(csv.reader(io.StringIO(data.decode("utf-8"))))
+
+    assert rows == [["Failures by Reason", ""], ["'+spaghetti", "3"]]
+
+
+def test_archive_xlsx_stores_typed_text_as_text():
+    data = _service()._generate_xlsx(["print_name", "notes", "cost"], [["Benchy", LINK, -1.5]], ["a", "b", "c"])
+
+    sheet = load_workbook(io.BytesIO(data)).active
+    assert sheet is not None
+
+    assert (sheet["B2"].value, sheet["B2"].data_type) == (LINK, "s")
+    assert sheet["C2"].value == -1.5
+
+
+def test_stats_xlsx_stores_typed_text_as_text():
+    data = _service()._generate_xlsx_simple([["Failures by Reason", ""], [LINK, 3]])
+
+    sheet = load_workbook(io.BytesIO(data)).active
+    assert sheet is not None
+
+    assert (sheet["A2"].value, sheet["A2"].data_type) == (LINK, "s")
+    assert sheet["B2"].value == 3

+ 62 - 0
frontend/src/__tests__/components/ForecastPanelShoppingListCsv.test.tsx

@@ -0,0 +1,62 @@
+/**
+ * The shopping list CSV shows typed text (here a note) as it was typed when
+ * opened in a spreadsheet: a note that starts like a formula gets a leading
+ * quote.
+ */
+
+import { describe, it, expect, afterEach, vi } from 'vitest';
+import { screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { http, HttpResponse } from 'msw';
+import { render } from '../utils';
+import { server } from '../mocks/server';
+import { ForecastPanel } from '../../components/ForecastPanel';
+
+const NOTE = '=HYPERLINK("https://elsewhere.example","open")';
+
+afterEach(() => {
+  server.resetHandlers();
+  vi.restoreAllMocks();
+});
+
+describe('ForecastPanel shopping list CSV', () => {
+  it('writes a note that starts like a formula as text', async () => {
+    server.use(
+      http.get('*/api/v1/settings/', () => HttpResponse.json({ forecast_global_lead_time_days: 7 })),
+      http.get('*/api/v1/inventory/sku-settings', () => HttpResponse.json([])),
+      http.get(/\/api\/v1\/inventory\/usage/, () => HttpResponse.json([])),
+      http.get('*/api/v1/inventory/shopping-list', () =>
+        HttpResponse.json([
+          {
+            id: 1, material: 'PLA', subtype: null, brand: 'Polymaker', color_name: 'Red',
+            quantity_spools: 2, status: 'pending', note: NOTE, added_at: '2025-01-01T00:00:00Z',
+          },
+        ]),
+      ),
+    );
+    const blobs: Blob[] = [];
+    vi.spyOn(URL, 'createObjectURL').mockImplementation((blob) => {
+      blobs.push(blob as Blob);
+      return 'blob:shopping-list';
+    });
+    vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined);
+    vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => undefined);
+    const user = userEvent.setup();
+
+    render(<ForecastPanel spools={[]} />);
+    // The list's badge shows once it has loaded; then open it.
+    await screen.findByText('1');
+    await user.click(screen.getByRole('button', { name: /shopping list/i }));
+    await user.click(await screen.findByRole('button', { name: 'CSV' }));
+
+    await waitFor(() => expect(blobs).toHaveLength(1));
+    const text = await new Promise<string>((resolve) => {
+      const reader = new FileReader();
+      reader.onload = () => resolve(String(reader.result));
+      reader.readAsText(blobs[0]);
+    });
+    const [, row] = text.split('\n');
+    expect(row).toContain('"Polymaker"');
+    expect(row.endsWith(`"'=HYPERLINK(""https://elsewhere.example"",""open"")"`)).toBe(true);
+  });
+});

+ 32 - 0
frontend/src/__tests__/utils/csvCell.test.ts

@@ -0,0 +1,32 @@
+import { describe, it, expect } from 'vitest';
+import { csvCell } from '../../utils/csvCell';
+
+// The shopping list CSV shows typed text (brand, colour, note) as it was
+// typed when opened in a spreadsheet.
+describe('csvCell', () => {
+  it.each([
+    ['Bambu Lab', '"Bambu Lab"'],
+    ['a = b', '"a = b"'],
+    ['=HYPERLINK("https://elsewhere.example","open")', '"\'=HYPERLINK(""https://elsewhere.example"",""open"")"'],
+    ['+1 extra', '"\'+1 extra"'],
+    ['-x', '"\'-x"'],
+    ['@SUM(A1)', '"\'@SUM(A1)"'],
+    ['  =1+1', '"\'  =1+1"'],
+    ['\t=1+1', '"\'\t=1+1"'],
+    ['\nsecond line', '"\'\nsecond line"'],
+    ['=1+1', '"\'=1+1"'],
+    ['', '""'],
+  ])('writes %j as %s', (value, expected) => {
+    expect(csvCell(value)).toBe(expected);
+  });
+
+  it('writes numbers as they are, negative ones too', () => {
+    expect(csvCell(-5)).toBe('"-5"');
+    expect(csvCell(1200)).toBe('"1200"');
+  });
+
+  it('writes null and undefined as empty cells', () => {
+    expect(csvCell(null)).toBe('""');
+    expect(csvCell(undefined)).toBe('""');
+  });
+});

+ 2 - 1
frontend/src/components/ForecastPanel.tsx

@@ -19,6 +19,7 @@ import { useAuth } from '../contexts/AuthContext';
 import { useUiSettings } from '../hooks/useUiSettings';
 import { ConfirmModal } from './ConfirmModal';
 import { parseUTCDate } from '../utils/date';
+import { csvCell } from '../utils/csvCell';
 import { ownRecords, timeOf } from '../utils/usageHistory';
 
 // ── Types ─────────────────────────────────────────────────────────────────────
@@ -1454,7 +1455,7 @@ function ShoppingListPanel({
         restock,
         i.status,
         i.note ?? '',
-      ].map((v) => `"${String(v).replace(/"/g, '""')}"`).join(',');
+      ].map((v) => csvCell(v)).join(',');
     });
     const csv = [headers.join(','), ...rows].join('\n');
     const blob = new Blob([csv], { type: 'text/csv' });

+ 18 - 0
frontend/src/utils/csvCell.ts

@@ -0,0 +1,18 @@
+// A cell starting with one of these is read as a formula by spreadsheet apps,
+// including the full-width forms some normalise to ASCII.
+const FORMULA_PREFIXES = ['=', '+', '-', '@', '=', '+', '-', '@'];
+
+/**
+ * One CSV cell, quoted. Text that starts like a formula gets a leading quote
+ * so a spreadsheet shows it as typed; leading whitespace is looked past, and
+ * a value starting with a tab or line break is quoted too. Numbers are
+ * written as they are.
+ */
+export function csvCell(value: unknown): string {
+  let text = String(value ?? '');
+  if (typeof value === 'string' && text !== '') {
+    const startsLikeFormula = FORMULA_PREFIXES.some((p) => text.trimStart().startsWith(p));
+    if ('\t\r\n'.includes(text[0]) || startsLikeFormula) text = `'${text}`;
+  }
+  return `"${text.replace(/"/g, '""')}"`;
+}