From 9b2accb37038b92440b8bbd785aadd09a91d2f7b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 1 May 2025 17:50:02 +0300 Subject: [PATCH 1/3] chore(sign): log architecture of sign tool --- apps/desktop/electron-forge/sign-windows.cjs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/desktop/electron-forge/sign-windows.cjs b/apps/desktop/electron-forge/sign-windows.cjs index badb89a94..c59f3ca3f 100644 --- a/apps/desktop/electron-forge/sign-windows.cjs +++ b/apps/desktop/electron-forge/sign-windows.cjs @@ -1,12 +1,11 @@ const child_process = require("child_process"); -const { default: e } = require("express"); const fs = require("fs"); const path = require("path"); const LOG_LOCATION = "c:\\ev_signer_trilium\\ev_signer_trilium.err.log"; +const { WINDOWS_SIGN_EXECUTABLE } = process.env; module.exports = function (sourcePath) { - const { WINDOWS_SIGN_EXECUTABLE } = process.env; if (!WINDOWS_SIGN_EXECUTABLE) { console.warn("[Sign] Skip signing due to missing environment variable."); return; @@ -32,10 +31,10 @@ module.exports = function (sourcePath) { } function printSigningErrorLogs(sourcePath) { - const buffer = fs.readFileSync(sourcePath); console.log("Platform: ", process.platform); console.log("CPU archi:", process.arch); - console.log("DLL archi: ", getDllArchitectureFromBuffer(buffer)); + console.log("DLL archi: ", getDllArchitectureFromFile(sourcePath)); + console.log("Signer archi: ", getDllArchitectureFromFile(WINDOWS_SIGN_EXECUTABLE)); if (!fs.existsSync(LOG_LOCATION)) { console.warn("[Sign] No debug log file found."); @@ -46,7 +45,9 @@ function printSigningErrorLogs(sourcePath) { console.error("[Sign] Debug log content:\n" + logContent); } -function getDllArchitectureFromBuffer(buffer) { +function getDllArchitectureFromFile(filePath) { + const buffer = fs.readFileSync(filePath); + // Check for MZ header if (buffer[0] !== 0x4D || buffer[1] !== 0x5A) { return 'Not a PE file (missing MZ header)'; From 308bbd160a627746bdbfb707f1fa9880befaf567 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 1 May 2025 17:59:52 +0300 Subject: [PATCH 2/3] chore(sign): log architecture of sign tool --- bin/electron-forge/sign-windows.cjs | 74 ++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/bin/electron-forge/sign-windows.cjs b/bin/electron-forge/sign-windows.cjs index 12246842d..c84ccf98e 100644 --- a/bin/electron-forge/sign-windows.cjs +++ b/bin/electron-forge/sign-windows.cjs @@ -1,29 +1,77 @@ const child_process = require("child_process"); const fs = require("fs"); -const { default: path } = require("path"); +const path = require("path"); -module.exports = function (filePath) { - const { WINDOWS_SIGN_EXECUTABLE } = process.env; - - const stats = fs.lstatSync(filePath); - console.log(filePath, stats); +const LOG_LOCATION = "c:\\ev_signer_trilium\\ev_signer_trilium.err.log"; +const { WINDOWS_SIGN_EXECUTABLE } = process.env; +module.exports = function (sourcePath) { if (!WINDOWS_SIGN_EXECUTABLE) { console.warn("[Sign] Skip signing due to missing environment variable."); return; } const outputDir = path.join(__dirname, "sign"); - console.log("Output dir is ", path.resolve(outputDir)); if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir); } - fs.copyFileSync(sourcePath, destPath); + try { + const destPath = path.resolve(path.join(outputDir, path.basename(sourcePath))); + fs.copyFileSync(sourcePath, destPath); + console.log(["[Sign] Copying file to signing location", destPath].join(" ")); + const command = `${WINDOWS_SIGN_EXECUTABLE} --executable "${sourcePath}"`; + console.log(`[Sign] ${command}`); - const command = `${WINDOWS_SIGN_EXECUTABLE} --executable "${filePath}"`; - console.log(`[Sign] ${command}`); + child_process.execSync(command); + } catch (e) { + console.error("[Sign] Got error while signing " + e.output.toString("utf-8")); + printSigningErrorLogs(sourcePath); + process.exit(2); + } +} - const output = child_process.execSync(command); - console.log(`[Sign] ${output}`); -} \ No newline at end of file +function printSigningErrorLogs(sourcePath) { + console.log("Platform: ", process.platform); + console.log("CPU archi:", process.arch); + console.log("DLL archi: ", getDllArchitectureFromFile(sourcePath)); + console.log("Signer archi: ", getDllArchitectureFromFile(WINDOWS_SIGN_EXECUTABLE)); + + if (!fs.existsSync(LOG_LOCATION)) { + console.warn("[Sign] No debug log file found."); + return; + } + + const logContent = fs.readFileSync(LOG_LOCATION, "utf-8"); + console.error("[Sign] Debug log content:\n" + logContent); +} + +function getDllArchitectureFromFile(filePath) { + const buffer = fs.readFileSync(filePath); + + // Check for MZ header + if (buffer[0] !== 0x4D || buffer[1] !== 0x5A) { + return 'Not a PE file (missing MZ header)'; + } + + // Offset to PE header + const peHeaderOffset = buffer.readUInt32LE(0x3C); + + // Confirm PE signature + const peSig = buffer.toString('utf8', peHeaderOffset, peHeaderOffset + 4); + if (peSig !== 'PE\u0000\u0000') { + return 'Invalid PE header'; + } + + // Machine field is 2 bytes at PE header + 4 + const machine = buffer.readUInt16LE(peHeaderOffset + 4); + + const archMap = { + 0x014c: 'x86 (32-bit)', + 0x8664: 'x64 (64-bit)', + 0x01c4: 'ARM (32-bit)', + 0xaa64: 'ARM64', + }; + + return archMap[machine] || `Unknown (0x${machine.toString(16)})`; + } \ No newline at end of file From 8f46116e9aa291ca23412cb64c4250b6cbdbc082 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 1 May 2025 19:15:02 +0300 Subject: [PATCH 3/3] chore(sign): stop terminating signing if it fails --- apps/desktop/electron-forge/sign-windows.cjs | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/desktop/electron-forge/sign-windows.cjs b/apps/desktop/electron-forge/sign-windows.cjs index c59f3ca3f..b0a2abc90 100644 --- a/apps/desktop/electron-forge/sign-windows.cjs +++ b/apps/desktop/electron-forge/sign-windows.cjs @@ -26,7 +26,6 @@ module.exports = function (sourcePath) { } catch (e) { console.error("[Sign] Got error while signing " + e.output.toString("utf-8")); printSigningErrorLogs(sourcePath); - process.exit(2); } }