Skip to content

Commit

Permalink
Escape RegExp special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ykitamura-mdsol committed Sep 11, 2019
1 parent cc67fdb commit bd4b656
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cloudwatchlogs-with-dlq/cloudwatchlogs_lambda.js
Expand Up @@ -85,7 +85,7 @@ function getConfig(env) {
"includeSecurityGroupInfo": ("INCLUDE_SECURITY_GROUP_INFO" in env) ? env.INCLUDE_SECURITY_GROUP_INFO === "true" : false,
// Regex to filter by logStream name prefixes
"logStreamPrefixRegex": ("LOG_STREAM_PREFIX" in env)
? new RegExp('^(' + env.LOG_STREAM_PREFIX.replace(/,/g, '|') + ')', 'i')
? new RegExp('^(' + escapeRegExp(env.LOG_STREAM_PREFIX).replace(/,/g, '|') + ')', 'i')
: ''
};
if (!config.SumoURL) {
Expand All @@ -99,6 +99,10 @@ function getConfig(env) {
return config;
}

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

function transformRecords(config, records) {
return new Promise(function (resolve, reject) {
if (config.LogFormat === "VPC-JSON" && config.includeSecurityGroupInfo) {
Expand Down
5 changes: 4 additions & 1 deletion cloudwatchlogs/cloudwatchlogs_lambda.js
Expand Up @@ -23,7 +23,7 @@ var includeLogInfo = false; // default is false

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

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

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

function sumoMetaKey(awslogsData, message) {
var sourceCategory = '';
Expand Down

0 comments on commit bd4b656

Please sign in to comment.