Skip to content

Commit 9245f0f

Browse files
committed
added sanitizeKeys to sanitize keys
1 parent c0f5fd2 commit 9245f0f

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ sanitizer.clean({
119119
```
120120

121121

122-
##### Whitelisting Routes
122+
#### Whitelisting Routes
123123

124124
If you want to skip sanitization for certain routes, you can specify a whitelist of routes when setting up the middleware:
125125

@@ -138,7 +138,7 @@ app.use(
138138
);
139139
```
140140

141-
##### Limit Sanitization
141+
#### Limit Sanitization
142142

143143
By default, `perfect-express-sanitizer` sanitizes all parts of the request (body, query, and header). If you only want to sanitize specific parts of the request, you can specify them when setting up the middleware:
144144

@@ -156,7 +156,24 @@ app.use(
156156
);
157157
```
158158

159-
##### Setting Sanitization Levels
159+
For an option that sanitizes keys, you could consider the following option `sanitizeKeys: true` example:
160+
161+
```javascript
162+
app.use(
163+
sanitizer.clean(
164+
{
165+
xss: true,
166+
noSql: true,
167+
sanitizeKeys: true,
168+
},
169+
whiteList = [],
170+
only = ["body", "query"]
171+
)
172+
);
173+
```
174+
175+
176+
#### Setting Sanitization Levels
160177

161178
You can set different levels of sanitization for SQL and NoSQL injections by specifying the sqlLevel and noSqlLevel options when setting up the middleware. The levels range from 1 to 5, with higher levels providing more comprehensive sanitization.
162179

modules/nosql_injection.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,23 @@ const sanitize = (data, options) => {
6666
});
6767
}
6868
if (typeof data === "object" && data !== null) {
69+
let sanitizedData = {};
6970
Object.keys(data).forEach((key) => {
70-
const item = data[key];
71+
let item = data[key];
7172
if(options?.allowedKeys && containsAllowedKey(item, options.allowedKeys)){
72-
return data;
73+
sanitizedData[key] = item;
7374
}
7475
if (typeof item === "string") {
75-
data[key] = noSQLSanitizer(item, level);
76+
sanitizedData[options.sanitizeKeys ? noSQLSanitizer(key, level) : key] = noSQLSanitizer(item, level);
7677
} else if (Array.isArray(item) || typeof item === "object") {
7778
try {
78-
data[key] = sanitize(item, level);
79+
sanitizedData[options.sanitizeKeys ? noSQLSanitizer(key, level) : key] = sanitize(item, options);
7980
} catch (error) {
80-
data[key] = item;
81+
sanitizedData[key] = item;
8182
}
8283
}
8384
});
85+
return sanitizedData;
8486
}
8587
return data;
8688
};

0 commit comments

Comments
 (0)