regexrubular

Removing sensitive informations from the logs using regex


In my Ruby app I have the following regex that helps me with removing sensitive informations from logs:

/(\\"|")secure[^:]+:\s*\1.*?\1/

It works when in logs are the following information:

{"secure_data": "Test"}

but when instead of string I have object in logs it does not work:

{"secure_data": {"name": "Test"}}

How can I update regex to work with both scenarios?

https://rubular.com/r/h9EBZot1e7NUkS


Solution

  • You may use this regex with negated character classes and an alternation:

    "secure[^:]+:\s*(?:"[^"]*"|{[^}]*})
    

    Inside non-capturing group (?:"[^"]*"|{[^}]*}) we are matching a quoted string or an object that starts with { and ends with }.

    Update RegEx Demo