### Eclipse Workspace Patch 1.0
#P josm
diff --git src/org/openstreetmap/josm/gui/io/DownloadFileTask.java src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
index 4954333..5e54dd4 100644
|
|
|
|
| 9 | 9 | import java.io.InputStream; |
| 10 | 10 | import java.io.OutputStream; |
| 11 | 11 | import java.net.MalformedURLException; |
| | 12 | import java.net.URI; |
| | 13 | import java.net.URISyntaxException; |
| 12 | 14 | import java.net.URL; |
| 13 | 15 | import java.nio.charset.StandardCharsets; |
| 14 | 16 | import java.nio.file.Files; |
| … |
… |
|
| 97 | 99 | public void download() throws DownloadException { |
| 98 | 100 | try { |
| 99 | 101 | if (mkdir) { |
| 100 | | File newDir = file.getParentFile(); |
| 101 | | if (!newDir.exists()) { |
| | 102 | final File newDir = file.getParentFile(); |
| | 103 | if (newDir != null && !newDir.exists()) { |
| 102 | 104 | Utils.mkDirs(newDir); |
| 103 | 105 | } |
| 104 | 106 | } |
| 105 | 107 | |
| 106 | | URL url = new URL(address); |
| 107 | | long size; |
| | 108 | final URL url = new URI(address).toURL(); |
| | 109 | final long size; |
| 108 | 110 | synchronized (this) { |
| 109 | 111 | downloadConnection = HttpClient.create(url).useCache(false); |
| 110 | 112 | downloadConnection.connect(); |
| … |
… |
|
| 118 | 120 | InputStream in = downloadConnection.getResponse().getContent(); |
| 119 | 121 | OutputStream out = Files.newOutputStream(file.toPath()) |
| 120 | 122 | ) { |
| 121 | | byte[] buffer = new byte[32_768]; |
| 122 | | int count = 0; |
| | 123 | final byte[] buffer = new byte[32_768]; |
| | 124 | long count = 0; |
| 123 | 125 | long p1 = 0; |
| 124 | 126 | long p2; |
| 125 | 127 | for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { |
| 126 | 128 | out.write(buffer, 0, read); |
| 127 | 129 | count += read; |
| 128 | | if (canceled) break; |
| 129 | | p2 = 100L * count / size; |
| 130 | | if (p2 != p1) { |
| 131 | | progressMonitor.setTicks((int) p2); |
| 132 | | p1 = p2; |
| | 130 | if (canceled) { |
| | 131 | break; |
| | 132 | } |
| | 133 | |
| | 134 | if (size > 0) { |
| | 135 | p2 = 100L * count / size; |
| | 136 | if (p2 != p1) { |
| | 137 | progressMonitor.setTicks((int) p2); |
| | 138 | p1 = p2; |
| | 139 | } |
| 133 | 140 | } |
| 134 | 141 | } |
| 135 | 142 | } |
| … |
… |
|
| 141 | 148 | Utils.deleteFile(file); |
| 142 | 149 | } |
| 143 | 150 | } |
| 144 | | } catch (MalformedURLException e) { |
| 145 | | String msg = tr("Cannot download file ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", |
| | 151 | } catch (MalformedURLException | URISyntaxException e) { |
| | 152 | final String msg = tr("Cannot download file ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", |
| 146 | 153 | file.getName(), address); |
| 147 | 154 | Logging.warn(msg); |
| 148 | 155 | throw new DownloadException(msg, e); |
| 149 | 156 | } catch (IOException | InvalidPathException e) { |
| 150 | | if (canceled) |
| 151 | | return; |
| | 157 | if (canceled) { |
| | 158 | return; |
| | 159 | } |
| 152 | 160 | throw new DownloadException(e.getMessage(), e); |
| 153 | 161 | } finally { |
| 154 | 162 | closeConnectionIfNeeded(); |
| … |
… |
|
| 157 | 165 | |
| 158 | 166 | @Override |
| 159 | 167 | protected void realRun() throws SAXException, IOException { |
| 160 | | if (canceled) return; |
| | 168 | if (canceled) { |
| | 169 | return; |
| | 170 | } |
| 161 | 171 | try { |
| 162 | 172 | download(); |
| 163 | 173 | } catch (DownloadException e) { |
| … |
… |
|
| 182 | 192 | * @throws IOException if any I/O error occurs |
| 183 | 193 | */ |
| 184 | 194 | public static void unzipFileRecursively(File file, String dir) throws IOException { |
| 185 | | Path dirPath = Paths.get(dir); |
| | 195 | final Path dirPath = Paths.get(dir); |
| 186 | 196 | try (ZipFile zf = new ZipFile(file, StandardCharsets.UTF_8)) { |
| 187 | | Enumeration<? extends ZipEntry> es = zf.entries(); |
| | 197 | final Enumeration<? extends ZipEntry> es = zf.entries(); |
| 188 | 198 | while (es.hasMoreElements()) { |
| 189 | | ZipEntry ze = es.nextElement(); |
| 190 | | File newFile = new File(dir, ze.getName()); |
| | 199 | final ZipEntry ze = es.nextElement(); |
| | 200 | final File newFile = new File(dir, ze.getName()); |
| 191 | 201 | // Checks for Zip Slip Vulnerability (CWE-22 / path traversal) |
| 192 | 202 | if (!newFile.toPath().normalize().startsWith(dirPath)) { |
| 193 | 203 | throw new IOException("Bad zip entry - Invalid or malicious file, potential CWE-22 attack"); |
| 194 | 204 | } |
| 195 | 205 | if (ze.isDirectory()) { |
| 196 | 206 | Utils.mkDirs(newFile); |
| 197 | | } else try (InputStream is = zf.getInputStream(ze)) { |
| 198 | | Files.copy(is, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 199 | | } |
| | 207 | } else { |
| | 208 | try (InputStream is = zf.getInputStream(ze)) { |
| | 209 | Files.copy(is, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| | 210 | } |
| | 211 | } |
| 200 | 212 | } |
| 201 | 213 | } |
| 202 | 214 | } |