@@ -191,7 +191,7 @@ func TestImportDesktopCopyMedia(t *testing.T) {
191191 ctx := context .Background ()
192192 source := t .TempDir ()
193193 createFixtureDBs (t , source )
194- mediaPath := filepath .Join (source , "Media" , "123@g.us" , "a" , "test.jpg" )
194+ mediaPath := filepath .Join (source , "Message" , " Media" , "123@g.us" , "a" , "test.jpg" )
195195 if err := os .MkdirAll (filepath .Dir (mediaPath ), 0o700 ); err != nil {
196196 t .Fatal (err )
197197 }
@@ -239,19 +239,60 @@ insert into ZWAMESSAGE values (5, 2, 1, 2, 'missing-media', 0, 700000004, 'missi
239239 missingPath = msg .MediaPath
240240 }
241241 }
242- wantCopied := filepath .Join (filepath .Dir (archivePath ), "media" , "Media" , "123@g.us" , "a" , "test.jpg" )
242+ wantCopied := filepath .Join (filepath .Dir (archivePath ), "media" , "Message" , " Media" , "123@g.us" , "a" , "test.jpg" )
243243 if copiedPath != wantCopied {
244244 t .Fatalf ("copied media path = %q, want %q" , copiedPath , wantCopied )
245245 }
246246 if data , err := os .ReadFile (copiedPath ); err != nil || string (data ) != "image" { // #nosec G304 -- copiedPath is asserted against the expected temp archive path above.
247247 t .Fatalf ("copied media content = %q err=%v" , data , err )
248248 }
249- wantMissing := filepath .Join (source , "Media" , "123@g.us" , "a" , "missing.jpg" )
249+ wantMissing := filepath .Join (source , "Message" , " Media" , "123@g.us" , "a" , "missing.jpg" )
250250 if missingPath != wantMissing {
251251 t .Fatalf ("missing media path = %q, want original %q" , missingPath , wantMissing )
252252 }
253253}
254254
255+ func TestResolveDesktopMediaPathPrefersMessageMedia (t * testing.T ) {
256+ source := t .TempDir ()
257+ messageMedia := filepath .Join (source , "Message" , "Media" , "chat" , "photo.jpg" )
258+ if err := os .MkdirAll (filepath .Dir (messageMedia ), 0o700 ); err != nil {
259+ t .Fatal (err )
260+ }
261+ if err := os .WriteFile (messageMedia , []byte ("image" ), 0o600 ); err != nil {
262+ t .Fatal (err )
263+ }
264+ if got := resolveDesktopMediaPath (source , "Media/chat/photo.jpg" ); got != messageMedia {
265+ t .Fatalf ("resolved media path = %q, want %q" , got , messageMedia )
266+ }
267+
268+ legacyMedia := filepath .Join (source , "Media" , "chat" , "legacy.jpg" )
269+ if err := os .MkdirAll (filepath .Dir (legacyMedia ), 0o700 ); err != nil {
270+ t .Fatal (err )
271+ }
272+ if err := os .WriteFile (legacyMedia , []byte ("image" ), 0o600 ); err != nil {
273+ t .Fatal (err )
274+ }
275+ if got := resolveDesktopMediaPath (source , "Media/chat/legacy.jpg" ); got != legacyMedia {
276+ t .Fatalf ("legacy media path = %q, want %q" , got , legacyMedia )
277+ }
278+
279+ missing := filepath .Join (source , "Message" , "Media" , "chat" , "missing.jpg" )
280+ if got := resolveDesktopMediaPath (source , "Media/chat/missing.jpg" ); got != missing {
281+ t .Fatalf ("missing media path = %q, want %q" , got , missing )
282+ }
283+
284+ absolute := filepath .Join (string (os .PathSeparator ), "tmp" , "outside.jpg" )
285+ confined := filepath .Join (source , "tmp" , "outside.jpg" )
286+ if got := resolveDesktopMediaPath (source , absolute ); got != confined {
287+ t .Fatalf ("absolute media path = %q, want confined %q" , got , confined )
288+ }
289+
290+ traversal := filepath .Join (source , "outside.jpg" )
291+ if got := resolveDesktopMediaPath (source , "../outside.jpg" ); got != traversal {
292+ t .Fatalf ("traversal media path = %q, want confined %q" , got , traversal )
293+ }
294+ }
295+
255296func TestCopyArchiveMediaDeduplicatesAndConfinesPaths (t * testing.T ) {
256297 source := t .TempDir ()
257298 mediaRoot := filepath .Join (t .TempDir (), "media" )
0 commit comments