Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Sheet9// File : Packages/SheetIO/Tests/SheetIOTests/HardeningTests.swift10// Module : SheetIOTests11// Repository : https://git.spboucher.ai/orix-sheet12// Created : 2026-11-2113// Last Modified : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89import SheetCore90@testable import SheetIO9192/// Hostile numeric values must never trap: every arithmetic on untrusted input is range-checked.93@Suite struct HardeningTests {94 @Test func zip64FieldsNearIntMaxDoNotOverflow() {95 for huge in [UInt64(Int.max), UInt64(Int.max) - 10, UInt64.max, 1 << 62] {96 // central directory entry whose ZIP64 extra field carries absurd sizes and offsets97 var d = Data()98 d.le32(0x0403_4b50); d.le16(45); d.le16(0x0800); d.le16(0); d.le16(0); d.le16(0x21); d.le32(0); d.le32(0); d.le32(0); d.le16(1); d.le16(0)99 d.append(0x61)100 let cd = d.count101 d.le32(0x0201_4b50); d.le16(45); d.le16(45); d.le16(0x0800); d.le16(0); d.le16(0); d.le16(0x21); d.le32(0)102 d.le32(0xFFFF_FFFF); d.le32(0xFFFF_FFFF); d.le16(1); d.le16(28); d.le16(0); d.le16(0); d.le16(0); d.le32(0); d.le32(0xFFFF_FFFF)103 d.append(0x61)104 d.le16(1); d.le16(24); d.le64(huge); d.le64(huge); d.le64(huge)105 let cdSize = d.count - cd106 let z = d.count107 d.le32(0x0606_4b50); d.le64(44); d.le16(45); d.le16(45); d.le32(0); d.le32(0); d.le64(1); d.le64(1); d.le64(UInt64(cdSize)); d.le64(UInt64(cd))108 d.le32(0x0706_4b50); d.le32(0); d.le64(UInt64(z)); d.le32(1)109 d.le32(0x0605_4b50); d.le16(0); d.le16(0); d.le16(0xFFFF); d.le16(0xFFFF); d.le32(0xFFFF_FFFF); d.le32(0xFFFF_FFFF); d.le16(0)110 var limits = ZipLimits()111 limits.maxEntrySize = .max; limits.maxTotalSize = .max; limits.maxCompressionRatio = .infinity; limits.maxEntries = .max112 for l in [ZipLimits(), limits] { #expect(throws: SheetIOError.self) { _ = try ZipArchive(data: d, limits: l) } }113 // ZIP64 end record pointing far outside the file, and an absurd entry count114 var e = Data(count: 64)115 e.le32(0x0706_4b50); e.le32(0); e.le64(huge); e.le32(1)116 e.le32(0x0605_4b50); e.le16(0); e.le16(0); e.le16(0xFFFF); e.le16(0xFFFF); e.le32(0xFFFF_FFFF); e.le32(0xFFFF_FFFF); e.le16(0)117 #expect(throws: SheetIOError.self) { _ = try ZipArchive(data: e, limits: limits) }118 }119 }120121 @Test func nativeCellChunksWithInvalidIndexes() throws {122 // a chunk declaring one text cell but no string table, and one with an out-of-grid cell123 func part(_ raw: [UInt8], cells: UInt32) throws -> Data {124 let rawData = Data(raw)125 let comp = try Deflate.compress(rawData, raw: false)126 var d = Data(NativeCells.magic)127 d.le32(1); d.le64(UInt64(cells)); d.le32(1)128 d.le32(UInt32(raw.count)); d.le32(UInt32(comp.count)); d.le32(cells); d.le32(Deflate.crc32(rawData))129 d.append(comp)130 return d131 }132 for raw in [[0, 0, 0, 3, 0] as [UInt8], [0, 0, 0, 0x13, 0, 0, 0], [0, 0xFF, 0xFF, 0xFF, 0x7F, 0, 0], [0, 0, 0, 7, 0xFF, 0xFF, 0x3F, 0xFF, 0x7F], [0xFF, 0xFF, 0xFF, 0xFF, 0x0F]] {133 let d = try part(raw, cells: 1)134 let lenient = try NativeCells.decode(d, strict: false, maxCells: 1000)135 #expect(lenient.store.count == 0 && lenient.badChunks.count == 1 && lenient.lostCells == 1)136 #expect(throws: SheetIOError.self) { _ = try NativeCells.decode(d, strict: true, maxCells: 1000) }137 }138 // a header announcing billions of cells in a tiny file139 let lying = try part([0], cells: 0xFFFF_FFF0)140 #expect(throws: SheetIOError.self) { _ = try NativeCells.decode(lying, strict: false, maxCells: 1000) }141 var o = LoadOptions(); o.maxCells = 100142 let big = Workbook(sheetNames: ["s"])143 for r in 0..<500 { big.sheets[0].cells.set(row: r, col: 0, Cell(value: .number(1))) }144 let dir = Scratch.dir("harden")145 defer { try? FileManager.default.removeItem(at: dir) }146 let url = dir.appendingPathComponent("big.orixsheet")147 try NativeFormat.write(big, to: url)148 #expect(throws: SheetIOError.self) { _ = try NativeFormat.read(url: url, options: o) }149 }150151 @Test func extremeValuesInWritersAndReaders() throws {152 let dir = Scratch.dir("extreme")153 defer { try? FileManager.default.removeItem(at: dir) }154 let wb = Workbook(sheetNames: ["X"])155 let s = wb.sheets[0]156 var date = CellStyle(); date.numberFormat = "yyyy-mm-dd hh:mm:ss"157 let dateID = wb.styles.intern(date)158 var time = CellStyle(); time.numberFormat = "hh:mm:ss"159 let timeID = wb.styles.intern(time)160 for (r, v) in [1e300, -1e300, -5, 2_958_466, 1e18, Double.greatestFiniteMagnitude, 5e-324, 45000.5].enumerated() {161 s.cells.set(row: r, col: 0, Cell(value: .number(v), style: dateID))162 s.cells.set(row: r, col: 1, Cell(value: .number(v), style: timeID))163 }164 s.cells.set(row: 20, col: 0, Cell(value: .number(.infinity))); s.cells.set(row: 21, col: 0, Cell(value: .number(.nan)))165 s.meta.zoom = .nan; s.meta.colWidths[0] = .infinity; s.meta.rowHeights[0] = .nan; s.meta.defaultColWidth = .nan; s.meta.print.marginTop = .infinity166 s.meta.frozenRows = -4167 for format in [FileFormat.xlsx, .ods, .csv, .tsv, .json, .jsonl, .orixsheet] {168 let url = dir.appendingPathComponent("x.\(format.rawValue)")169 try WorkbookWriter.save(wb, to: url, format: format)170 let back = try WorkbookReader.load(url: url).workbook171 guard format != .json, format != .jsonl else { continue } // header row and ISO date strings: only "does not trap" matters172 #expect(back.sheets[0].value(7, 0).numberValue.map { abs($0 - 45000.5) < 1e-6 } == true, "\(format)")173 #expect(back.sheets[0].value(0, 0) == .number(1e300), "\(format)")174 }175 if Tools.hasPython { _ = try Tools.run(Tools.python, ["-I", "-c", "import openpyxl,sys; openpyxl.load_workbook(sys.argv[1])", dir.appendingPathComponent("x.xlsx").path]) }176 // hostile attribute values in a worksheet177 let sheet = "<worksheet xmlns=\"\(OPC.mainNS)\"><sheetViews><sheetView zoomScale=\"99999999999999999999\" workbookViewId=\"0\"><pane xSplit=\"1e300\" ySplit=\"-1e300\" state=\"frozen\"/></sheetView></sheetViews><sheetFormatPr defaultRowHeight=\"1e999\" defaultColWidth=\"nan\"/><cols><col min=\"1\" max=\"999999999999\" width=\"1e308\" customWidth=\"1\"/><col min=\"-5\" max=\"3\" width=\"inf\"/></cols><sheetData><row r=\"1\" ht=\"1e400\" outlineLevel=\"99999999999\"><c r=\"A1\"><v>nan</v></c><c r=\"B1\"><v>1e999</v></c><c r=\"C1\"><v>inf</v></c><c r=\"D1\"><v>7</v></c></row></sheetData><rowBreaks><brk id=\"99999999999999\"/></rowBreaks><pageSetup scale=\"-7\" fitToWidth=\"99999999\"/></worksheet>"178 let hostile = dir.appendingPathComponent("hostile.xlsx")179 try rebuildZip(Scratch.fixture("macro.xlsm"), replacing: ["xl/worksheets/sheet1.xml": Data(sheet.utf8)]).write(to: hostile)180 let h = try WorkbookReader.load(url: hostile).workbook181 #expect(cellValue(h, 0, "D1") == .number(7) && h.sheets[0].meta.frozenCols == Limits.maxCols - 1 && h.sheets[0].meta.frozenRows == 0)182 #expect(cellValue(h, 0, "A1") == .text("nan") && cellValue(h, 0, "B1") == .text("1e999"))183 try WorkbookWriter.save(h, to: dir.appendingPathComponent("hostile-out.xlsx"), format: .xlsx)184 // hostile repeat counts in ODS185 let content = "<?xml version=\"1.0\"?><office:document-content xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"><office:body><office:spreadsheet><table:table table:name=\"S\"><table:table-column table:number-columns-repeated=\"9223372036854775807\"/><table:table-column table:number-columns-repeated=\"9223372036854775807\"/><table:table-row table:number-rows-repeated=\"9223372036854775807\"><table:table-cell table:number-columns-repeated=\"9223372036854775807\"/></table:table-row><table:table-row table:number-rows-repeated=\"9223372036854775807\"><table:table-cell office:value-type=\"float\" office:value=\"inf\" table:number-columns-spanned=\"9223372036854775807\" table:number-rows-spanned=\"9223372036854775807\" table:number-matrix-rows-spanned=\"9223372036854775807\" table:formula=\"of:=1\"><text:p>x<text:s text:c=\"99999999999\"/></text:p></table:table-cell></table:table-row></table:table></office:body></office:document-content>"186 let ods = dir.appendingPathComponent("hostile.ods")187 try rebuildZip(Scratch.fixture("features_lo.ods"), replacing: ["content.xml": Data(content.utf8)]).write(to: ods)188 _ = try? WorkbookReader.load(url: ods) // must return or throw, never trap189 }190}191