Skip to content

Commit 7d73d44

Browse files
committed
LPD-72782 Create a configuration to remove client's IP from Liferay object
1 parent 0e69ede commit 7d73d44

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* SPDX-FileCopyrightText: (c) 2025 Liferay, Inc. https://liferay.com
3+
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
4+
*/
5+
6+
package com.liferay.frontend.js.web.internal.configuration;
7+
8+
import aQute.bnd.annotation.metatype.Meta;
9+
10+
import com.liferay.portal.configuration.metatype.annotations.ExtendedObjectClassDefinition;
11+
12+
/**
13+
* @author Iván Zaera Avellón
14+
*/
15+
@ExtendedObjectClassDefinition(
16+
category = "infrastructure",
17+
scope = ExtendedObjectClassDefinition.Scope.COMPANY, strictScope = true
18+
)
19+
@Meta.OCD(
20+
id = "com.liferay.frontend.js.web.internal.configuration.LiferayGlobalObjectConfiguration",
21+
localization = "content/Language",
22+
name = "liferay-global-object-configuration-name"
23+
)
24+
public interface LiferayGlobalObjectConfiguration {
25+
26+
@Meta.AD(
27+
deflt = "false", description = "disable-get-remote-methods-help",
28+
name = "disable-get-remote-methods", required = false
29+
)
30+
public boolean disableGetRemoteMethods();
31+
32+
}

modules/apps/frontend-js/frontend-js-web/src/main/java/com/liferay/frontend/js/web/internal/servlet/taglib/LiferayGlobalObjectPreAUIDynamicInclude.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
package com.liferay.frontend.js.web.internal.servlet.taglib;
77

88
import com.liferay.exportimport.kernel.staging.Staging;
9+
import com.liferay.frontend.js.web.internal.configuration.LiferayGlobalObjectConfiguration;
910
import com.liferay.layout.seo.kernel.LayoutSEOLink;
1011
import com.liferay.layout.seo.kernel.LayoutSEOLinkManager;
1112
import com.liferay.petra.string.CharPool;
1213
import com.liferay.petra.string.StringBundler;
1314
import com.liferay.petra.string.StringPool;
15+
import com.liferay.portal.configuration.metatype.bnd.util.ConfigurableUtil;
16+
import com.liferay.portal.configuration.module.configuration.ConfigurationProvider;
1417
import com.liferay.portal.kernel.content.security.policy.ContentSecurityPolicyNonceProviderUtil;
1518
import com.liferay.portal.kernel.exception.PortalException;
1619
import com.liferay.portal.kernel.feature.flag.FeatureFlag;
@@ -23,6 +26,7 @@
2326
import com.liferay.portal.kernel.model.LayoutTypePortlet;
2427
import com.liferay.portal.kernel.model.User;
2528
import com.liferay.portal.kernel.model.impl.VirtualLayout;
29+
import com.liferay.portal.kernel.module.configuration.ConfigurationException;
2630
import com.liferay.portal.kernel.security.auth.AuthToken;
2731
import com.liferay.portal.kernel.security.permission.ActionKeys;
2832
import com.liferay.portal.kernel.service.permission.LayoutPermission;
@@ -63,6 +67,7 @@
6367
import java.text.Format;
6468
import java.text.SimpleDateFormat;
6569

70+
import java.util.Collections;
6671
import java.util.Locale;
6772
import java.util.Map;
6873
import java.util.TimeZone;
@@ -87,6 +92,9 @@ public void include(
8792
HttpServletResponse httpServletResponse, String key)
8893
throws IOException {
8994

95+
LiferayGlobalObjectConfiguration liferayGlobalObjectConfiguration =
96+
_getLiferayGlobalObjectConfiguration(httpServletRequest);
97+
9098
PrintWriter printWriter = httpServletResponse.getWriter();
9199

92100
printWriter.print("<script");
@@ -107,7 +115,8 @@ public void include(
107115
_renderLiferayPortlet(sb);
108116
_renderLiferayPortletKeys(sb);
109117
_renderLiferayPropsValues(httpServletRequest, sb);
110-
_renderLiferayThemeDisplay(httpServletRequest, sb);
118+
_renderLiferayThemeDisplay(
119+
httpServletRequest, liferayGlobalObjectConfiguration, sb);
111120
_renderLiferayUtil(sb);
112121

113122
_renderValue(
@@ -210,6 +219,35 @@ else if (dayIndex < monthIndex) {
210219
return dateFormatPattern;
211220
}
212221

222+
private LiferayGlobalObjectConfiguration
223+
_getLiferayGlobalObjectConfiguration(
224+
HttpServletRequest httpServletRequest) {
225+
226+
LiferayGlobalObjectConfiguration liferayGlobalObjectConfiguration;
227+
228+
long companyId = _portal.getCompanyId(httpServletRequest);
229+
230+
try {
231+
liferayGlobalObjectConfiguration =
232+
_configurationProvider.getCompanyConfiguration(
233+
LiferayGlobalObjectConfiguration.class, companyId);
234+
}
235+
catch (ConfigurationException configurationException) {
236+
if (_log.isWarnEnabled()) {
237+
_log.warn(
238+
"Using default configuration for company " + companyId,
239+
configurationException);
240+
}
241+
242+
liferayGlobalObjectConfiguration =
243+
ConfigurableUtil.createConfigurable(
244+
LiferayGlobalObjectConfiguration.class,
245+
Collections.emptyMap());
246+
}
247+
248+
return liferayGlobalObjectConfiguration;
249+
}
250+
213251
private void _renderLiferayAUI(
214252
HttpServletRequest httpServletRequest, StringBundler sb) {
215253

@@ -499,7 +537,9 @@ private void _renderLiferayPropsValues(
499537
}
500538

501539
private void _renderLiferayThemeDisplay(
502-
HttpServletRequest httpServletRequest, StringBundler sb)
540+
HttpServletRequest httpServletRequest,
541+
LiferayGlobalObjectConfiguration liferayGlobalObjectConfiguration,
542+
StringBundler sb)
503543
throws PortalException {
504544

505545
sb.append("ThemeDisplay: {\n");
@@ -581,8 +621,11 @@ private void _renderLiferayThemeDisplay(
581621
_renderMethod("getPlid", sb, themeDisplay.getPlid());
582622
_renderMethod("getPortalURL", sb, themeDisplay.getPortalURL());
583623
_renderMethod("getRealUserId", sb, themeDisplay.getRealUserId());
584-
_renderMethod("getRemoteAddr", sb, themeDisplay.getRemoteAddr());
585-
_renderMethod("getRemoteHost", sb, themeDisplay.getRemoteHost());
624+
625+
if (!liferayGlobalObjectConfiguration.disableGetRemoteMethods()) {
626+
_renderMethod("getRemoteAddr", sb, themeDisplay.getRemoteAddr());
627+
_renderMethod("getRemoteHost", sb, themeDisplay.getRemoteHost());
628+
}
586629

587630
Group scopeGroup = themeDisplay.getScopeGroup();
588631

@@ -751,6 +794,9 @@ else if (value instanceof String) {
751794
@Reference
752795
private AuthToken _authToken;
753796

797+
@Reference
798+
private ConfigurationProvider _configurationProvider;
799+
754800
private final Map<Locale, String> _displayNames = new ConcurrentHashMap<>();
755801

756802
@Reference

modules/apps/portal-language/portal-language-lang/src/main/resources/content/Language.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6220,6 +6220,8 @@ disable-as-a-collection-provider-help=Disabling this as a collection provider wi
62206220
disable-caching=Disable Caching
62216221
disable-document-recording=Disable Document Recording
62226222
disable-forwarding=Disable Forwarding
6223+
disable-get-remote-methods=Disable getRemoteAddr() and getRemoteHost() methods
6224+
disable-get-remote-methods-help=If checked, the methods getRemoteAddr() and getRemoteHost() will not be defined in the Liferay.ThemeDisplay global object. This may be useful if you are worried about revealing personal information or if you want to make pages for unauthenticated users publicly cacheable. However, it will make any code you deploy (that uses these methods) fail.
62236225
disable-globally=Disable Multi-Factor Authentication
62246226
disable-globally-description=Disable multi-factor authentication system-wide.
62256227
disable-inheritance-confirmation=Disable Inheritance Confirmation
@@ -10778,6 +10780,7 @@ licenses-registered=Licenses Registered
1077810780
liferay=Liferay
1077910781
liferay-analytics-key=Liferay Analytics Key
1078010782
liferay-dxp-instance-has-to-be-connected-with-analytics-cloud-to-view-content-performance-metrics-and-build-a-successful-content-strategy=In order to view content performance metrics and build a successful content strategy, your Liferay DXP instance has to be connected with Liferay Analytics Cloud.
10783+
liferay-global-object-configuration-name=Liferay Global Object
1078110784
liferay-has-failed-to-connect-to-the-ldap-server=Liferay has failed to connect to the LDAP server. Please check your configuration and verify that the LDAP server is running.
1078210785
liferay-has-failed-to-connect-to-the-opensso-server=Liferay has failed to connect to the OpenSSO server. Please check your configuration and verify that the OpenSSO server is running.
1078310786
liferay-has-failed-to-connect-to-the-opensso-services=Liferay has failed to connect to the OpenSSO services. Please verify that the OpenSSO services are running.

0 commit comments

Comments
 (0)