Ticket #24976: patch-Fix-Sonar-issues-in-DownloadFileTask.patch

File patch-Fix-Sonar-issues-in-DownloadFileTask.patch, 5.2 KB (added by DanProgs <Abenteurer.2901@…>, 38 hours ago)
  • src/org/openstreetmap/josm/gui/io/DownloadFileTask.java

    ### 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
     
    99import java.io.InputStream;
    1010import java.io.OutputStream;
    1111import java.net.MalformedURLException;
     12import java.net.URI;
     13import java.net.URISyntaxException;
    1214import java.net.URL;
    1315import java.nio.charset.StandardCharsets;
    1416import java.nio.file.Files;
    … …  
    9799    public void download() throws DownloadException {
    98100        try {
    99101            if (mkdir) {
    100                 File newDir = file.getParentFile();
    101                 if (!newDir.exists()) {
     102                final File newDir = file.getParentFile();
     103                if (newDir != null && !newDir.exists()) {
    102104                    Utils.mkDirs(newDir);
    103105                }
    104106            }
    105107
    106             URL url = new URL(address);
    107             long size;
     108            final URL url = new URI(address).toURL();
     109            final long size;
    108110            synchronized (this) {
    109111                downloadConnection = HttpClient.create(url).useCache(false);
    110112                downloadConnection.connect();
    … …  
    118120                InputStream in = downloadConnection.getResponse().getContent();
    119121                OutputStream out = Files.newOutputStream(file.toPath())
    120122            ) {
    121                 byte[] buffer = new byte[32_768];
    122                 int count = 0;
     123                final byte[] buffer = new byte[32_768];
     124                long count = 0;
    123125                long p1 = 0;
    124126                long p2;
    125127                for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
    126128                    out.write(buffer, 0, read);
    127129                    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                        }
    133140                    }
    134141                }
    135142            }
    … …  
    141148                    Utils.deleteFile(file);
    142149                }
    143150            }
    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.",
    146153                    file.getName(), address);
    147154            Logging.warn(msg);
    148155            throw new DownloadException(msg, e);
    149156        } catch (IOException | InvalidPathException e) {
    150             if (canceled)
    151                 return;
     157            if (canceled) {
     158                return;
     159            }
    152160            throw new DownloadException(e.getMessage(), e);
    153161        } finally {
    154162            closeConnectionIfNeeded();
    … …  
    157165
    158166    @Override
    159167    protected void realRun() throws SAXException, IOException {
    160         if (canceled) return;
     168        if (canceled) {
     169            return;
     170        }
    161171        try {
    162172            download();
    163173        } catch (DownloadException e) {
    … …  
    182192     * @throws IOException if any I/O error occurs
    183193     */
    184194    public static void unzipFileRecursively(File file, String dir) throws IOException {
    185         Path dirPath = Paths.get(dir);
     195        final Path dirPath = Paths.get(dir);
    186196        try (ZipFile zf = new ZipFile(file, StandardCharsets.UTF_8)) {
    187             Enumeration<? extends ZipEntry> es = zf.entries();
     197            final Enumeration<? extends ZipEntry> es = zf.entries();
    188198            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());
    191201                // Checks for Zip Slip Vulnerability (CWE-22 / path traversal)
    192202                if (!newFile.toPath().normalize().startsWith(dirPath)) {
    193203                    throw new IOException("Bad zip entry - Invalid or malicious file, potential CWE-22 attack");
    194204                }
    195205                if (ze.isDirectory()) {
    196206                    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                }
    200212            }
    201213        }
    202214    }