Skip to content
Merged
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
3 changes: 2 additions & 1 deletion ui/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
} from './utils/autoUpdater';
import { UPDATES_ENABLED } from './updates';
import './utils/recipeHash';
import { calculateStableRecipeHash } from './utils/stableRecipeHash';
import { Client } from './api/client';
import { GooseApp } from './api';
import * as mesh from './mesh';
Expand Down Expand Up @@ -210,7 +211,7 @@ async function seedDefaultRecipes(): Promise<void> {
) {
bundledTitles.push({ title: parsed.title, description: parsed.description });
}
const hash = crypto.createHash('sha256').update(JSON.stringify(parsed)).digest('hex');
const hash = calculateStableRecipeHash(parsed);
const hashFile = path.join(hashesDir, `${hash}.hash`);
if (!fsSync.existsSync(hashFile)) {
await fs.writeFile(hashFile, new Date().toISOString());
Expand Down
12 changes: 3 additions & 9 deletions ui/desktop/src/utils/recipeHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { ipcMain, app, BrowserWindow } from 'electron';
import fs from 'node:fs/promises';
import fsSync from 'node:fs';
import path from 'node:path';
import crypto from 'crypto';

function calculateRecipeHash(recipe: unknown): string {
const hash = crypto.createHash('sha256');
hash.update(JSON.stringify(recipe));
return hash.digest('hex');
}
import { calculateStableRecipeHash } from './stableRecipeHash';

async function getRecipeHashesDir(): Promise<string> {
const userDataPath = app.getPath('userData');
Expand Down Expand Up @@ -41,7 +35,7 @@ function isBundledRecipeByTitleAndDescription(recipe: unknown): boolean {
}

ipcMain.handle('has-accepted-recipe-before', async (_event, recipe) => {
const hash = calculateRecipeHash(recipe);
const hash = calculateStableRecipeHash(recipe);
const hashFile = path.join(await getRecipeHashesDir(), `${hash}.hash`);
try {
await fs.access(hashFile);
Expand All @@ -55,7 +49,7 @@ ipcMain.handle('has-accepted-recipe-before', async (_event, recipe) => {
});

ipcMain.handle('record-recipe-hash', async (_event, recipe) => {
const hash = calculateRecipeHash(recipe);
const hash = calculateStableRecipeHash(recipe);
const filePath = path.join(await getRecipeHashesDir(), `${hash}.hash`);
const timestamp = new Date().toISOString();
await fs.writeFile(filePath, timestamp);
Expand Down
23 changes: 23 additions & 0 deletions ui/desktop/src/utils/stableRecipeHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest';
import { calculateStableRecipeHash } from './stableRecipeHash';

describe('calculateStableRecipeHash', () => {
it('is stable when object keys are serialized in a different order', () => {
const fromSave = {
version: '1.0.0',
title: '本地验收工作流',
description: '用于验证保存后信任',
instructions: '请按要求回答。',
parameters: [{ key: 'question', input_type: 'string', requirement: 'required' }],
};
const fromList = {
parameters: [{ requirement: 'required', input_type: 'string', key: 'question' }],
instructions: '请按要求回答。',
description: '用于验证保存后信任',
title: '本地验收工作流',
version: '1.0.0',
};

expect(calculateStableRecipeHash(fromSave)).toBe(calculateStableRecipeHash(fromList));
});
});
27 changes: 27 additions & 0 deletions ui/desktop/src/utils/stableRecipeHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import crypto from 'crypto';

function normalizeForHash(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map(normalizeForHash);
}

if (value && typeof value === 'object') {
return Object.keys(value as Record<string, unknown>)
.sort()
.reduce<Record<string, unknown>>((acc, key) => {
const item = (value as Record<string, unknown>)[key];
if (item !== undefined) {
acc[key] = normalizeForHash(item);
}
return acc;
}, {});
}

return value;
}

export function calculateStableRecipeHash(recipe: unknown): string {
const hash = crypto.createHash('sha256');
hash.update(JSON.stringify(normalizeForHash(recipe)));
return hash.digest('hex');
}
Loading