Skip to content

PutDataProtectionPolicy fails with "Regexes in Custom Data Identifiers are too complex" even though the regex is under 200 characters

0

I'm applying a CloudWatch Logs data protection policy that uses custom data identifiers. PutDataProtectionPolicy fails with:

InvalidParameterException: Regexes in Custom Data Identifiers are too complex.

Each regex is under the documented 200-character limit and uses only the allowed characters, and the policy has fewer than 10 custom identifiers. The error does not say which identifier is the problem. What makes a custom data identifier regex "too complex", and how do I find the offending one?

An example of a pattern that was rejected (it matches a URL that carries credentials or a query string):

[Hh][Tt][Tt][Pp][Ss]?.//[^ /?@]{1,80}@[^ ]{1,60}|[Hh][Tt][Tt][Pp][Ss]?.//[^ ?]{1,150}\?[^ ]{1,100}

asked a month ago105 views

1 Answer
0
Accepted Answer

The documented limits (10 identifiers per policy, 200 characters per regex, restricted character set) are not the only constraint. The service also rejects regexes it considers too complex, and that limit is not documented. In my case the cause was large counted repeats over negated character classes.

What I found

I put each custom identifier into its own policy and called PutDataProtectionPolicy on a scratch log group. Of ten patterns, eight were accepted and two were rejected. Both rejected patterns had large bounded repeats over negated classes (a class like [^ ?] matches "anything except these characters"):

  • [^ /?@]{1,80}, [^ ]{1,60}, [^ ?]{1,150}, [^ ]{1,100}
  • [^ ,;]{6,40} repeated across three alternatives

The accepted patterns used small repeats (up to {1,16} or {4,20}), plus {3,40} over a short letters-and-digits class.

The fix

Replacing the bounded repeats with open-ended ones made both patterns pass, alone and combined with the rest of the policy:

# before (rejected)
[Hh][Tt][Tt][Pp][Ss]?.//[^ /?@]{1,80}@[^ ]{1,60}|[Hh][Tt][Tt][Pp][Ss]?.//[^ ?]{1,150}\?[^ ]{1,100}

# after (accepted)
[Hh][Tt][Tt][Pp][Ss]?.//[^ /?@]+@[^ ]+|[Hh][Tt][Tt][Pp][Ss]?.//[^ ?]+\?[^ ]+

For the token pattern, [^ ,;]{6,40} (three alternatives, each followed by other bounded repeats) became [^ ,;]{6,}. Open-ended repeats also mask values longer than the old cap, instead of cutting them off.

What the service actually limits (measured)

I ran controlled experiments on a scratch log group: one regex per policy, changing one thing at a time. Results below are from one account in us-east-1; the service may change over time.

Pattern shapeAcceptedRejected
Id[A-Za-z]{1,N} (also \w and [A-Za-z0-9._@-])N up to 120N of 126 and above
Id[^ ]{1,N} (also [^ ,;], [^0-9/], ., \S)N up to 31N of 32 and above
Id[^ ]+ and Id[^ ]{6,}yes
Id[^ ]{30,} and Id[^ ]{100,}rejected
Id[A-Za-z]{130,}yes
128 or 129 literal charactersyes

Findings:

  • The limit applies to each regex, not to the whole policy. Ten identifiers that each passed on their own were accepted together.
  • Alternatives (a|b|c) do not add up: ten alternatives of [^ ]{1,10} were accepted. Repeats that follow each other inside one alternative do add up: Id[^ ]{1,20}Ab[^ ]{1,20} was rejected even though each half is fine alone.
  • Negated classes, . and \S use roughly four times as much of the budget as plain ASCII classes. That is consistent with them matching multi-byte characters, but I can't confirm the reason.
  • Combined bounded repeats in one alternative stay under about 120 "ASCII units": [A-Za-z]{1,60}[^ ]{1,16} (60 + 16 x 4 = 124) was accepted, [A-Za-z]{1,60}[^ ]{1,20} (140) was rejected.
  • Open-ended repeats (+, *) and {n,} over ASCII classes were accepted at every size I tried. A large minimum over a negated class ({30,}) was still rejected.
  • Literal text is not the problem; the 200-character regex limit still applies.
  • \x3A, \x22 and \x27 escapes are accepted, so you can write a colon or quote that way.

This is empirical. AWS does not publish the engine or the limit, so treat the numbers as observed behavior rather than a guarantee. I also tried ranking patterns with Google's RE2 and it did not predict the service's decisions, so I would not rely on that.

How to find the offending identifier

The error does not name the identifier, so test them one at a time against a scratch log group:

  1. Create a throwaway log group.
  2. For each custom identifier, write a policy containing only that identifier. FindingsDestination can be empty for a test:
{
  "Name": "scratch-test",
  "Version": "2021-06-01",
  "Configuration": {
    "CustomDataIdentifier": [
      { "Name": "MyIdentifier", "Regex": "YOUR_REGEX_HERE" }
    ]
  },
  "Statement": [
    {
      "Sid": "AuditStatement",
      "DataIdentifier": ["MyIdentifier"],
      "Operation": { "Audit": { "FindingsDestination": {} } }
    },
    {
      "Sid": "DeidentifyStatement",
      "DataIdentifier": ["MyIdentifier"],
      "Operation": { "Deidentify": { "MaskConfig": {} } }
    }
  ]
}
  1. Apply it with aws logs put-data-protection-policy --log-group-identifier <scratch-log-group> --policy-document file://policy.json and note which ones are rejected.
  2. Remove the policy with aws logs delete-data-protection-policy --log-group-identifier <scratch-log-group> and delete the scratch log group when done.

Test the combined policy once the identifiers pass individually, because the complexity check may also apply to the policy as a whole.

Other constraints that shaped the patterns

  • No parentheses, so no groups and no (?i); use character classes like [Hh][Tt][Tt][Pp] for case-insensitivity.
  • The colon is not in the allowed character set. Matching https:// worked with . in place of the colon (https?.//), or with the \x3A escape.
  • A regex must be at most 200 characters, so each alternative repeats its tail; plan for that when you have several labels.

I hope AWS adds the complexity limit to the custom data identifier documentation.

answered a month ago

EXPERT

reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.