From def78f44237897763539360773f311c9216dde91 Mon Sep 17 00:00:00 2001 From: Daniel DeGroff Date: Wed, 22 Jan 2025 11:00:58 -0700 Subject: [PATCH] Reduce noise from fuzzing, or various unexpected inputs that may cause message lookup to fail when no action invocation exists. --- .../l10n/ResourceBundleMessageProvider.java | 11 +++++--- src/test/java/messages/package.properties | 7 +++-- .../ResourceBundleMessageProviderTest.java | 27 ++++++++++++++++++- src/test/web/messages/package.properties | 5 +++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProvider.java b/src/main/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProvider.java index 7f00ce6c..beae49aa 100644 --- a/src/main/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProvider.java +++ b/src/main/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProvider.java @@ -79,7 +79,8 @@ public String getMessage(String key, Object... values) throws MissingMessageExce String message = getOptionalMessage(key, values); if (message == null) { ActionInvocation actionInvocation = invocationStore.getCurrent(); - throw new MissingMessageException("Message could not be found for the URI [" + actionInvocation.actionURI + "] and key [" + key + "]"); + String uri = actionInvocation != null ? actionInvocation.actionURI : null; + throw new MissingMessageException("Message could not be found for the URI [" + uri + "] and key [" + key + "]"); } return message; @@ -96,7 +97,8 @@ public String getOptionalMessage(String key, Object... values) { if (template == null) { if (!"[ValidationException]".equals(key)) { - logger.debug("Message could not be found for the URI [{}] and key [{}]", actionInvocation.actionURI, key); + String uri = actionInvocation != null ? actionInvocation.actionURI : null; + logger.debug("Message could not be found for the URI [{}] and key [{}]", uri, key); } return null; @@ -131,12 +133,13 @@ protected Queue determineBundles(String bundle) { * @return The message or null if it doesn't exist. */ protected String findMessage(ActionInvocation actionInvocation, String key) { - String message = findMessage(actionInvocation.actionURI, key); + String actionURI = actionInvocation != null ? actionInvocation.actionURI : "/"; + String message = findMessage(actionURI, key); if (message != null) { return message; } - ActionConfiguration config = actionInvocation.configuration; + ActionConfiguration config = actionInvocation != null ? actionInvocation.configuration : null; if (config == null) { return null; } diff --git a/src/test/java/messages/package.properties b/src/test/java/messages/package.properties index f0ba8a43..91254544 100644 --- a/src/test/java/messages/package.properties +++ b/src/test/java/messages/package.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2001-2007, Inversoft Inc., All Rights Reserved +# Copyright (c) 2001-2025, Inversoft Inc., All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,4 +14,7 @@ # language governing permissions and limitations under the License. # key=Super Package Message -format_key=Super Package Message %s %s %s \ No newline at end of file +format_key=Super Package Message %s %s %s + +# Default messages +[blank]=Required diff --git a/src/test/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProviderTest.java b/src/test/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProviderTest.java index e0087ec9..0960b139 100644 --- a/src/test/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProviderTest.java +++ b/src/test/java/org/primeframework/mvc/message/l10n/ResourceBundleMessageProviderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001-2024, Inversoft Inc., All Rights Reserved + * Copyright (c) 2001-2025, Inversoft Inc., All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,6 +142,31 @@ public void missing() { verify(store); } + @Test + public void noActionInvocation() { + HTTPContext context = new HTTPContext(Path.of("src/test/java")); + ActionInvocationStore store = createStrictMock(ActionInvocationStore.class); + expect(store.getCurrent()).andReturn(null).times(3); + replay(store); + + LocaleProvider localeProvider = createStrictMock(LocaleProvider.class); + expect(localeProvider.get()).andReturn(Locale.US).anyTimes(); + replay(localeProvider); + + ResourceBundleMessageProvider provider = new ResourceBundleMessageProvider(localeProvider, new WebControl(new ServletContainerResolver(context), configuration), store); + assertEquals(provider.getMessage("[blank]foo.bar"), "Required"); + + // Really missing + try { + provider.getMessage("[not_found]bar"); + fail("Should have failed"); + } catch (MissingMessageException e) { + // Expected + } + + verify(store); + } + @Test public void search() { HTTPContext context = new HTTPContext(Path.of("src/test/java")); diff --git a/src/test/web/messages/package.properties b/src/test/web/messages/package.properties index 8264ebba..b41035c1 100644 --- a/src/test/web/messages/package.properties +++ b/src/test/web/messages/package.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2001-2022, Inversoft Inc., All Rights Reserved +# Copyright (c) 2001-2025, Inversoft Inc., All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -23,3 +23,6 @@ format_key=Super Package Message %s %s %s [UnsupportedContentType]=Unsupported [Content-Type] HTTP request header value of [%s]. [invalidJSON]=Unable to parse JSON. The property [%s] was invalid. The error was [%s]. The detailed exception was [%s]. + +# Default messages +[blank]=Required