Debian Codegraph @

src/com/github/weisj/jsvg/parser/resources/impl/DefaultResourcePolicy.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 }