{"record":{"id":"db9c409cb7371002","repo":"apache/pulsar","slug":"invalid-policy-regex-policy","errorCode":null,"errorMessage":"invalid policy regex ${policy}","messagePattern":"invalid policy regex (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/NamespaceIsolationDataImpl.java","lineNumber":105,"sourceCode":"        return new NamespaceIsolationDataImplBuilder();\n    }\n\n    public void validate() {\n        checkArgument(namespaces != null && !namespaces.isEmpty() && primary != null && !primary.isEmpty()\n                && validateRegex(primary) && secondary != null && validateRegex(secondary)\n                && autoFailoverPolicy != null);\n        autoFailoverPolicy.validate();\n    }\n\n    private boolean validateRegex(List<String> policies) {\n        if (policies != null && !policies.isEmpty()) {\n            policies.forEach((policy) -> {\n                try {\n                    if (StringUtils.isNotBlank(policy)) {\n                        Pattern.compile(policy);\n                    }\n                } catch (PatternSyntaxException exception) {\n                    throw new IllegalArgumentException(\"invalid policy regex \" + policy);\n                }\n            });\n        }\n        return true;\n    }\n\n    public static class NamespaceIsolationDataImplBuilder implements NamespaceIsolationData.Builder {\n        private List<String> namespaces = new ArrayList<>();\n        private List<String> primary = new ArrayList<>();\n        private List<String> secondary = new ArrayList<>();\n        private AutoFailoverPolicyData autoFailoverPolicy;\n        private NamespaceIsolationPolicyUnloadScope unloadScope;\n\n        public NamespaceIsolationDataImplBuilder namespaces(List<String> namespaces) {\n            this.namespaces = namespaces;\n            return this;\n        }\n","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/NamespaceIsolationDataImpl.java#L87-L123","documentation":"NamespaceIsolationDataImpl.validate compiles every entry of the namespaces regex list with java.util.regex.Pattern. If any non-blank regex has invalid syntax (PatternSyntaxException), validate throws IllegalArgumentException('invalid policy regex <policy>'), rejecting the whole namespace-isolation policy before it is stored.","triggerScenarios":"Submitting a NamespaceIsolationData (admin API: setNamespaceIsolationPolicy, or pulsar-admin namespaces set-namespace-isolation-policy) whose 'namespaces' list contains an invalid regex, e.g. 'public/[a-z(' (unbalanced bracket), '*', or a stray '+' or backslash.","commonSituations":"Hand-written namespace regexes with unescaped dots/brackets or trailing operators; assuming shell-style globs ('tenant/ns/*') work instead of regex ('tenant/ns/.*'); copy-paste losing characters.","solutions":["Test the regex with Pattern.compile() (or any Java regex tester) before submitting; fix the syntax error reported by PatternSyntaxException.","Remember these are Java regexes, not globs: use '.*' not '*' for wildcards and escape '.' as '\\\\.' where literal.","Escape metacharacters ( [ ] ( ) * + ? \\ ^ $ | ) or wrap literal parts with Pattern.quote().","Remove or blank out broken list entries — blank entries are skipped, but syntactically invalid ones abort validation."],"exampleFix":"// before\npolicy.setNamespaces(List.of(\"public/default/*\"));      // glob, invalid Java regex intent\npolicy.setNamespaces(List.of(\"public/default/[\"));      // syntax error\n// after\npolicy.setNamespaces(List.of(\"public/default/.*\"));     // proper Java regex","handlingStrategy":"validation","validationCode":"static void validateNamespaceRegexes(java.util.Collection<String> namespaces) {\n    for (String p : namespaces) {\n        if (p != null && !p.isBlank()) {\n            try { java.util.regex.Pattern.compile(p); }\n            catch (java.util.regex.PatternSyntaxException e) {\n                throw new IllegalArgumentException(\"invalid policy regex \" + p, e);\n            }\n        }\n    }\n}\n// call before setNamespaceIsolationPolicy","typeGuard":"static boolean isValidRegex(String policy) {\n    if (policy == null || policy.isBlank()) return true;\n    try { java.util.regex.Pattern.compile(policy); return true; }\n    catch (java.util.regex.PatternSyntaxException e) { return false; }\n}","tryCatchPattern":"try {\n    admin.namespaces().setNamespaceIsolationPolicy(cluster, data);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"invalid policy regex\")) {\n        // fix or Pattern.quote() the offending regex in data.getNamespaces()\n    }\n    throw e;\n}","preventionTips":["Pre-compile every regex client-side before submitting the policy.","Remember Java regex semantics: '.*' for wildcards, not glob '*'; escape '.' and other metacharacters.","Use Pattern.quote() for literal namespace segments.","Keep a test that compiles all shipped default policy regexes."],"tags":["regex","validation","config"],"backgroundTag":"invalid-regex","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}