| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.parser.resources.impl; |
| 23 |
|
| 24 |
import java.net.URI; |
| 25 |
import java.net.URISyntaxException; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.logging.Logger; |
| 31 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 32 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 33 |
import com.github.weisj.jsvg.parser.resources.ResourcePolicy; |
| 34 |
|
| 35 |
public class DefaultResourcePolicy implements ResourcePolicy { |
| 36 |
private static final Logger LOGGER = LogFactory.createLogger(DefaultResourcePolicy.class); |
| 37 |
|
| 38 |
public static final int FLAG_ALLOW_RELATIVE = 1; |
| 39 |
public static final int FLAG_ALLOW_ABSOLUTE = 1 << 1; |
| 40 |
public static final int FLAG_ALLOW_NON_LOCAL = 1 << 2; |
| 41 |
public static final int FLAG_ALLOW_EMBEDDED_DATA = 1 << 3; |
| 42 |
|
| 43 |
private final int flags; |
| 44 |
|
| 45 |
public DefaultResourcePolicy(int flags) { |
| 46 |
this.flags = flags; |
| 47 |
} |
| 48 |
|
| 49 |
public boolean allowsExternalResources() { |
| 50 |
return (flags & FLAG_ALLOW_RELATIVE) != 0 || (flags & FLAG_ALLOW_ABSOLUTE) != 0; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public @Nullable URI resolveResourceURI(@Nullable URI baseURI, @NotNull String path) { |
| 55 |
try { |
| 56 |
return resolveResourceURI(baseURI, new URI(cleanup(path))); |
| 57 |
} catch (URISyntaxException e) { |
| 58 |
LOGGER.log(Level.INFO, "Failed to resolve URI: " + path); |
| 59 |
return null; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private static @NotNull String cleanup(@NotNull String path) { |
| 64 |
if (path.startsWith("data")) { |
| 65 |
return path.replace(" ", ""); |
| 66 |
} |
| 67 |
return path; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public @Nullable URI resolveResourceURI(@Nullable URI baseDocumentUri, @NotNull URI resourceUri) { |
| 72 |
if ("data".equals(resourceUri.getScheme())) { |
| 73 |
if ((flags & FLAG_ALLOW_EMBEDDED_DATA) == 0) { |
| 74 |
LOGGER.log(Level.INFO, |
| 75 |
() -> String.format("Rejected URI %s because embedded data is not allowed", resourceUri)); |
| 76 |
return null; |
| 77 |
} |
| 78 |
return resourceUri; |
| 79 |
} else if (resourceUri.isAbsolute()) { |
| 80 |
if ((flags & FLAG_ALLOW_ABSOLUTE) == 0) { |
| 81 |
LOGGER.log(Level.INFO, |
| 82 |
() -> String.format("Rejected URI %s because absolute paths are not allowed", resourceUri)); |
| 83 |
return null; |
| 84 |
} |
| 85 |
if (!"file".equals(resourceUri.getScheme()) && (flags & FLAG_ALLOW_NON_LOCAL) == 0) { |
| 86 |
LOGGER.log(Level.INFO, |
| 87 |
() -> String.format("Rejected URI %s because non-local paths are not allowed", resourceUri)); |
| 88 |
return null; |
| 89 |
} |
| 90 |
return resourceUri; |
| 91 |
} else { |
| 92 |
if (baseDocumentUri == null) return null; |
| 93 |
return baseDocumentUri.resolve(resourceUri); |
| 94 |
} |
| 95 |
} |
| 96 |
} |