Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): stage annotation into an approved init-image root

Adam Eivy178e5a3dde75
diff --git a/server/routes/imageGen.js b/server/routes/imageGen.js
index 9fa292ea36..8912ad8f0f 100644
--- a/server/routes/imageGen.js
+++ b/server/routes/imageGen.js
@@ -11,7 +11,7 @@
import { Router } from 'express';
import { z } from 'zod';
-import { unlink, stat } from 'fs/promises';
+import { unlink, stat, copyFile } from 'fs/promises';
import { asyncHandler, ServerError, failValidation } from '../lib/errorHandler.js';
import {
validateRequest, imageEdgeSchema, refineImagePixelCap, PIXEL_CAP_MESSAGE,
@@ -714,10 +714,20 @@ router.post('/:filename/regenerate', asyncHandler(async (req, res) => {
if (!isValidSketchKey(key)) {
throw new ServerError('Image cannot be annotated', { status: 400, code: 'VALIDATION_ERROR' });
}
- initImageAbsPath = await getSketchPngPath(key);
- if (!initImageAbsPath) {
+ const sketchPngPath = await getSketchPngPath(key);
+ if (!sketchPngPath) {
throw new ServerError('No saved annotation to re-render — draw over the image and save first.', { status: 400, code: 'NO_ANNOTATION' });
}
+ // The local runner re-validates initImagePath against its approved image
+ // roots (gallery / image-refs / visual-templates) and silently drops
+ // anything outside them — `data/media-sketches/` is NOT one, so passing the
+ // sidecar path directly would make the re-render ignore the annotation.
+ // Stage a snapshot of the flattened annotation into the image-refs root the
+ // runner accepts; the copy also freezes the markup at enqueue time so a
+ // later re-save can't change what this queued job renders from.
+ await ensureDir(PATHS.imageRefs);
+ initImageAbsPath = join(PATHS.imageRefs, `annot-${randomUUID()}.png`);
+ await copyFile(sketchPngPath, initImageAbsPath);
}
// Sidecar (for prompt/model) and the on-disk dimension probe have no data
diff --git a/server/routes/imageGen.test.js b/server/routes/imageGen.test.js
index d53b10e25a..061b270f46 100644
--- a/server/routes/imageGen.test.js
+++ b/server/routes/imageGen.test.js
@@ -6,6 +6,9 @@ import imageGenRoutes from './imageGen.js';
import * as fileUtils from '../lib/fileUtils.js';
import * as regen from '../services/imageGen/regen.js';
import * as mediaSketches from '../services/mediaSketches.js';
+import { writeFile, rm } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join as pathJoin } from 'path';
vi.mock('../services/imageGen/index.js', () => ({
checkConnection: vi.fn(),
@@ -1191,16 +1194,23 @@ describe('Image Gen Routes', () => {
.send({ annotated: true, method: 'light' });
expect(response.status).toBe(400);
+ expect(response.body.code).toBe('VALIDATION_ERROR');
expect(mediaJobQueue.enqueueJob).not.toHaveBeenCalled();
resolveGallerySpy.mockRestore();
});
- it('seeds the annotated re-render init image from the sketch PNG at the higher default strength', async () => {
+ it('stages the flattened annotation into an init-image root the runner accepts, at the higher default strength', async () => {
+ // A real flattened-sketch file for the route to copy — the fix stages a
+ // snapshot into PATHS.imageRefs, and the runner's resolveImageInputPath
+ // must accept the staged path (media-sketches/ is NOT an approved root).
+ const srcSketch = pathJoin(tmpdir(), `annot-src-${Date.now()}.png`);
+ await writeFile(srcSketch, Buffer.from('89504e470d0a1a0a', 'hex'));
+
const resolveGallerySpy = vi.spyOn(fileUtils, 'resolveGalleryImage')
.mockReturnValueOnce('/fake/gallery/source.png');
const sketchPathSpy = vi.spyOn(mediaSketches, 'getSketchPngPath')
- .mockResolvedValueOnce('/fake/data/media-sketches/abc.png');
+ .mockResolvedValueOnce(srcSketch);
imageGen.local.readImageSidecar.mockResolvedValueOnce({
path: '/fake/gallery/source.png.json',
metadata: { modelId: 'flux-dev', prompt: 'a robot' },
@@ -1209,8 +1219,7 @@ describe('Image Gen Routes', () => {
.mockResolvedValueOnce({ width: 1024, height: 1024 });
const resolveBackendSpy = vi.spyOn(regen, 'resolveRegenBackend')
.mockResolvedValueOnce({ available: true, model: { id: 'flux-dev' }, pythonPath: '/fake/venv/python' });
- const buildParamsSpy = vi.spyOn(regen, 'buildRegenParams')
- .mockReturnValueOnce({ kind: 'image', regenJob: true, filename: 'source.png' });
+ // buildRegenParams is NOT mocked — assert the REAL params the route enqueues.
mediaJobQueue.enqueueJob.mockReturnValueOnce({ jobId: 'annot-job-001', position: 1, status: 'queued' });
const response = await request(app)
@@ -1219,17 +1228,21 @@ describe('Image Gen Routes', () => {
expect(response.status).toBe(200);
expect(response.body.jobId).toBe('annot-job-001');
- expect(buildParamsSpy).toHaveBeenCalledWith(expect.objectContaining({
- initImageAbsPath: '/fake/data/media-sketches/abc.png',
- annotated: true,
- strength: regen.REGEN_ANNOTATED_STRENGTH_DEFAULT,
- }));
-
+ const [enqueued] = mediaJobQueue.enqueueJob.mock.calls;
+ const params = enqueued[0].params;
+ expect(params.annotatedRegen).toBe(true);
+ expect(params.initImageStrength).toBe(regen.REGEN_ANNOTATED_STRENGTH_DEFAULT);
+ // Regression guard for the silently-dropped-init-image bug: the staged
+ // path must resolve under an approved image-input root.
+ expect(params.initImagePath).toContain('image-refs');
+ expect(fileUtils.resolveImageInputPath(params.initImagePath)).not.toBeNull();
+
+ await rm(params.initImagePath, { force: true });
+ await rm(srcSketch, { force: true });
resolveGallerySpy.mockRestore();
sketchPathSpy.mockRestore();
readDimsSpy.mockRestore();
resolveBackendSpy.mockRestore();
- buildParamsSpy.mockRestore();
});
});
});