Skip to content

Commit bd4b656

Browse files
Escape RegExp special characters
1 parent cc67fdb commit bd4b656

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

cloudwatchlogs-with-dlq/cloudwatchlogs_lambda.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function getConfig(env) {
8585
"includeSecurityGroupInfo": ("INCLUDE_SECURITY_GROUP_INFO" in env) ? env.INCLUDE_SECURITY_GROUP_INFO === "true" : false,
8686
// Regex to filter by logStream name prefixes
8787
"logStreamPrefixRegex": ("LOG_STREAM_PREFIX" in env)
88-
? new RegExp('^(' + env.LOG_STREAM_PREFIX.replace(/,/g, '|') + ')', 'i')
88+
? new RegExp('^(' + escapeRegExp(env.LOG_STREAM_PREFIX).replace(/,/g, '|') + ')', 'i')
8989
: ''
9090
};
9191
if (!config.SumoURL) {
@@ -99,6 +99,10 @@ function getConfig(env) {
9999
return config;
100100
}
101101

102+
function escapeRegExp(string) {
103+
return string.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&');
104+
}
105+
102106
function transformRecords(config, records) {
103107
return new Promise(function (resolve, reject) {
104108
if (config.LogFormat === "VPC-JSON" && config.includeSecurityGroupInfo) {

cloudwatchlogs/cloudwatchlogs_lambda.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var includeLogInfo = false; // default is false
2323

2424
// Regex to filter by logStream name prefixes
2525
var logStreamPrefixRegex = process.env.LOG_STREAM_PREFIX
26-
? new RegExp('^(' + process.env.LOG_STREAM_PREFIX.replace(/,/g, '|') + ')', 'i')
26+
? new RegExp('^(' + escapeRegExp(process.env.LOG_STREAM_PREFIX).replace(/,/g, '|') + ')', 'i')
2727
: '';
2828

2929
// Regex used to detect logs coming from lambda functions.
@@ -38,6 +38,9 @@ var https = require('https');
3838
var zlib = require('zlib');
3939
var url = require('url');
4040

41+
function escapeRegExp(string) {
42+
return string.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&');
43+
}
4144

4245
function sumoMetaKey(awslogsData, message) {
4346
var sourceCategory = '';

0 commit comments

Comments
 (0)