Class UrlUtil
- java.lang.Object
-
- com.vaadin.util.UrlUtil
-
public class UrlUtil extends Object
FOR INTERNAL USE ONLY! This class is not considered a part of the public API.Internal utility class for URL handling.
---
Notes about safe URL schemes:
In regular applications and integration tests the default population logic of the cached set of safe URL schemes is triggered at launch (in
VaadinService.init()), but for unit tests there is no built-in pre-population. If the cached set has not been populated, and there is no valid result fromVaadinService.getCurrent(),isSafeUrl(String)will fall back to using the default wildcard, but also logs a warning. The fallback could theoretically also happen in a regular application, but only if some thread manages to callisSafeUrl(String)before theVaadinService.init()has been completed. If some application actually needs to create anExternalResourcein such a thread, it should use a constructor with the validation bypass, and the URL validation should be done via some custom means instead rather than relying on this class.Also, in regular applications the set of safe URL schemes cached within this class should stay stable once it's been populated. It is not recommended to update the set on the fly, and re-triggering
VaadinService.init()orAbstractDeploymentConfiguration.getUrlSafeSchemes()will not update it. If a different set is needed for some specific purpose, that should again bypass the built-in validation and use some custom validation instead. If changes to the cached set are needed regardless (within the framework tests), they need to happen viasetUrlSafeSchemes(Set), either by entering the new set directly, or entering an empty set or null for triggering a re-evaluation of the application property the next timeisSafeUrl(String)is called.Note: If a test needs to modify the cached set, that can and will affect the other tests within the same run, particularly if they are run in parallel. Test classes like that should be annotated with net.jcip.annotations.NotThreadSafe, and they should clean after themselves.
- Since:
- See Also:
AbstractDeploymentConfiguration.getUrlSafeSchemes()
-
-
Constructor Summary
Constructors Constructor Description UrlUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetUnsafeUrlMessage(String type, String url, String unsafeMethod)FOR INTERNAL USE ONLY!static Set<String>getUrlSafeSchemes()FOR INTERNAL USE ONLY!static booleanisSafeUrl(String url)FOR INTERNAL USE ONLY!static booleanisSafeUrl(String url, Set<String> safeSchemes)FOR INTERNAL USE ONLY!static voidsetUrlSafeSchemes(Set<String> urlSafeSchemes)FOR INTERNAL USE ONLY!
-
-
-
Method Detail
-
getUrlSafeSchemes
public static Set<String> getUrlSafeSchemes()
FOR INTERNAL USE ONLY!Returns the current set of safe URL schemes that is used by the
isSafeUrl(String)check. The set is nevernull, but it can be empty, and it can be explicitly cleared viasetUrlSafeSchemes(Set).Every call of this method returns the same
Setinstance, unless it gets replaced via another call ofsetUrlSafeSchemes(Set). TheSetreturned by this method is never the same instance as theSetgiven to the setter, but an unmodifiable copy with the same contents.The default population logic of this set (triggered in
VaadinService.init()) is based on the "safeUrlSchemes" application property, and will fall back to wildcard if the property hasn't been set.- Returns:
- unmodifiable set of current safe URL schemes
-
setUrlSafeSchemes
public static void setUrlSafeSchemes(Set<String> urlSafeSchemes)
FOR INTERNAL USE ONLY! Populates the current set of safe URL schemes.If the given set is null or empty,
getUrlSafeSchemes()will return a new empty set. Otherwise the previous set will be replaced with a new unmodifiable set which only contains the given schemes.The set received from
getUrlSafeSchemes()will never be the same instance as the set given to this method.- Parameters:
urlSafeSchemes- a set of safe URL schemes
-
isSafeUrl
public static boolean isSafeUrl(String url)
FOR INTERNAL USE ONLY!Checks whether the scheme of the given URL is considered safe by the current deployment configuration.
The set of safe schemes is read from the current
VaadinService'sDeploymentConfiguration.getUrlSafeSchemes(), falling back toConstants.URL_SAFE_SCHEMES_WILDCARDwhen noVaadinServiceis available. AnullURL is always considered unsafe, even if the rest of the validation can get bypassed via a wildcard. Relative URLs (without a scheme) are always considered safe, whereas URLs containing control characters are rejected (unless allowed via a wildcard) as they can be used to obfuscate the scheme.- Parameters:
url- the URL to check, may benull- Returns:
trueif the URL is safe,falseotherwise
-
getUnsafeUrlMessage
public static String getUnsafeUrlMessage(String type, String url, String unsafeMethod)
FOR INTERNAL USE ONLY!Builds the message for the
IllegalArgumentExceptionthat a validating URL setter throws when given a URL whose scheme is not considered safe. The message points to both theConstants.URL_SAFE_SCHEMESconfiguration property and the setter that bypasses validation.- Parameters:
type- the kind of URL being set, for example"src"or"path"url- the rejected URLunsafeMethod- the signature of the method that bypasses validation, for example"openUnsafe(String, String)"or"new ExternalResource(String, true)"- Returns:
- the exception message
-
isSafeUrl
public static boolean isSafeUrl(String url, Set<String> safeSchemes)
FOR INTERNAL USE ONLY!Checks whether the scheme of the given URL is part of the given set of safe schemes. See
isSafeUrl(String)for the validation rules. AnullURL is always considered unsafe.- Parameters:
url- the URL to check, may benullsafeSchemes- the set of safe lower-case schemes, or a set containingConstants.URL_SAFE_SCHEMES_WILDCARDto treat any non-null scheme as safe- Returns:
trueif the URL is safe,falseotherwise
-
-