mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
fix(journey): validate and safely serve media uploads
This commit is contained in:
@@ -10,12 +10,14 @@
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { Addons } from '../../src/db/entities/Addons.entity';
|
||||
import { JourneyPhotos } from '../../src/db/entities/JourneyPhotos.entity';
|
||||
import { Photos } from '../../src/db/entities/Photos.entity';
|
||||
import { TrekPhotos } from '../../src/db/entities/TrekPhotos.entity';
|
||||
import { authCookie, generateToken } from '../helpers/auth';
|
||||
import { jpegWithExif } from '../helpers/exif-jpeg';
|
||||
import { makeJourney } from '../helpers/factories/journeys';
|
||||
import { findRow, insertRow, upsertRow } from '../helpers/factories/rows';
|
||||
import { setAppSetting } from '../helpers/factories/settings';
|
||||
import { makeShareToken, makeTrip } from '../helpers/factories/trips';
|
||||
import { makeUser } from '../helpers/factories/users';
|
||||
import { resetDatabase } from '../helpers/reset-database';
|
||||
@@ -123,6 +125,9 @@ describe('journey share link', () => {
|
||||
|
||||
const owned = await bytes(request(app).get(`/api/photos/${photoId}/original`).set('Cookie', authCookie(user.id)));
|
||||
expect(owned.status).toBe(200);
|
||||
expect(owned.headers['content-type']).toBe('image/jpeg');
|
||||
expect(owned.headers['content-disposition']).toBeUndefined();
|
||||
expect(owned.headers['content-security-policy']).toBe('sandbox');
|
||||
expect(await gpsOf(owned.body as Buffer)).toBeCloseTo(GPS.lat, 5);
|
||||
});
|
||||
|
||||
@@ -150,6 +155,45 @@ describe('journey share link', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Journey media format', () => {
|
||||
it.each(['document.txt', 'invalid.png'])(
|
||||
'rejects %s submitted as a photo even with wildcard uploads',
|
||||
async (filename) => {
|
||||
const { user } = await makeUser(orm);
|
||||
const journey = await makeJourney(orm, user.id);
|
||||
await setAppSetting(orm, 'allowed_file_types', '*');
|
||||
|
||||
await request(app)
|
||||
.post(`/api/journeys/${journey.id}/gallery/photos`)
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.attach('photos', Buffer.from('not an image'), { filename, contentType: 'image/png' })
|
||||
.expect(400);
|
||||
|
||||
expect(await findRow(orm, JourneyPhotos, { journey: journey.id })).toBeNull();
|
||||
},
|
||||
);
|
||||
|
||||
it('serves a legacy document stored as a Journey photo as a download', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
const journey = await makeJourney(orm, user.id);
|
||||
const name = 'pubmedia-legacy.integration.html';
|
||||
const data = Buffer.from('legacy document fixture');
|
||||
fs.mkdirSync(path.join(uploadsDir, 'journey'), { recursive: true });
|
||||
fs.writeFileSync(path.join(uploadsDir, 'journey', name), data);
|
||||
track('journey', name);
|
||||
const photoId = await insertRow(orm, TrekPhotos, { provider: 'local', file_path: `journey/${name}` });
|
||||
await insertRow(orm, JourneyPhotos, { journey: journey.id, photo: photoId, created_at: Date.now() });
|
||||
|
||||
const res = await bytes(request(app).get(`/api/photos/${photoId}/original`).set('Cookie', authCookie(user.id)));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-type']).toBe('application/octet-stream');
|
||||
expect(res.headers['content-disposition']).toBe('attachment');
|
||||
expect(res.headers['content-security-policy']).toBe('sandbox');
|
||||
expect(res.headers['x-content-type-options']).toBe('nosniff');
|
||||
expect(res.body).toEqual(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('/uploads/journey', () => {
|
||||
it('PUBMEDIA-010: serves a cover, location-free, and 404s an entry photo for everyone', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
|
||||
Reference in New Issue
Block a user