{"record":{"id":"6c2a6af45812003c","repo":"zaproxy/zaproxy","slug":"invalid-query","errorCode":null,"errorMessage":"Invalid query","messagePattern":"Invalid query","errorType":"exception","errorClass":"URIException","httpStatus":null,"severity":"error","filePath":"zap/src/main/java/org/apache/commons/httpclient/URI.java","lineNumber":2159,"sourceCode":"        String charset = getProtocolCharset();\n\n        /*\n         * Parse the query component.\n         * <p><blockquote><pre>\n         *  query     =  $7 = <undefined>\n         *                                        @@@@@@@@@\n         *  ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?\n         * </pre></blockquote><p>\n         */\n        if (0 <= at && at + 1 < length && tmp.charAt(at) == '?') {\n            int next = tmp.indexOf('#', at + 1);\n            if (next == -1) {\n                next = tmp.length();\n            }\n            if (escaped) {\n                _query = tmp.substring(at + 1, next).toCharArray();\n                if (!validate(_query, uric)) {\n                    throw new URIException(\"Invalid query\");\n                }\n            } else {\n                _query = encode(tmp.substring(at + 1, next), allowed_query, charset);\n            }\n            at = next;\n        }\n\n        /*\n         * Parse the fragment component.\n         * <p><blockquote><pre>\n         *  fragment  =  $9 = Related\n         *                                                   @@@@@@@@\n         *  ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?\n         * </pre></blockquote><p>\n         */\n        if (0 <= at && at + 1 <= length && tmp.charAt(at) == '#') {\n            if (at + 1 == length) { // empty fragment\n                _fragment = \"\".toCharArray();","sourceCodeStart":2141,"sourceCodeEnd":2177,"githubUrl":"https://github.com/zaproxy/zaproxy/blob/9d1970a436b1b189bfb588fc88864c80d9baf6a5/zap/src/main/java/org/apache/commons/httpclient/URI.java#L2141-L2177","documentation":"In escaped mode (true passed to the constructor / parseUriReference) the query string after '?' is validated against the allowed query character set (uric). If it contains characters that are neither allowed nor properly percent-escaped, URIException(\"Invalid query\") is thrown.","triggerScenarios":"new URI(\"http://host/path?q=a b\") or any query containing raw spaces, '<', '>', '\"', '{', '}', '|', '\\\\', '^', '`' or non-ASCII characters while the escaped=true constructor is used, so the library validates instead of encoding.","commonSituations":"Building URLs by string concatenation with unencoded user input or search terms; non-ASCII (e.g. Chinese/Cyrillic) query parameters pasted in; query strings copied from logs that were partially decoded; using the escaped constructor with a not-yet-escaped URL.","solutions":["Percent-encode the query parameters (URLEncoder.encode / URIUtil.encode) before constructing the URI, or use the non-escaped constructor so the library encodes them.","Replace raw spaces with %20 and encode non-ASCII characters in the query.","Verify you picked the right constructor variant (escaped vs unescaped) for your input."],"exampleFix":"// before\nURI uri = new URI(\"http://example.com/search?q=\" + term, true); // term = \"hello world\" -> Invalid query\n// after\nString encoded = java.net.URLEncoder.encode(term, \"UTF-8\");\nURI uri = new URI(\"http://example.com/search?q=\" + encoded, true);\n// or let the library encode:\nURI uri = new URI(\"http://example.com/search?q=\" + term, false);","handlingStrategy":"validation","validationCode":"String encodeQuery(String raw) throws java.io.UnsupportedEncodingException {\n    return java.net.URLEncoder.encode(raw, \"UTF-8\");\n}\n// encode every name and value BEFORE building the query string","typeGuard":"boolean isEscapedQuery(String q) {\n    return q != null && q.matches(\"[A-Za-z0-9-._~%!$&'()*+,;=:@/?#]*\");\n}","tryCatchPattern":"try {\n    URI uri = new URI(url, true);\n} catch (URIException e) {\n    if (\"Invalid query\".equals(e.getMessage())) {\n        // rebuild with URLEncoder.encode on each parameter and retry once\n    } else { throw e; }\n}","preventionTips":["Always encode query parameter names and values with URLEncoder.encode.","Use the unescaped URI constructor when your input is raw text.","Never concatenate raw user input into a query string."],"tags":["uri","query-string","escaping","url-encoding","httpclient"],"backgroundTag":"invalid-uri-query-characters","analyzedSha":"9d1970a436b1b189bfb588fc88864c80d9baf6a5","analyzedAt":"2026-09-05T19:26:59.356Z","contentChangedAt":"2026-09-05T19:26:59.356Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}