{"record":{"id":"6e94c82ef9ef21f9","repo":"grpc/grpc-java","slug":"invalid-regex-pattern-for","errorCode":null,"errorMessage":"Invalid regex pattern for : ","messagePattern":"Invalid regex pattern for : ","errorType":"exception","errorClass":"HeaderMutationRulesParseException","httpStatus":null,"severity":"error","filePath":"xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutationRulesParser.java","lineNumber":51,"sourceCode":"    builder.disallowAll(proto.getDisallowAll().getValue());\n    builder.disallowIsError(proto.getDisallowIsError().getValue());\n    if (proto.hasAllowExpression()) {\n      builder.allowExpression(\n          parseRegex(proto.getAllowExpression().getRegex(), \"allow_expression\"));\n    }\n    if (proto.hasDisallowExpression()) {\n      builder.disallowExpression(\n          parseRegex(proto.getDisallowExpression().getRegex(), \"disallow_expression\"));\n    }\n    return builder.build();\n  }\n\n  private static Pattern parseRegex(String regex, String fieldName)\n      throws HeaderMutationRulesParseException {\n    try {\n      return Pattern.compile(regex);\n    } catch (PatternSyntaxException e) {\n      throw new HeaderMutationRulesParseException(\n          \"Invalid regex pattern for \" + fieldName + \": \" + e.getMessage(), e);\n    }\n  }\n}\n","sourceCodeStart":33,"sourceCodeEnd":56,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutationRulesParser.java#L33-L56","documentation":"The HeaderMutationRulesParser compiles regex fields from xDS HeaderMutation rules into Java Patterns. When a regex fails to compile (PatternSyntaxException), it wraps the failure in HeaderMutationRulesParseException with the field name and the underlying regex error. This fails fast on malformed xDS configuration.","triggerScenarios":"Parsing xDS HeaderMutation rules whose regex field (e.g., allowed_regular_expressions) contains an invalid Java-flavored regex, such as unbalanced brackets, bad escapes, or PCRE-only syntax.","commonSituations":"Configs authored for Envoy (RE2 syntax) containing constructs Java regex rejects (e.g., possessive quantifiers, named-group syntax differences, stray backslashes in YAML).","solutions":["Fix the regex so it compiles as a Java Pattern — use the message from e.getMessage() to locate the syntax error","Test the regex with Pattern.compile() locally before embedding in config","Escape metacharacters correctly (double backslashes in YAML/JSON strings)","Rewrite RE2/PCRE-specific constructs into Java-compatible equivalents"],"exampleFix":"// before\nparseRegex(\"[a-z+\", \"allowed_regular_expressions\"); // unclosed class\n// after\nparseRegex(\"[a-z]+\", \"allowed_regular_expressions\");","handlingStrategy":"try-catch","validationCode":"try {\n  java.util.regex.Pattern.compile(regex);\n} catch (PatternSyntaxException e) {\n  throw new ConfigException(\"Invalid regex in rules: \" + e.getMessage());\n}","typeGuard":null,"tryCatchPattern":"try {\n  HeaderMutationRules.parse(rules);\n} catch (HeaderMutationRulesParseException e) {\n  log.error(\"Bad regex in field {}: {}\", e.getMessage());\n}","preventionTips":["Test all regexes with Pattern.compile before embedding in config","Double-escape backslashes in JSON/YAML config strings","Prefer simple RE2-compatible patterns that are also valid Java regex","Validate xDS configs at load time, not runtime"],"tags":["grpc","xds","regex","config-parsing"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}