Note N3 · Peder
FILE ACCESS
Letting users download shared files safely: ownership, relationship checks and the right headers
- Sheet
- PEDER
- Topic
- Cloudflare Workers · Hono
- Source
- src/rotalar/dosya.js
- Date
- 2026-11-12
- Reading
- 2 min
Short answer: knowing a file's download URL must not be enough to download it. Validate the id as a number, check the record exists, check the requester is the sender or the recipient; if the recipient, check the counselling relationship with the sender is still active. Log unauthorised attempts and write penalty points to the IP. And send the file in a way the browser can neither execute nor cache.
01Order of checks
const dosyaId = Number(c.req.param('id'));
if (!Number.isSafeInteger(dosyaId) || dosyaId <= 0) return c.text('Geçersiz dosya kimliği.', 400);
const dosya = await bir(c, 'SELECT * FROM dosyalar WHERE id = ? AND silindi = 0', dosyaId);
if (!dosya) return c.text('Dosya bulunamadı.', 404);
if (dosya.gonderen_id !== ben.id && dosya.alici_id !== ben.id) {
await cezaPuaniEkle(c, ip(c), 'IDOR_ENGELI', `dosya_id=${dosyaId}`, 'kritik');
return c.text('Bu dosyaya erişim yetkiniz bulunmamaktadır.', 403);
}
- The id comes from the URL;
Number.isSafeIntegerchecks that it is a number and doesn't overflow. The query is parameterised. - A deleted file counts as "not found".
- Sequential ids are guessable, so knowing an id means nothing; only the sender and the recipient have access. A user asking for someone else's file is signed in; the attempt is written to the security log and passed to the IP penalty engine with high points.
02When the relationship ends, so does access
File sharing between a trainee and a client is part of a counselling relationship. When the relationship ends (the trainee changed, the process finished), the recipient shouldn't keep reaching old files. For the recipient it is also checked that messaging between the two parties is still allowed; the sender can always reach their own file. Every successful download is written to the access log.
03Response headers
c.header('Content-Type', 'application/octet-stream');
c.header('Content-Disposition', `attachment; filename="${asciiAd}"; filename*=UTF-8''${utfAd}`);
c.header('X-Content-Type-Options', 'nosniff');
c.header('Content-Security-Policy', "default-src 'none'; sandbox");
c.header('Cache-Control', 'no-store, no-cache, must-revalidate, private');
c.header('Vary', 'Cookie');
- octet-stream + attachment + nosniff: the browser downloads the file instead of opening it, and doesn't sniff the content and treat it as HTML or script.
- CSP sandbox: even if the file somehow opens in the browser, it can't run scripts and doesn't count as the site's origin.
- The filename twice: an ASCII-reduced name with quotes and backslashes cleaned for old browsers, and an RFC 5987 UTF-8 name for the rest. Turkish filenames don't break, and a user-supplied name can't break the header.
- no-store, private, Vary: Cookie: no copy of the file stays on a shared computer or in an intermediate cache.
04Storage
Files are stored in the database encrypted on the client; the server returns the encrypted data as is. On upload, the encrypted data is checked to be long enough to carry an AES-GCM authentication tag.
→More notes from this project
—End of notewritten by MUHAMMEDPYZ