Skip to content

Commit 8fed78c

Browse files
committed
WIP: Sanitization function that keeps track of parentheses depth
1 parent b9cf532 commit 8fed78c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/logger.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ const LOG = cds.log('graphql')
66

77
const _isEmptyObject = o => Object.keys(o).length === 0
88

9+
const _sanitizeArguments = query => {
10+
let result = ''
11+
let depth = 0
12+
13+
for (const char of query) {
14+
if (char === '(') {
15+
if (depth === 0) result += '( '
16+
depth++
17+
} else if (char === ')') {
18+
depth--
19+
if (depth === 0) result += '*** )'
20+
} else {
21+
if (depth === 0) result += char
22+
}
23+
}
24+
25+
return result
26+
}
27+
928
const queryLogger = (req, _, next) => {
1029
let query = req.body?.query || (req.query.query && decodeURIComponent(req.query.query))
1130
// Only log requests that contain a query
@@ -41,7 +60,7 @@ const queryLogger = (req, _, next) => {
4160
// If query is multiline string, add newline padding to front
4261
let formattedQuery = query.includes('\n') ? `\n${query}` : query
4362
// Sanitize all values between parentheses
44-
if (IS_PRODUCTION) formattedQuery = formattedQuery.replace(/\([\s\S]*?\)/g, '( *** )')
63+
if (IS_PRODUCTION) formattedQuery = _sanitizeArguments(formattedQuery)
4564

4665
// Don't log undefined values
4766
LOG.info(...[req.method, formattedQueryInfo, formattedQuery].filter(e => e))

0 commit comments

Comments
 (0)