[env] throw an error when UUID file is empty (#164802)

## Summary

Another victim of the infamous javascript true-ish/false-ish.

Fix a bug that allowed an empty UUID file to bypass the regexp
validation and use an empty string as instance UUID.
This commit is contained in:
Pierre Gayvallet 2023-08-25 18:45:00 +02:00 committed by GitHub
parent 76ac34a9b7
commit b74311ffc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -158,6 +158,15 @@ describe('resolveInstanceUuid', () => {
});
});
describe('when file exists but is empty', () => {
it('throws an explicit error for uuid formatting', async () => {
mockReadFile({ uuid: '' });
await expect(
resolveInstanceUuid({ pathConfig, serverConfig, logger })
).rejects.toThrowErrorMatchingInlineSnapshot(`"data-folder/uuid contains an invalid UUID"`);
});
});
describe('when file contains a trailing new line', () => {
it('returns the trimmed file uuid', async () => {
mockReadFile({ uuid: DEFAULT_FILE_UUID + '\n' });

View file

@ -65,13 +65,16 @@ export async function resolveInstanceUuid({
async function readUuidFromFile(filepath: string, logger: Logger): Promise<string | undefined> {
const content = await readFileContent(filepath);
if (content === undefined) {
return undefined;
}
if (content === UUID_7_6_0_BUG) {
logger.debug(`UUID from 7.6.0 bug detected, ignoring file UUID`);
return undefined;
}
if (content && !content.match(uuidRegexp)) {
if (!content.match(uuidRegexp)) {
throw new Error(`${filepath} contains an invalid UUID`);
}