Skip to content

Commit 45e6201

Browse files
authored
fix(core): Validating null ttls in dotParse and dotCache directives (dotCMS#31984)
### Proposed Changes * TTL values are verified before a cast/parse to avoid NumberFormatExceptions in dotParse and dotCache Refs: dotCMS#31980
1 parent d74bc4c commit 45e6201

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

dotCMS/src/main/java/com/dotcms/rendering/velocity/directive/DotCacheDirective.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dotcms.rendering.velocity.directive;
22

3+
import com.dotmarketing.util.Logger;
34
import java.io.IOException;
45
import java.io.Serializable;
56
import java.io.StringWriter;
@@ -40,7 +41,14 @@ public boolean render(InternalContextAdapter context, Writer writer, Node node)
4041
HttpServletRequest request = (HttpServletRequest) context.get("request");
4142
boolean shouldCache = shouldCache(request);
4243
boolean refreshCache = refreshCache(request);
43-
final int ttl = (Integer) node.jjtGetChild(1).value(context);
44+
45+
int ttl = 0;
46+
Object ttlObj = node.jjtGetChild(1).value(context);
47+
if (ttlObj instanceof Integer) {
48+
ttl = (Integer) node.jjtGetChild(1).value(context);
49+
} else{
50+
Logger.debug(this.getClass(), "TTL value for #dotcache must be an Integer and cannot be null. Using ttl = 0");
51+
}
4452
if (!shouldCache || ttl <= 0) {
4553
node.jjtGetChild(2).render(context, writer);
4654
return true;

dotCMS/src/main/java/com/dotcms/rendering/velocity/directive/DotParse.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ private void throwNotResourceNotFoundException(final RenderParams params, final
204204
*/
205205
void afterRender(final String render, String[] arguments) {
206206
if (arguments.length > 1) {
207-
CacheLocator.getBlockDirectiveCache().add(getTTLCacheKey(arguments), Map.of(RENDER, render), Integer.parseInt(arguments[1]));
207+
try {
208+
int ttl = Integer.parseInt(arguments[1]);
209+
CacheLocator.getBlockDirectiveCache().add(getTTLCacheKey(arguments), Map.of(RENDER, render), ttl);
210+
} catch (NumberFormatException e) {
211+
Logger.debug(this.getClass(), "Invalid TTL value in dotParse: " + arguments[1]);
212+
}
208213
}
209214
}
210215

0 commit comments

Comments
 (0)