|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.net.URI;
|
20 | 20 | import java.net.URISyntaxException;
|
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.security.MessageDigest; |
21 | 23 | import java.util.Optional;
|
22 | 24 |
|
23 | 25 | import org.slf4j.Logger;
|
|
28 | 30 | import com.vaadin.flow.internal.StateNode;
|
29 | 31 | import com.vaadin.flow.internal.UrlUtil;
|
30 | 32 | import com.vaadin.flow.server.AbstractStreamResource;
|
| 33 | +import com.vaadin.flow.server.ErrorEvent; |
31 | 34 | import com.vaadin.flow.server.HttpStatusCode;
|
32 | 35 | import com.vaadin.flow.server.RequestHandler;
|
33 | 36 | import com.vaadin.flow.server.StreamReceiver;
|
34 | 37 | import com.vaadin.flow.server.StreamResource;
|
35 | 38 | import com.vaadin.flow.server.StreamResourceRegistry;
|
| 39 | +import com.vaadin.flow.server.UploadException; |
36 | 40 | import com.vaadin.flow.server.VaadinRequest;
|
37 | 41 | import com.vaadin.flow.server.VaadinResponse;
|
38 | 42 | import com.vaadin.flow.server.VaadinSession;
|
| 43 | +import com.vaadin.flow.server.frontend.FrontendUtils; |
| 44 | +import com.vaadin.flow.server.streams.UploadHandler; |
39 | 45 |
|
40 |
| -import static com.vaadin.flow.server.communication.StreamReceiverHandler.DEFAULT_FILE_COUNT_MAX; |
41 |
| -import static com.vaadin.flow.server.communication.StreamReceiverHandler.DEFAULT_FILE_SIZE_MAX; |
42 |
| -import static com.vaadin.flow.server.communication.StreamReceiverHandler.DEFAULT_SIZE_MAX; |
| 46 | +import static com.vaadin.flow.server.Constants.DEFAULT_FILE_COUNT_MAX; |
| 47 | +import static com.vaadin.flow.server.Constants.DEFAULT_FILE_SIZE_MAX; |
| 48 | +import static com.vaadin.flow.server.Constants.DEFAULT_REQUEST_SIZE_MAX; |
43 | 49 |
|
44 | 50 | /**
|
45 | 51 | * Handles {@link StreamResource} and {@link StreamReceiver} instances
|
@@ -109,48 +115,108 @@ public boolean handleRequest(VaadinSession session, VaadinRequest request,
|
109 | 115 |
|
110 | 116 | AbstractStreamResource resource = abstractStreamResource.get();
|
111 | 117 | if (resource instanceof StreamResourceRegistry.ElementStreamResource elementRequest) {
|
112 |
| - Element owner = elementRequest.getOwner(); |
113 |
| - StateNode node = owner.getNode(); |
114 |
| - |
115 |
| - if ((node.isInert() |
116 |
| - && !elementRequest.getElementRequestHandler().allowInert()) |
117 |
| - || !node.isAttached() || !node.isEnabled()) { |
118 |
| - response.sendError(HttpStatusCode.FORBIDDEN.getCode(), |
119 |
| - "Resource not available"); |
120 |
| - return true; |
121 |
| - } else { |
122 |
| - elementRequest.getElementRequestHandler().handleRequest(request, |
123 |
| - response, session, elementRequest.getOwner()); |
124 |
| - } |
| 118 | + callElementResourceHandler(session, request, response, |
| 119 | + elementRequest, pathInfo); |
125 | 120 | } else if (resource instanceof StreamResource) {
|
126 | 121 | resourceHandler.handleRequest(session, request, response,
|
127 | 122 | (StreamResource) resource);
|
128 | 123 | } else if (resource instanceof StreamReceiver streamReceiver) {
|
129 |
| - String[] parts = parsePath(pathInfo); |
| 124 | + PathData parts = parsePath(pathInfo); |
130 | 125 |
|
131 | 126 | receiverHandler.handleRequest(session, request, response,
|
132 |
| - streamReceiver, parts[0], parts[1]); |
| 127 | + streamReceiver, parts.UIid, parts.securityKey); |
133 | 128 | } else {
|
134 | 129 | getLogger().warn("Received unknown stream resource.");
|
135 | 130 | }
|
136 | 131 | return true;
|
137 | 132 | }
|
138 | 133 |
|
| 134 | + private void callElementResourceHandler(VaadinSession session, |
| 135 | + VaadinRequest request, VaadinResponse response, |
| 136 | + StreamResourceRegistry.ElementStreamResource elementRequest, |
| 137 | + String pathInfo) throws IOException { |
| 138 | + Element owner = elementRequest.getOwner(); |
| 139 | + StateNode node = owner.getNode(); |
| 140 | + |
| 141 | + if ((node.isInert() |
| 142 | + && !elementRequest.getElementRequestHandler().allowInert()) |
| 143 | + || !node.isAttached() || !node.isEnabled()) { |
| 144 | + response.sendError(HttpStatusCode.FORBIDDEN.getCode(), |
| 145 | + "Resource not available"); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + if (elementRequest |
| 150 | + .getElementRequestHandler() instanceof UploadHandler) { |
| 151 | + // Validate upload security key. Else respond with |
| 152 | + // FORBIDDEN. |
| 153 | + PathData parts = parsePath(pathInfo); |
| 154 | + session.lock(); |
| 155 | + try { |
| 156 | + String secKey = elementRequest.getId(); |
| 157 | + if (secKey == null || !MessageDigest.isEqual( |
| 158 | + secKey.getBytes(StandardCharsets.UTF_8), |
| 159 | + parts.securityKey.getBytes(StandardCharsets.UTF_8))) { |
| 160 | + LoggerFactory.getLogger(StreamRequestHandler.class).warn( |
| 161 | + "Received incoming stream with faulty security key."); |
| 162 | + response.sendError(HttpStatusCode.FORBIDDEN.getCode(), |
| 163 | + "Resource not available"); |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + // Set current UI to upload url ui. |
| 168 | + UI ui = session.getUIById(Integer.parseInt(parts.UIid)); |
| 169 | + UI.setCurrent(ui); |
| 170 | + |
| 171 | + if (node == null) { |
| 172 | + session.getErrorHandler() |
| 173 | + .error(new ErrorEvent(new UploadException( |
| 174 | + "File upload ignored because the node for the upload owner component was not found"))); |
| 175 | + response.sendError(HttpStatusCode.FORBIDDEN.getCode(), |
| 176 | + "Resource not available"); |
| 177 | + return; |
| 178 | + } |
| 179 | + if (!node.isAttached()) { |
| 180 | + session.getErrorHandler() |
| 181 | + .error(new ErrorEvent(new UploadException( |
| 182 | + "Warning: file upload ignored for " |
| 183 | + + node.getId() |
| 184 | + + " because the component was disabled"))); |
| 185 | + response.sendError(HttpStatusCode.FORBIDDEN.getCode(), |
| 186 | + "Resource not available"); |
| 187 | + return; |
| 188 | + } |
| 189 | + } finally { |
| 190 | + session.unlock(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + elementRequest.getElementRequestHandler().handleRequest(request, |
| 195 | + response, session, elementRequest.getOwner()); |
| 196 | + } |
| 197 | + |
| 198 | + private record PathData(String UIid, String securityKey, String fileName) { |
| 199 | + } |
| 200 | + |
139 | 201 | /**
|
140 | 202 | * Parse the pathInfo for id data.
|
141 | 203 | * <p>
|
142 | 204 | * URI pattern: VAADIN/dynamic/resource/[UIID]/[SECKEY]/[NAME]
|
143 | 205 | *
|
144 | 206 | * @see #generateURI
|
145 | 207 | */
|
146 |
| - private String[] parsePath(String pathInfo) { |
| 208 | + private PathData parsePath(String pathInfo) { |
147 | 209 | // strip away part until the data we are interested starts
|
148 | 210 | int startOfData = pathInfo.indexOf(DYN_RES_PREFIX)
|
149 | 211 | + DYN_RES_PREFIX.length();
|
150 | 212 |
|
151 | 213 | String uppUri = pathInfo.substring(startOfData);
|
152 | 214 | // [0] UIid, [1] security key, [2] name
|
153 |
| - return uppUri.split("/", 3); |
| 215 | + String[] split = uppUri.split("/", 3); |
| 216 | + if (split.length == 3) { |
| 217 | + return new PathData(split[0], split[1], split[2]); |
| 218 | + } |
| 219 | + return new PathData(split[0], split[1], ""); |
154 | 220 | }
|
155 | 221 |
|
156 | 222 | /**
|
@@ -195,7 +261,7 @@ private static Optional<URI> getPathUri(String path) {
|
195 | 261 | * @return maximum request size for upload
|
196 | 262 | */
|
197 | 263 | protected long getRequestSizeMax() {
|
198 |
| - return DEFAULT_SIZE_MAX; |
| 264 | + return DEFAULT_REQUEST_SIZE_MAX; |
199 | 265 | }
|
200 | 266 |
|
201 | 267 | /**
|
|
0 commit comments