mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: oplog: santitize argument keys
This commit is contained in:
1 parent
3ee29da3bd
commit
94244cb55f
1 file changed
+22
-3
@@ -1,3 +1,4 @@
|
||||
import { cloneDeep } from 'lodash';
|
||||
import { ObjectID } from 'mongodb';
|
||||
import { OplogDoc } from '../interface';
|
||||
import * as bus from '../service/bus';
|
||||
@@ -16,10 +17,28 @@ export async function add(data: Partial<OplogDoc> & { type: string }): Promise<O
|
||||
return res.insertedId;
|
||||
}
|
||||
|
||||
function safeKeys(data: any) {
|
||||
if (['string', 'number', 'boolean'].includes(typeof data)) return data;
|
||||
if (data instanceof Array) data.forEach(safeKeys);
|
||||
else if (data instanceof ObjectID) return data;
|
||||
else if (data instanceof Object) {
|
||||
for (const key in data) {
|
||||
if (['password', 'verifyPassword'].includes(key)) {
|
||||
delete data[key];
|
||||
continue;
|
||||
}
|
||||
safeKeys(data[key]);
|
||||
if (key.includes('$') || key.includes('.')) {
|
||||
data[key.replace(/[$.]/g, '_')] = data[key];
|
||||
delete data[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function log<T extends Handler>(handler: T, type: string, data: any) {
|
||||
const args = { ...handler.args };
|
||||
delete args.password;
|
||||
delete args.verifyPassword;
|
||||
const args = safeKeys(cloneDeep(handler.args));
|
||||
await bus.parallel('oplog/log', type, handler, args, data);
|
||||
const res = await coll.insertOne({
|
||||
...data,
|
||||
|
||||
Reference in new issue
Block a user