|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
64 | 64 | import com.thoughtworks.xstream.mapper.CannotResolveClassException;
|
65 | 65 | import com.thoughtworks.xstream.mapper.Mapper;
|
66 | 66 | import com.thoughtworks.xstream.mapper.MapperWrapper;
|
| 67 | +import com.thoughtworks.xstream.security.ForbiddenClassException; |
| 68 | +import com.thoughtworks.xstream.security.TypePermission; |
67 | 69 | import org.w3c.dom.Document;
|
68 | 70 | import org.w3c.dom.Element;
|
69 | 71 | import org.w3c.dom.Node;
|
|
106 | 108 | * Therefore, it has limited namespace support. As such, it is rather unsuitable for
|
107 | 109 | * usage within Web Services.
|
108 | 110 | *
|
109 |
| - * <p>This marshaller requires XStream 1.4.5 or higher, as of Spring 4.3. |
| 111 | + * <p>This marshaller requires XStream 1.4.7 or higher, as of Spring 5.2.17. |
110 | 112 | * Note that {@link XStream} construction has been reworked in 4.0, with the
|
111 | 113 | * stream driver and the class loader getting passed into XStream itself now.
|
112 | 114 | *
|
@@ -146,6 +148,9 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
|
146 | 148 | @Nullable
|
147 | 149 | private ConverterMatcher[] converters;
|
148 | 150 |
|
| 151 | + @Nullable |
| 152 | + private TypePermission[] typePermissions; |
| 153 | + |
149 | 154 | @Nullable
|
150 | 155 | private MarshallingStrategy marshallingStrategy;
|
151 | 156 |
|
@@ -268,6 +273,20 @@ public void setConverters(ConverterMatcher... converters) {
|
268 | 273 | this.converters = converters;
|
269 | 274 | }
|
270 | 275 |
|
| 276 | + /** |
| 277 | + * Set XStream type permissions such as |
| 278 | + * {@link com.thoughtworks.xstream.security.AnyTypePermission}, |
| 279 | + * {@link com.thoughtworks.xstream.security.ExplicitTypePermission} etc, |
| 280 | + * as an alternative to overriding the {@link #customizeXStream} method. |
| 281 | + * <p>Note: As of XStream 1.4.18, the default type permissions are |
| 282 | + * restricted to well-known core JDK types. For any custom types, |
| 283 | + * explicit type permissions need to be registered. |
| 284 | + * @since 5.2.17 |
| 285 | + */ |
| 286 | + public void setTypePermissions(TypePermission... typePermissions) { |
| 287 | + this.typePermissions = typePermissions; |
| 288 | + } |
| 289 | + |
271 | 290 | /**
|
272 | 291 | * Set a custom XStream {@link MarshallingStrategy} to use.
|
273 | 292 | * @since 4.0
|
@@ -407,7 +426,7 @@ public void setBeanClassLoader(ClassLoader classLoader) {
|
407 | 426 |
|
408 | 427 | @Override
|
409 | 428 | public void afterPropertiesSet() {
|
410 |
| - // no-op due to use of SingletonSupplier for the XStream field. |
| 429 | + // no-op due to use of SingletonSupplier for the XStream field |
411 | 430 | }
|
412 | 431 |
|
413 | 432 | /**
|
@@ -479,6 +498,12 @@ else if (this.converters[i] instanceof SingleValueConverter) {
|
479 | 498 | }
|
480 | 499 | }
|
481 | 500 |
|
| 501 | + if (this.typePermissions != null) { |
| 502 | + for (TypePermission permission : this.typePermissions) { |
| 503 | + xstream.addPermission(permission); |
| 504 | + } |
| 505 | + } |
| 506 | + |
482 | 507 | if (this.marshallingStrategy != null) {
|
483 | 508 | xstream.setMarshallingStrategy(this.marshallingStrategy);
|
484 | 509 | }
|
@@ -844,7 +869,7 @@ private Object doUnmarshal(HierarchicalStreamReader streamReader, @Nullable Data
|
844 | 869 | */
|
845 | 870 | protected XmlMappingException convertXStreamException(Exception ex, boolean marshalling) {
|
846 | 871 | if (ex instanceof StreamException || ex instanceof CannotResolveClassException ||
|
847 |
| - ex instanceof ConversionException) { |
| 872 | + ex instanceof ForbiddenClassException || ex instanceof ConversionException) { |
848 | 873 | if (marshalling) {
|
849 | 874 | return new MarshallingFailureException("XStream marshalling exception", ex);
|
850 | 875 | }
|
|
0 commit comments