Hi @gunars_v ,
In a previous project, we had a requirement where a trusted external service needed to consume structured content from AEM in JSON format. The initial idea was to expose /bin/querybuilder.json, but we quickly flagged the risks — unrestricted access, no input validation, and potential for performance hits.
Solution:
1. Custom Sling Servlet (Wrapper around QueryBuilder)
A safer way is to build a custom servlet that:
- Accepts only whitelisted query parameters (e.g., q, path)
- Hardcodes safe values for predicates like type, path, p.limit
- Filters out risky options like orderby, nodename, or deep traversals
- Controls and sanitizes the response (you can choose what fields to include)
Example predicate setup inside the servlet:
predicateMap.put("path", "/content/site-a");
predicateMap.put("type", "cq:Page");
predicateMap.put("p.limit", "10");
predicateMap.put("fulltext", searchTerm);
Dispatcher Configuration
Expose only this custom endpoint — not the default /bin/querybuilder.json.
/0100 {
/type "allow"
/method "GET"
/url "/bin/secure/search"
/extension "json"
/ip "203.0.113.10" # IP of the trusted third-party system
}
If IP restriction isn’t possible, consider mTLS or token-based access.
Extra Safety Steps That Helped:
Rate limiting at CDN or load balancer to avoid abuse
Input sanitization for all parameters — don’t trust anything from the request
Audit logging of each request (query string, IP, timestamp)
Error handling with meaningful HTTP status codes
Regards,
Amit