Skip to content

Commit

Permalink
Validate GUID in Uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
myrotvorets-team committed Sep 26, 2023
1 parent 3235444 commit 754c20b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/services/upload.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class UploadService {
}

public static filenameByGuid(guid: string, ext = '.jpg'): string {
if (!/^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/u.test(guid)) {
throw new TypeError(`GUID is not valid: ${guid}`);
}

const hashedPath = UploadService.hashFileName(guid);
const filename = `${guid}${ext}`;
return join(hashedPath, filename);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/services/upload.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@ describe('UploadService', function () {
const actual = service.filenameByGuid(input, '');
expect(actual).to.equal(expected);
});

it('should reject invalid input', function () {
expect(() => service.filenameByGuid('guid', '')).to.throw(TypeError);
});
});
});

0 comments on commit 754c20b

Please sign in to comment.