← Back to PRs

#9817: fix(media): resolve relative paths before reading local files (#8759)

by lailoo open 2026-02-05 18:26 View on GitHub →
channel: whatsapp-web stale
Fixes #8759 ## Problem When an agent reply contains a `MEDIA:./path` directive with a relative path (e.g., `MEDIA:./avatars/default.jpg`), the file read fails with ENOENT even though the file exists. ## Root Cause `loadWebMedia()` in `src/web/media.ts` was calling `fs.readFile(mediaUrl)` directly for local paths without resolving relative paths to absolute paths first. ## Solution Resolve relative paths starting with `./` or `../` against `process.cwd()` before reading: ```typescript if (mediaUrl.startsWith('./') || mediaUrl.startsWith('../')) { mediaUrl = path.resolve(process.cwd(), mediaUrl); } ``` ## Testing Added regression tests for both `./` and `../` relative paths. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR fixes local `MEDIA:` loads when the directive uses a relative path by resolving `./` and `../` paths against `process.cwd()` before calling `fs.readFile` in `src/web/media.ts`. It also adds regression tests in `src/web/media.test.ts` intended to cover both `./` and `../` relative paths. The change fits into the existing `loadWebMediaInternal` flow by keeping remote URLs handled via `fetchRemoteMedia`, expanding `~` via `resolveUserPath`, and then normalizing local paths prior to reading/sniffing MIME. <h3>Confidence Score: 4/5</h3> - This PR is likely safe to merge once the regression test correctly exercises `../`-prefixed paths. - The runtime change is small and localized (path resolution before local reads). The main issue is test coverage: the added `../` test doesn’t actually start with `../`, so it doesn’t guard the new branch and could miss regressions. - src/web/media.test.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs