Skip to content

Commit 8a62d73

Browse files
committed
feat: add validation of the prefix
1 parent 2eb3d4a commit 8a62d73

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
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
@@ -31,6 +31,48 @@ describe('MongoLogManager', function () {
3131
sinon.restore();
3232
});
3333

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

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

Lines changed: 7 additions & 0 deletions
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

0 commit comments

Comments
 (0)