Common security mistakes in Liferay customizations
Liferay ships with a mature security model: permission checkers, role-based access, a service layer that enforces entitlements, and parameterized persistence. Most vulnerabilities I find in custom code come from quietly stepping around that model rather than from any flaw in the platform itself. The pattern is almost always the same: a developer reaches for the convenient shortcut and skips the guardrail.
Skipping the permission check
The most common mistake is fetching an entity by id and rendering it without verifying the current user is allowed to see it. The local service layer (LocalServiceUtil) deliberately does not check permissions, while the remote service layer (ServiceUtil) does. Reaching for the local service in a portlet action because it is one line shorter is how data leaks across organizations and sites.
// Wrong: local service performs no permission check
Document doc = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
// Right: use the permission-aware service, or check explicitly
Document doc = DLAppServiceUtil.getFileEntry(fileEntryId);
// Or guard the local call when you must use it
if (!permissionChecker.hasPermission(
groupId, DLFileEntry.class.getName(), fileEntryId, ActionKeys.VIEW)) {
throw new PrincipalException.MustHavePermission(userId, ActionKeys.VIEW);
}The mistakes I see most often
| Mistake | Why it is dangerous | The fix |
|---|---|---|
| Calling *LocalServiceUtil from a request handler | Bypasses permission enforcement entirely | Use *ServiceUtil or check PermissionChecker |
| Building SQL with string concatenation | SQL injection via user-supplied values | Use parameterized queries / Dynamic Query |
| Echoing parameters into JSP/JS without escaping | Stored or reflected XSS | HtmlUtil.escape / escapeJS on all output |
| Trusting hidden form fields and IDs | Forced browsing / IDOR | Re-validate ownership server-side |
| Secrets in source or portal-ext.properties | Credential leak through the repo | Externalize to a vault / env config |
Never build SQL by hand
When a custom finder needs something beyond the generated Service Builder methods, the temptation is to concatenate a query string. Any user-controlled value in that string is a SQL injection waiting to happen. Use parameterized queries or Liferay's DynamicQuery API, which binds values safely.
// Wrong: user input concatenated straight into SQL
String sql = "SELECT * FROM MyEntity WHERE name = '" + name + "'";
// Right: DynamicQuery with a bound parameter
DynamicQuery query = DynamicQueryFactoryUtil.forClass(MyEntity.class)
.add(RestrictionsFactoryUtil.eq("name", name));
List<MyEntity> results = myEntityLocalService.dynamicQuery(query);Escape everything you render
Cross-site scripting still tops the list of real-world findings. Any value that originated from a user (a comment, a display name, a query parameter reflected back) must be escaped at the point of output. Liferay gives you HtmlUtil.escape for HTML context and HtmlUtil.escapeJS for values dropped into a script block. Escaping on input is not enough; escape on output, in the context where the value lands.
Keep secrets out of the build
Database passwords, API keys, and signing secrets committed to the repository are the breach that keeps giving, because they survive in git history long after someone rotates the live value. Externalize them into environment configuration or a secrets manager, and reference them at runtime. Treat portal-ext.properties in version control as a red flag in code review.
Make the safe path the easy path
Security mistakes are rarely the result of ignorance; they are the result of friction. If using the permission-aware service is harder than the local one, people will choose the local one under deadline pressure. Wrap the correct patterns in small shared helpers, add a few static-analysis rules for the obvious offenders, and put a permission-and-injection checklist into every pull request. The goal is a codebase where doing the secure thing is also the path of least resistance.