Skip to content

Commit

Permalink
feat(back): Added checks for BSP compression
Browse files Browse the repository at this point in the history
  • Loading branch information
GordiNoki authored and tsa96 committed Jan 11, 2024
1 parent 2b23ab3 commit c7a7202
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Binary file added apps/backend-e2e/files/map_nozip.bsp
Binary file not shown.
33 changes: 33 additions & 0 deletions apps/backend-e2e/src/maps.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ describe('Maps', () => {
u2,
u3,
bspBuffer,
nozipBspBuffer,
bspHash,
vmfBuffer,
vmfHash;
Expand All @@ -519,6 +520,8 @@ describe('Maps', () => {
bspBuffer = readFileSync(path.join(FILES_PATH, 'map.bsp'));
bspHash = createSha1Hash(bspBuffer);

nozipBspBuffer = readFileSync(path.join(FILES_PATH, 'map_nozip.bsp'));

vmfBuffer = readFileSync(path.join(FILES_PATH, 'map.vmf'));
vmfHash = createSha1Hash(vmfBuffer);

Expand Down Expand Up @@ -976,6 +979,36 @@ describe('Maps', () => {
});
});

it('should 400 if a BSP file has invalid header', async () => {
await req.postAttach({
url: 'maps',
status: 400,
data: createMapObject,
files: [
{
file: Buffer.alloc(100),
field: 'bsp',
fileName: 'surf_map.bsp'
},
{ file: vmfBuffer, field: 'vmfs', fileName: 'surf_map.vmf' }
],
token
});
});

it('should 400 if a BSP file was not compressed', async () => {
await req.postAttach({
url: 'maps',
status: 400,
data: createMapObject,
files: [
{ file: nozipBspBuffer, field: 'bsp', fileName: 'surf_map.bsp' },
{ file: vmfBuffer, field: 'vmfs', fileName: 'surf_map.vmf' }
],
token
});
});

it('should succeed if VMF file is missing', async () =>
req.postAttach({
url: 'maps',
Expand Down
25 changes: 25 additions & 0 deletions apps/backend/src/app/modules/maps/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
LeaderboardHandler,
LeaderboardProps
} from './leaderboard-handler.util';
import { BspHeader, BspReadError } from '@momentum/formats/bsp';

@Injectable()
export class MapsService {
Expand Down Expand Up @@ -538,6 +539,9 @@ export class MapsService {
vmfFiles?: File[]
): Promise<MapDto> {
await this.checkCreateDto(userID, dto);

await this.checkMapCompression(bspFile);

this.checkMapFiles(dto.fileName, bspFile, vmfFiles);
this.checkMapFileNames(dto.name, dto.fileName);

Expand Down Expand Up @@ -634,6 +638,8 @@ export class MapsService {
throw new ForbiddenException('Map does not allow editing');
}

await this.checkMapCompression(bspFile);

this.checkMapFiles(map.fileName, bspFile, vmfFiles);

const hasVmf = vmfFiles?.length > 0;
Expand Down Expand Up @@ -1674,6 +1680,25 @@ export class MapsService {
}
}

/**
* Check if provided bsp file was compressed with bspzip
* @throws BadRequestException
*/
async checkMapCompression(bspFile: File) {
const header = await BspHeader.fromBlob(new Blob([bspFile.buffer])).catch(
(error) => {
throw new BadRequestException(
error instanceof BspReadError
? error.message
: 'Unknown error reading BSP file'
);
}
);

if (!header.isCompressed())
throw new BadRequestException('BSP is not compressed');
}

//#endregion
}

Expand Down

0 comments on commit c7a7202

Please sign in to comment.