Skip to content

Commit 99145d1

Browse files
committed
feat: add validation of the prefix
1 parent 6bb2e37 commit 99145d1

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/mongodb-log-writer/src/mongo-log-manager.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,48 @@ describe('MongoLogManager', function () {
2929
await fs.rmdir(directory, { recursive: true });
3030
});
3131

32+
it('constructor throws with invalid prefixes', function () {
33+
expect(() => {
34+
new MongoLogManager({
35+
directory,
36+
retentionDays,
37+
prefix: '%asdabs/',
38+
onwarn,
39+
onerror,
40+
});
41+
}).to.throw();
42+
43+
expect(() => {
44+
new MongoLogManager({
45+
directory,
46+
retentionDays,
47+
prefix: '$$$$',
48+
onwarn,
49+
onerror,
50+
});
51+
}).to.throw();
52+
53+
expect(() => {
54+
new MongoLogManager({
55+
directory,
56+
retentionDays,
57+
prefix: 'abc_',
58+
onwarn,
59+
onerror,
60+
});
61+
}).not.to.throw();
62+
63+
expect(() => {
64+
new MongoLogManager({
65+
directory,
66+
retentionDays,
67+
prefix: 'something',
68+
onwarn,
69+
onerror,
70+
});
71+
}).not.to.throw();
72+
});
73+
3274
it('allows creating and writing to log files', async function () {
3375
const manager = new MongoLogManager({
3476
directory,

packages/mongodb-log-writer/src/mongo-log-manager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export class MongoLogManager {
3636
_options: MongoLogOptions;
3737

3838
constructor(options: MongoLogOptions) {
39+
if (options.prefix) {
40+
if (!/^[a-z0-9_]+$/i.test(options.prefix)) {
41+
throw new Error(
42+
'Prefix must only contain letters, numbers, and underscores'
43+
);
44+
}
45+
}
3946
this._options = options;
4047
}
4148

@@ -69,7 +76,6 @@ export class MongoLogManager {
6976
? this._options.logRetentionGB * 1024 * 1024 * 1024
7077
: Infinity;
7178
let usedStorageSize = this._options.logRetentionGB ? 0 : -Infinity;
72-
// eslint-disable-next-line no-console
7379

7480
for await (const dirent of dirHandle) {
7581
// Cap the overall time spent inside this function. Consider situations like

0 commit comments

Comments
 (0)