From a029cb7c53818f9eec2bcf422f3ab9bdb1550d65 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 11 Dec 2021 15:15:11 -0800 Subject: [PATCH 01/18] Fix `DM_DEFAULT_ENCODING` SpotBugs violations --- .../java/hudson/cli/CLIConnectionFactory.java | 3 +- .../java/hudson/cli/PrivateKeyProvider.java | 6 +-- core/src/main/java/hudson/FilePath.java | 5 ++- core/src/main/java/hudson/Proc.java | 3 +- core/src/main/java/hudson/WebAppMain.java | 3 +- core/src/main/java/hudson/cli/CLICommand.java | 19 ++++++++-- .../hudson/console/AnnotatedLargeText.java | 2 +- .../ConsoleAnnotationOutputStream.java | 2 +- .../main/java/hudson/console/ConsoleNote.java | 5 ++- .../java/hudson/model/FileParameterValue.java | 3 +- core/src/main/java/hudson/model/Run.java | 4 +- .../main/java/hudson/model/UpdateCenter.java | 5 ++- .../java/hudson/model/UsageStatistics.java | 2 +- core/src/main/java/hudson/scm/SCM.java | 6 ++- .../HudsonAuthenticationEntryPoint.java | 3 +- .../security/csrf/DefaultCrumbIssuer.java | 5 ++- .../main/java/hudson/util/BootFailure.java | 8 ++-- .../main/java/hudson/util/CompressedFile.java | 3 +- .../main/java/hudson/util/ConsistentHash.java | 3 +- core/src/main/java/hudson/util/IOUtils.java | 3 +- .../java/hudson/util/LogTaskListener.java | 9 ++++- .../main/java/hudson/util/ProcessTree.java | 37 ++++++++++++++----- core/src/main/java/hudson/util/Scrambler.java | 2 +- core/src/main/java/hudson/util/Secret.java | 2 +- .../security/DefaultConfidentialStore.java | 3 +- .../security/s2m/AdminWhitelistRule.java | 2 +- .../jenkins/security/s2m/ConfigDirectory.java | 8 ++-- .../slaves/restarter/UnixSlaveRestarter.java | 3 +- .../jenkins/util/JSONSignatureValidator.java | 2 +- src/spotbugs/spotbugs-excludes.xml | 32 ---------------- 30 files changed, 110 insertions(+), 83 deletions(-) diff --git a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java index eceff79eff3c..df0fae998a6d 100644 --- a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java +++ b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java @@ -1,5 +1,6 @@ package hudson.cli; +import java.nio.charset.Charset; import java.util.Base64; /** @@ -33,7 +34,7 @@ public CLIConnectionFactory basicAuth(String username, String password) { * Cf. {@code BasicHeaderApiTokenAuthenticator}. */ public CLIConnectionFactory basicAuth(String userInfo) { - return authorization("Basic " + Base64.getEncoder().encodeToString(userInfo.getBytes())); + return authorization("Basic " + Base64.getEncoder().encodeToString(userInfo.getBytes(Charset.defaultCharset()))); } /** diff --git a/cli/src/main/java/hudson/cli/PrivateKeyProvider.java b/cli/src/main/java/hudson/cli/PrivateKeyProvider.java index 1ab1eff33049..f283053f9cb5 100644 --- a/cli/src/main/java/hudson/cli/PrivateKeyProvider.java +++ b/cli/src/main/java/hudson/cli/PrivateKeyProvider.java @@ -23,7 +23,6 @@ */ package hudson.cli; -import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.logging.Level.FINE; import java.io.ByteArrayInputStream; @@ -32,6 +31,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Paths; @@ -134,7 +134,7 @@ private static String readPemFile(File f) throws IOException{ DataInputStream dis = new DataInputStream(is)) { byte[] bytes = new byte[(int) f.length()]; dis.readFully(bytes); - return new String(bytes); + return new String(bytes, StandardCharsets.UTF_8); } catch (InvalidPathException e) { throw new IOException(e); } @@ -143,7 +143,7 @@ private static String readPemFile(File f) throws IOException{ public static KeyPair loadKey(String pemString, String passwd) throws IOException, GeneralSecurityException { Iterable itr = SecurityUtils.loadKeyPairIdentities(null, new PathResource(Paths.get("key")), - new ByteArrayInputStream(pemString.getBytes(UTF_8)), + new ByteArrayInputStream(pemString.getBytes(StandardCharsets.UTF_8)), FilePasswordProvider.of(passwd)); long numLoaded = itr == null ? 0 : StreamSupport.stream(itr.spliterator(), false).count(); if (numLoaded <= 0) { diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 5b79710b25f3..1ff872a4e948 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -86,6 +86,7 @@ import java.net.URI; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.Charset; import java.nio.file.FileSystemException; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -2293,7 +2294,7 @@ private static class ReadToString extends SecureFileCallable { private static final long serialVersionUID = 1L; @Override public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { - return new String(Files.readAllBytes(fileToPath(reading(f)))); + return new String(Files.readAllBytes(fileToPath(reading(f))), Charset.defaultCharset()); } } @@ -2350,7 +2351,7 @@ private static class Write extends SecureFileCallable { public Void invoke(File f, VirtualChannel channel) throws IOException { mkdirs(f.getParentFile()); try (OutputStream fos = Files.newOutputStream(fileToPath(writing(f))); - Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos)) { + Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos, Charset.defaultCharset())) { w.write(content); } return null; diff --git a/core/src/main/java/hudson/Proc.java b/core/src/main/java/hudson/Proc.java index dd94d2097c39..24cd16552295 100644 --- a/core/src/main/java/hudson/Proc.java +++ b/core/src/main/java/hudson/Proc.java @@ -39,6 +39,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; import java.util.Locale; import java.util.Map; import java.util.concurrent.CancellationException; @@ -354,7 +355,7 @@ public int join() throws InterruptedException, IOException { // } catch (IOException x) { // LOGGER.log(Level.FINE,"stderr termination failed",x); // } - out.write(msg.getBytes()); + out.write(msg.getBytes(Charset.defaultCharset())); out.write('\n'); } return r; diff --git a/core/src/main/java/hudson/WebAppMain.java b/core/src/main/java/hudson/WebAppMain.java index f41de96eac8d..5fc53ae396bb 100644 --- a/core/src/main/java/hudson/WebAppMain.java +++ b/core/src/main/java/hudson/WebAppMain.java @@ -50,6 +50,7 @@ import java.io.OutputStream; import java.net.URL; import java.net.URLClassLoader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.StandardOpenOption; @@ -312,7 +313,7 @@ public void joinInit() throws InterruptedException { */ private void recordBootAttempt(File home) { try (OutputStream o=Files.newOutputStream(BootFailure.getBootFailureFile(home).toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { - o.write((new Date() + System.getProperty("line.separator", "\n")).getBytes()); + o.write((new Date() + System.getProperty("line.separator", "\n")).getBytes(Charset.defaultCharset())); } catch (IOException | InvalidPathException e) { LOGGER.log(WARNING, "Failed to record boot attempts",e); } diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index 198736dc9fca..a12769df05fa 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; import java.nio.charset.Charset; @@ -404,7 +405,11 @@ protected void printUsage(PrintStream stderr, CmdLineParser p) { public final String getSingleLineSummary() { ByteArrayOutputStream out = new ByteArrayOutputStream(); getCmdLineParser().printSingleLineUsage(out); - return out.toString(); + try { + return out.toString(Charset.defaultCharset().name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** @@ -414,7 +419,11 @@ public final String getSingleLineSummary() { public final String getUsage() { ByteArrayOutputStream out = new ByteArrayOutputStream(); getCmdLineParser().printUsage(out); - return out.toString(); + try { + return out.toString(Charset.defaultCharset().name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** @@ -427,7 +436,11 @@ public final String getLongDescription() { printUsageSummary(ps); ps.close(); - return out.toString(); + try { + return out.toString(Charset.defaultCharset().name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } /** diff --git a/core/src/main/java/hudson/console/AnnotatedLargeText.java b/core/src/main/java/hudson/console/AnnotatedLargeText.java index af70227ad1a5..dafff632d305 100644 --- a/core/src/main/java/hudson/console/AnnotatedLargeText.java +++ b/core/src/main/java/hudson/console/AnnotatedLargeText.java @@ -186,7 +186,7 @@ public long writeHtmlTo(long start, Writer w) throws IOException { oos.close(); StaplerResponse rsp = Stapler.getCurrentResponse(); if (rsp!=null) - rsp.setHeader("X-ConsoleAnnotator", new String(Base64.getEncoder().encode(baos.toByteArray()))); + rsp.setHeader("X-ConsoleAnnotator", Base64.getEncoder().encodeToString(baos.toByteArray())); return r; } diff --git a/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java b/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java index 00a84b6f4612..6e70a743d824 100644 --- a/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java +++ b/core/src/main/java/hudson/console/ConsoleAnnotationOutputStream.java @@ -121,7 +121,7 @@ public ConsoleAnnotator annotate(T context, MarkupText text) { } } catch (IOException | ClassNotFoundException e) { // if we failed to resurrect an annotation, ignore it. - LOGGER.log(Level.FINE, "Failed to resurrect annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest)) + "\"", e); + LOGGER.log(Level.FINE, "Failed to resurrect annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest, Charset.defaultCharset())) + "\"", e); } int bytesUsed = rest - b.available(); // bytes consumed by annotations diff --git a/core/src/main/java/hudson/console/ConsoleNote.java b/core/src/main/java/hudson/console/ConsoleNote.java index 443625350cb2..d796bb8138dc 100644 --- a/core/src/main/java/hudson/console/ConsoleNote.java +++ b/core/src/main/java/hudson/console/ConsoleNote.java @@ -43,6 +43,7 @@ import java.io.OutputStream; import java.io.Serializable; import java.io.Writer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; @@ -325,11 +326,11 @@ public static void skip(DataInputStream in) throws IOException { * Preamble of the encoded form. ANSI escape sequence to stop echo back * plus a few magic characters. */ - public static final byte[] PREAMBLE = PREAMBLE_STR.getBytes(); + public static final byte[] PREAMBLE = PREAMBLE_STR.getBytes(Charset.defaultCharset()); /** * Post amble is the ANSI escape sequence that brings back the echo. */ - public static final byte[] POSTAMBLE = POSTAMBLE_STR.getBytes(); + public static final byte[] POSTAMBLE = POSTAMBLE_STR.getBytes(Charset.defaultCharset()); /** * Locates the preamble in the given buffer. diff --git a/core/src/main/java/hudson/model/FileParameterValue.java b/core/src/main/java/hudson/model/FileParameterValue.java index b3e5c146cd9d..cc9f39faefee 100644 --- a/core/src/main/java/hudson/model/FileParameterValue.java +++ b/core/src/main/java/hudson/model/FileParameterValue.java @@ -36,6 +36,7 @@ import java.io.OutputStream; import java.io.UncheckedIOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.regex.Pattern; import jenkins.util.SystemProperties; @@ -302,7 +303,7 @@ public String getString(String encoding) throws UnsupportedEncodingException { @Override public String getString() { - return new String(get()); + return new String(get(), Charset.defaultCharset()); } @Override diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index e331a9e0c172..33673abfcdc3 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -1509,11 +1509,11 @@ public Collection getBuildFingerprints() { } String message = "No such file: " + logFile; - return new ByteArrayInputStream(charset != null ? message.getBytes(charset) : message.getBytes()); + return new ByteArrayInputStream(charset != null ? message.getBytes(charset) : message.getBytes(Charset.defaultCharset())); } public @NonNull Reader getLogReader() throws IOException { - if (charset==null) return new InputStreamReader(getLogInputStream()); + if (charset==null) return new InputStreamReader(getLogInputStream(),Charset.defaultCharset()); else return new InputStreamReader(getLogInputStream(),charset); } diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 90e50c0875e4..62b7680defc9 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -68,6 +68,7 @@ import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; +import java.nio.charset.Charset; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; import java.nio.file.InvalidPathException; @@ -2002,11 +2003,11 @@ private static VerificationResult verifyChecksums(String expectedDigest, String } if (caseSensitive) { - if (MessageDigest.isEqual(expectedDigest.getBytes(), actualDigest.getBytes())) { + if (MessageDigest.isEqual(expectedDigest.getBytes(Charset.defaultCharset()), actualDigest.getBytes(Charset.defaultCharset()))) { return VerificationResult.PASS; } } else { - if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(), actualDigest.toLowerCase().getBytes())) { + if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(Charset.defaultCharset()), actualDigest.toLowerCase().getBytes(Charset.defaultCharset()))) { return VerificationResult.PASS; } } diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index bc292db26c36..a62031bfc406 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -193,7 +193,7 @@ public String getStatData() throws IOException { o.write(w); } - return new String(Base64.getEncoder().encode(baos.toByteArray())); + return Base64.getEncoder().encodeToString(baos.toByteArray()); } catch (Throwable e) { // the exception could be GeneralSecurityException, InvalidParameterException or any other depending on the security provider you have installed LOG.log(Level.INFO, "Usage statistics could not be sent ({0})", e.getMessage()); LOG.log(Level.FINE, "Error sending usage statistics", e); diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 7b94b07e7dd5..1e2fdde83ec8 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -52,8 +52,10 @@ import hudson.security.PermissionScope; import hudson.tasks.Builder; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -739,7 +741,7 @@ protected final boolean createEmptyChangeLog(File changelogFile, BuildListener l * @since 1.568 */ protected final void createEmptyChangeLog(@NonNull File changelogFile, @NonNull TaskListener listener, @NonNull String rootTag) throws IOException { - try (FileWriter w = new FileWriter(changelogFile)) { + try (Writer w = Files.newBufferedWriter(Util.fileToPath(changelogFile), Charset.defaultCharset())) { w.write("<"+rootTag +"/>"); } } diff --git a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java index 229c7974b521..cf70b2f8ecd3 100644 --- a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java +++ b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java @@ -31,6 +31,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URLEncoder; +import java.nio.charset.Charset; import java.text.MessageFormat; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -100,7 +101,7 @@ public void commence(HttpServletRequest req, HttpServletResponse rsp, Authentica PrintWriter out; try { - out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream())); + out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream(), Charset.defaultCharset())); } catch (IllegalStateException e) { out = rsp.getWriter(); } diff --git a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java index cd1c0f795701..5338c2e06cad 100644 --- a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java +++ b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java @@ -10,6 +10,7 @@ import hudson.Util; import hudson.model.ModelObject; import hudson.model.PersistentDescriptor; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -83,8 +84,8 @@ protected synchronized String issueCrumb(ServletRequest request, String salt) { buffer.append(req.getSession().getId()); } - md.update(buffer.toString().getBytes()); - return Util.toHexString(md.digest(salt.getBytes())); + md.update(buffer.toString().getBytes(Charset.defaultCharset())); + return Util.toHexString(md.digest(salt.getBytes(Charset.defaultCharset()))); } } return null; diff --git a/core/src/main/java/hudson/util/BootFailure.java b/core/src/main/java/hudson/util/BootFailure.java index 23dff92dcd84..0de96ea8bd1b 100644 --- a/core/src/main/java/hudson/util/BootFailure.java +++ b/core/src/main/java/hudson/util/BootFailure.java @@ -4,8 +4,10 @@ import hudson.WebAppMain; import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; @@ -60,7 +62,7 @@ protected List loadAttempts(File home) { File f = getBootFailureFile(home); try { if (f.exists()) { - try (BufferedReader failureFileReader = new BufferedReader(new FileReader(f))) { + try (BufferedReader failureFileReader = Files.newBufferedReader(f.toPath(), Charset.defaultCharset())) { String line; DateFormat df = DateFormat.getDateInstance(); while ((line=failureFileReader.readLine())!=null) { @@ -72,7 +74,7 @@ protected List loadAttempts(File home) { } } } - } catch (IOException e) { + } catch (IOException | InvalidPathException e) { LOGGER.log(Level.WARNING,"Failed to parse "+f,e); } } diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 48add2d49f10..5cc858d9daa0 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -34,6 +34,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; @@ -115,7 +116,7 @@ public String loadAsString() throws IOException { StringBuilder str = new StringBuilder((int)sizeGuess); try (InputStream is = read(); - Reader r = new InputStreamReader(is)) { + Reader r = new InputStreamReader(is, Charset.defaultCharset())) { char[] buf = new char[8192]; int len; while((len=r.read(buf,0,buf.length))>0) diff --git a/core/src/main/java/hudson/util/ConsistentHash.java b/core/src/main/java/hudson/util/ConsistentHash.java index 76c7c8b872f3..001d1f0b6af3 100644 --- a/core/src/main/java/hudson/util/ConsistentHash.java +++ b/core/src/main/java/hudson/util/ConsistentHash.java @@ -24,6 +24,7 @@ package hudson.util; import hudson.util.Iterators.DuplicateFilterIterator; +import java.nio.charset.Charset; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -304,7 +305,7 @@ private synchronized void refreshTable() { private int digest(String s) { try { MessageDigest messageDigest = createMessageDigest(); - messageDigest.update(s.getBytes()); + messageDigest.update(s.getBytes(Charset.defaultCharset())); byte[] digest = messageDigest.digest(); // 16 bytes -> 4 bytes diff --git a/core/src/main/java/hudson/util/IOUtils.java b/core/src/main/java/hudson/util/IOUtils.java index c0b92536ea76..e2eab5dd4514 100644 --- a/core/src/main/java/hudson/util/IOUtils.java +++ b/core/src/main/java/hudson/util/IOUtils.java @@ -17,6 +17,7 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.util.Collection; @@ -148,7 +149,7 @@ public static int mode(File f) throws PosixException { */ public static String readFirstLine(InputStream is, String encoding) throws IOException { try (BufferedReader reader = new BufferedReader( - encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding))) { + encoding == null ? new InputStreamReader(is, Charset.defaultCharset()) : new InputStreamReader(is, encoding))) { return reader.readLine(); } } diff --git a/core/src/main/java/hudson/util/LogTaskListener.java b/core/src/main/java/hudson/util/LogTaskListener.java index 33b2ca2318ad..d94e2141ddfd 100644 --- a/core/src/main/java/hudson/util/LogTaskListener.java +++ b/core/src/main/java/hudson/util/LogTaskListener.java @@ -31,6 +31,8 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -86,7 +88,12 @@ public void write(int b) throws IOException { @Override public void flush() throws IOException { if (baos.size() > 0) { - LogRecord lr = new LogRecord(level, baos.toString()); + LogRecord lr; + try { + lr = new LogRecord(level, baos.toString(Charset.defaultCharset().name())); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } lr.setLoggerName(logger.getName()); lr.setSourceClassName(caller.getClassName()); lr.setSourceMethodName(caller.getMethodName()); diff --git a/core/src/main/java/hudson/util/ProcessTree.java b/core/src/main/java/hudson/util/ProcessTree.java index 2a0d3c6e2358..a9f182ee26c6 100644 --- a/core/src/main/java/hudson/util/ProcessTree.java +++ b/core/src/main/java/hudson/util/ProcessTree.java @@ -50,15 +50,17 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.ObjectStreamException; import java.io.RandomAccessFile; import java.io.Serializable; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -983,7 +985,7 @@ class LinuxProcess extends UnixProcess { LinuxProcess(int pid) throws IOException { super(pid); - try (BufferedReader r = new BufferedReader(new FileReader(getFile("status")))) { + try (BufferedReader r = Files.newBufferedReader(Util.fileToPath(getFile("status")), StandardCharsets.UTF_8)) { String line; while((line=r.readLine())!=null) { line=line.toLowerCase(Locale.ENGLISH); @@ -1015,7 +1017,7 @@ public synchronized List getArguments() { for (int i = 0; i < cmdline.length; i++) { byte b = cmdline[i]; if(b==0) { - arguments.add(new String(cmdline,pos,i-pos)); + arguments.add(new String(cmdline,pos,i-pos, StandardCharsets.UTF_8)); pos=i+1; } } @@ -1039,7 +1041,7 @@ public synchronized EnvVars getEnvironmentVariables() { for (int i = 0; i < environ.length; i++) { byte b = environ[i]; if(b==0) { - envVars.addLine(new String(environ,pos,i-pos)); + envVars.addLine(new String(environ,pos,i-pos, StandardCharsets.UTF_8)); pos=i+1; } } @@ -1345,7 +1347,12 @@ private String readLine(int fd, long addr, String prefix) throws IOException { buf.write(ch); addr++; } - String line = buf.toString(); + String line; + try { + line = buf.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } if(LOGGER.isLoggable(FINEST)) LOGGER.finest(prefix+" was "+line); return line; @@ -1616,7 +1623,12 @@ private String readLine(int fd, long addr, String prefix) throws IOException { buf.write(ch); addr++; } - String line = buf.toString(); + String line; + try { + line = buf.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } if(LOGGER.isLoggable(FINEST)) LOGGER.finest(prefix+" was "+line); return line; @@ -1779,13 +1791,16 @@ byte peek() { return getByte(offset); } - @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "TODO needs triage") String readString() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte ch; while(offset < length && (ch = getByte(offset++))!='\0') baos.write(ch); - return baos.toString(); + try { + return baos.toString(StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } } void skip0() { @@ -2098,7 +2113,11 @@ private void parse(Memory m, NativeLong size, Consumer consumer) { while ((ch = m.getByte(offset++)) != '\0') { baos.write(ch); } - consumer.accept(baos.toString()); + try { + consumer.accept(baos.toString(StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } baos.reset(); } } diff --git a/core/src/main/java/hudson/util/Scrambler.java b/core/src/main/java/hudson/util/Scrambler.java index 374a1e9ba21d..283a80347bad 100644 --- a/core/src/main/java/hudson/util/Scrambler.java +++ b/core/src/main/java/hudson/util/Scrambler.java @@ -40,7 +40,7 @@ public class Scrambler { public static String scramble(String secret) { if(secret==null) return null; - return new String(Base64.getEncoder().encode(secret.getBytes(StandardCharsets.UTF_8))); + return Base64.getEncoder().encodeToString(secret.getBytes(StandardCharsets.UTF_8)); } public static String descramble(String scrambled) { diff --git a/core/src/main/java/hudson/util/Secret.java b/core/src/main/java/hudson/util/Secret.java index 45385af68e0d..6da075b331a5 100644 --- a/core/src/main/java/hudson/util/Secret.java +++ b/core/src/main/java/hudson/util/Secret.java @@ -148,7 +148,7 @@ public String getEncryptedValue() { System.arraycopy(iv, 0, payload, pos, iv.length); pos+=iv.length; System.arraycopy(encrypted, 0, payload, pos, encrypted.length); - return "{"+new String(Base64.getEncoder().encode(payload))+"}"; + return "{" + Base64.getEncoder().encodeToString(payload) + "}"; } catch (GeneralSecurityException e) { throw new Error(e); // impossible } diff --git a/core/src/main/java/jenkins/security/DefaultConfidentialStore.java b/core/src/main/java/jenkins/security/DefaultConfidentialStore.java index d24bc837dd4a..45e729bcac9c 100644 --- a/core/src/main/java/jenkins/security/DefaultConfidentialStore.java +++ b/core/src/main/java/jenkins/security/DefaultConfidentialStore.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.security.GeneralSecurityException; @@ -151,5 +152,5 @@ public byte[] randomBytes(int size) { return random; } - private static final byte[] MAGIC = "::::MAGIC::::".getBytes(); + private static final byte[] MAGIC = "::::MAGIC::::".getBytes(Charset.defaultCharset()); } diff --git a/core/src/main/java/jenkins/security/s2m/AdminWhitelistRule.java b/core/src/main/java/jenkins/security/s2m/AdminWhitelistRule.java index 98bcac0f7b8b..65aa0fe5b899 100644 --- a/core/src/main/java/jenkins/security/s2m/AdminWhitelistRule.java +++ b/core/src/main/java/jenkins/security/s2m/AdminWhitelistRule.java @@ -116,7 +116,7 @@ private File getMasterKillSwitchFile(@NonNull Jenkins jenkins) { * Transform path for Windows. */ private InputStream transformForWindows(InputStream src) throws IOException { - BufferedReader r = new BufferedReader(new InputStreamReader(src)); + BufferedReader r = new BufferedReader(new InputStreamReader(src, Charset.defaultCharset())); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (PrintStream p = new PrintStream(out)) { String line; diff --git a/core/src/main/java/jenkins/security/s2m/ConfigDirectory.java b/core/src/main/java/jenkins/security/s2m/ConfigDirectory.java index cbfa699e3acf..46627e424cfa 100644 --- a/core/src/main/java/jenkins/security/s2m/ConfigDirectory.java +++ b/core/src/main/java/jenkins/security/s2m/ConfigDirectory.java @@ -2,8 +2,10 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.util.Arrays; import java.util.Collection; import java.util.logging.Level; @@ -36,7 +38,7 @@ public synchronized void load2() { for (String fragment : fragments) { File f = new File(dir, fragment); - try (BufferedReader reader = new BufferedReader(new FileReader(f))) { + try (BufferedReader reader = Files.newBufferedReader(f.toPath(), Charset.defaultCharset())) { String line; while ((line=reader.readLine())!=null) { if (line.startsWith("#")) continue; // comment @@ -44,7 +46,7 @@ public synchronized void load2() { if (r != null) result.add(r); } - } catch (IOException e) { + } catch (IOException | InvalidPathException e) { LOGGER.log(Level.WARNING, "Failed to parse "+f,e); } } diff --git a/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java b/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java index d0114c85fa8b..8b7aeeefbdf5 100644 --- a/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java +++ b/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java @@ -13,6 +13,7 @@ import hudson.Extension; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.logging.Logger; import jenkins.util.JavaVMArguments; @@ -107,7 +108,7 @@ private static String resolveSymlink(File link) throws IOException { byte[] buf = new byte[r]; m.read(0, buf, 0, r); - return new String(buf); + return new String(buf, StandardCharsets.UTF_8); } throw new IOException("Failed to readlink " + link); diff --git a/core/src/main/java/jenkins/util/JSONSignatureValidator.java b/core/src/main/java/jenkins/util/JSONSignatureValidator.java index 274c0cb96cd9..2cf5463f364c 100644 --- a/core/src/main/java/jenkins/util/JSONSignatureValidator.java +++ b/core/src/main/java/jenkins/util/JSONSignatureValidator.java @@ -239,7 +239,7 @@ private boolean verifySignature(Signature signature, String providedSignature) { * Utility method supporting both possible digest formats: Base64 and Hex */ private boolean digestMatches(byte[] digest, String providedDigest) { - return providedDigest.equalsIgnoreCase(Hex.encodeHexString(digest)) || providedDigest.equalsIgnoreCase(new String(Base64.getEncoder().encode(digest))); + return providedDigest.equalsIgnoreCase(Hex.encodeHexString(digest)) || providedDigest.equalsIgnoreCase(Base64.getEncoder().encodeToString(digest)); } diff --git a/src/spotbugs/spotbugs-excludes.xml b/src/spotbugs/spotbugs-excludes.xml index 19f12efdc543..9e175ed16185 100644 --- a/src/spotbugs/spotbugs-excludes.xml +++ b/src/spotbugs/spotbugs-excludes.xml @@ -36,48 +36,16 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 67063e758022047e51c4b05ea8607c319265e97f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sun, 12 Dec 2021 09:27:39 -0800 Subject: [PATCH 02/18] Revert changes to SCM --- core/src/main/java/hudson/scm/SCM.java | 6 ++---- src/spotbugs/spotbugs-excludes.xml | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 1e2fdde83ec8..7b94b07e7dd5 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -52,10 +52,8 @@ import hudson.security.PermissionScope; import hudson.tasks.Builder; import java.io.File; +import java.io.FileWriter; import java.io.IOException; -import java.io.Writer; -import java.nio.charset.Charset; -import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -741,7 +739,7 @@ protected final boolean createEmptyChangeLog(File changelogFile, BuildListener l * @since 1.568 */ protected final void createEmptyChangeLog(@NonNull File changelogFile, @NonNull TaskListener listener, @NonNull String rootTag) throws IOException { - try (Writer w = Files.newBufferedWriter(Util.fileToPath(changelogFile), Charset.defaultCharset())) { + try (FileWriter w = new FileWriter(changelogFile)) { w.write("<"+rootTag +"/>"); } } diff --git a/src/spotbugs/spotbugs-excludes.xml b/src/spotbugs/spotbugs-excludes.xml index 9e175ed16185..c4a69c7526da 100644 --- a/src/spotbugs/spotbugs-excludes.xml +++ b/src/spotbugs/spotbugs-excludes.xml @@ -41,6 +41,7 @@ + From ebb6700eb18c7324c54522aa4928395c3b83514f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sun, 12 Dec 2021 13:58:19 -0800 Subject: [PATCH 03/18] Restore changes to SCM; fix test bugs --- core/src/main/java/hudson/scm/SCM.java | 6 ++++-- src/spotbugs/spotbugs-excludes.xml | 1 - test/src/test/java/hudson/model/JobTest.java | 8 +++++--- test/src/test/java/hudson/model/RunMapTest.java | 6 ++++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 7b94b07e7dd5..1e2fdde83ec8 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -52,8 +52,10 @@ import hudson.security.PermissionScope; import hudson.tasks.Builder; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -739,7 +741,7 @@ protected final boolean createEmptyChangeLog(File changelogFile, BuildListener l * @since 1.568 */ protected final void createEmptyChangeLog(@NonNull File changelogFile, @NonNull TaskListener listener, @NonNull String rootTag) throws IOException { - try (FileWriter w = new FileWriter(changelogFile)) { + try (Writer w = Files.newBufferedWriter(Util.fileToPath(changelogFile), Charset.defaultCharset())) { w.write("<"+rootTag +"/>"); } } diff --git a/src/spotbugs/spotbugs-excludes.xml b/src/spotbugs/spotbugs-excludes.xml index c4a69c7526da..9e175ed16185 100644 --- a/src/spotbugs/spotbugs-excludes.xml +++ b/src/spotbugs/spotbugs-excludes.xml @@ -41,7 +41,6 @@ - diff --git a/test/src/test/java/hudson/model/JobTest.java b/test/src/test/java/hudson/model/JobTest.java index 6c73c911bc59..5736bd69196a 100644 --- a/test/src/test/java/hudson/model/JobTest.java +++ b/test/src/test/java/hudson/model/JobTest.java @@ -501,15 +501,17 @@ public void interruptOnDelete() throws Exception { p.setConcurrentBuild(true); p.getBuildersList().add(new SleepBuilder(30000)); // we want the uninterrupted job to run for long time FreeStyleBuild build1 = p.scheduleBuild2(0).getStartCondition().get(); + j.waitForMessage("Sleeping", build1); FreeStyleBuild build2 = p.scheduleBuild2(0).getStartCondition().get(); + j.waitForMessage("Sleeping", build2); QueueTaskFuture build3 = p.scheduleBuild2(0); long start = System.nanoTime(); p.delete(); long end = System.nanoTime(); assertThat(end - start, Matchers.lessThan(TimeUnit.SECONDS.toNanos(1))); - assertThat(build1.getResult(), Matchers.is(Result.ABORTED)); - assertThat(build2.getResult(), Matchers.is(Result.ABORTED)); - assertThat(build3.isCancelled(), Matchers.is(true)); + j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(build1)); + j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(build2)); + assertTrue(build3.isCancelled()); } @Issue("SECURITY-1868") diff --git a/test/src/test/java/hudson/model/RunMapTest.java b/test/src/test/java/hudson/model/RunMapTest.java index 4d1bc00cd5b6..61873bb8c051 100644 --- a/test/src/test/java/hudson/model/RunMapTest.java +++ b/test/src/test/java/hudson/model/RunMapTest.java @@ -45,6 +45,7 @@ public class RunMapTest { // now create a build that hangs until we signal the OneShotEvent p.getBuildersList().add(new SleepBuilder(9999999)); FreeStyleBuild b2 = p.scheduleBuild2(0).waitForStart(); + r.waitForMessage("Sleeping", b2); assertEquals(2, b2.number); // now reload @@ -59,6 +60,8 @@ public class RunMapTest { b1 = p.getBuildByNumber(1); assertSame(b1.getNextBuild(), b2); assertSame(b2.getPreviousBuild(), b1); + b2.doStop(); + r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b2)); } @Issue("JENKINS-27530") @@ -81,16 +84,19 @@ public class RunMapTest { assertEquals(1, items.length); assertEquals(p, items[0].task); // the real issue: assignBuildNumber was being called on the wrong Job QueueTaskFuture b2f = items[0].getFuture(); + r.waitForMessage("Sleeping", b1); b1.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b1)); FreeStyleBuild b2 = (FreeStyleBuild) b2f.waitForStart(); assertEquals(2, b2.getNumber()); assertEquals(p, b2.getParent()); + r.waitForMessage("Sleeping", b2); b2.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b2)); FreeStyleBuild b3 = p.scheduleBuild2(0).waitForStart(); assertEquals(3, b3.getNumber()); assertEquals(p, b3.getParent()); + r.waitForMessage("Sleeping", b3); b3.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b3)); } From dbecf0a183406d6932783b8b2dae6b434bf3ba5c Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sun, 12 Dec 2021 15:14:23 -0800 Subject: [PATCH 04/18] Not a test bug after all --- core/src/main/java/hudson/model/AbstractBuild.java | 3 ++- test/src/test/java/hudson/model/JobTest.java | 8 +++----- test/src/test/java/hudson/model/RunMapTest.java | 6 ------ 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 0a7f1cb68323..add476c3cea7 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -65,6 +65,7 @@ import java.io.IOException; import java.io.InterruptedIOException; import java.lang.ref.WeakReference; +import java.nio.channels.ClosedByInterruptException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; @@ -668,7 +669,7 @@ public void defaultCheckout() throws IOException, InterruptedException { } } catch (AbortException e) { listener.error(e.getMessage()); - } catch (InterruptedIOException e) { + } catch (ClosedByInterruptException | InterruptedIOException e) { throw (InterruptedException)new InterruptedException().initCause(e); } catch (IOException e) { // checkout error not yet reported diff --git a/test/src/test/java/hudson/model/JobTest.java b/test/src/test/java/hudson/model/JobTest.java index 5736bd69196a..6c73c911bc59 100644 --- a/test/src/test/java/hudson/model/JobTest.java +++ b/test/src/test/java/hudson/model/JobTest.java @@ -501,17 +501,15 @@ public void interruptOnDelete() throws Exception { p.setConcurrentBuild(true); p.getBuildersList().add(new SleepBuilder(30000)); // we want the uninterrupted job to run for long time FreeStyleBuild build1 = p.scheduleBuild2(0).getStartCondition().get(); - j.waitForMessage("Sleeping", build1); FreeStyleBuild build2 = p.scheduleBuild2(0).getStartCondition().get(); - j.waitForMessage("Sleeping", build2); QueueTaskFuture build3 = p.scheduleBuild2(0); long start = System.nanoTime(); p.delete(); long end = System.nanoTime(); assertThat(end - start, Matchers.lessThan(TimeUnit.SECONDS.toNanos(1))); - j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(build1)); - j.assertBuildStatus(Result.ABORTED, j.waitForCompletion(build2)); - assertTrue(build3.isCancelled()); + assertThat(build1.getResult(), Matchers.is(Result.ABORTED)); + assertThat(build2.getResult(), Matchers.is(Result.ABORTED)); + assertThat(build3.isCancelled(), Matchers.is(true)); } @Issue("SECURITY-1868") diff --git a/test/src/test/java/hudson/model/RunMapTest.java b/test/src/test/java/hudson/model/RunMapTest.java index 61873bb8c051..4d1bc00cd5b6 100644 --- a/test/src/test/java/hudson/model/RunMapTest.java +++ b/test/src/test/java/hudson/model/RunMapTest.java @@ -45,7 +45,6 @@ public class RunMapTest { // now create a build that hangs until we signal the OneShotEvent p.getBuildersList().add(new SleepBuilder(9999999)); FreeStyleBuild b2 = p.scheduleBuild2(0).waitForStart(); - r.waitForMessage("Sleeping", b2); assertEquals(2, b2.number); // now reload @@ -60,8 +59,6 @@ public class RunMapTest { b1 = p.getBuildByNumber(1); assertSame(b1.getNextBuild(), b2); assertSame(b2.getPreviousBuild(), b1); - b2.doStop(); - r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b2)); } @Issue("JENKINS-27530") @@ -84,19 +81,16 @@ public class RunMapTest { assertEquals(1, items.length); assertEquals(p, items[0].task); // the real issue: assignBuildNumber was being called on the wrong Job QueueTaskFuture b2f = items[0].getFuture(); - r.waitForMessage("Sleeping", b1); b1.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b1)); FreeStyleBuild b2 = (FreeStyleBuild) b2f.waitForStart(); assertEquals(2, b2.getNumber()); assertEquals(p, b2.getParent()); - r.waitForMessage("Sleeping", b2); b2.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b2)); FreeStyleBuild b3 = p.scheduleBuild2(0).waitForStart(); assertEquals(3, b3.getNumber()); assertEquals(p, b3.getParent()); - r.waitForMessage("Sleeping", b3); b3.getExecutor().interrupt(); r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b3)); } From 032a3fa01e74e5ccbd19bab964ce0b5990e29535 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 10:21:30 -0800 Subject: [PATCH 05/18] Update cli/src/main/java/hudson/cli/CLIConnectionFactory.java Co-authored-by: James Nord --- cli/src/main/java/hudson/cli/CLIConnectionFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java index df0fae998a6d..7d847d3eeab6 100644 --- a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java +++ b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java @@ -1,6 +1,6 @@ package hudson.cli; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Base64; /** From 181119a57d6550b9592a2110b2ec781690c13a9f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 10:21:41 -0800 Subject: [PATCH 06/18] Update cli/src/main/java/hudson/cli/CLIConnectionFactory.java Co-authored-by: James Nord --- cli/src/main/java/hudson/cli/CLIConnectionFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java index 7d847d3eeab6..0553dd215de5 100644 --- a/cli/src/main/java/hudson/cli/CLIConnectionFactory.java +++ b/cli/src/main/java/hudson/cli/CLIConnectionFactory.java @@ -34,7 +34,7 @@ public CLIConnectionFactory basicAuth(String username, String password) { * Cf. {@code BasicHeaderApiTokenAuthenticator}. */ public CLIConnectionFactory basicAuth(String userInfo) { - return authorization("Basic " + Base64.getEncoder().encodeToString(userInfo.getBytes(Charset.defaultCharset()))); + return authorization("Basic " + Base64.getEncoder().encodeToString(userInfo.getBytes(StandardCharsets.UTF_8))); } /** From 5876b03d7a1c80a61a7575e1683baa4e785b2062 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:34:24 -0800 Subject: [PATCH 07/18] Use getClientCharset() in CLICommand --- core/src/main/java/hudson/cli/CLICommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index a12769df05fa..d27d0b6beff5 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -406,7 +406,7 @@ public final String getSingleLineSummary() { ByteArrayOutputStream out = new ByteArrayOutputStream(); getCmdLineParser().printSingleLineUsage(out); try { - return out.toString(Charset.defaultCharset().name()); + return out.toString(getClientCharset().name()); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } @@ -420,7 +420,7 @@ public final String getUsage() { ByteArrayOutputStream out = new ByteArrayOutputStream(); getCmdLineParser().printUsage(out); try { - return out.toString(Charset.defaultCharset().name()); + return out.toString(getClientCharset().name()); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } @@ -437,7 +437,7 @@ public final String getLongDescription() { printUsageSummary(ps); ps.close(); try { - return out.toString(Charset.defaultCharset().name()); + return out.toString(getClientCharset().name()); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } @@ -470,7 +470,7 @@ public void setClientCharset(@NonNull Charset encoding) { this.encoding = encoding; } - protected @NonNull Charset getClientCharset() throws IOException, InterruptedException { + protected @NonNull Charset getClientCharset() { if (encoding != null) { return encoding; } From f7628b3b1a73970dc5505bfa13120f40c15cabb9 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:34:53 -0800 Subject: [PATCH 08/18] Use UTF-8 in ConsoleNote --- core/src/main/java/hudson/console/ConsoleNote.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/console/ConsoleNote.java b/core/src/main/java/hudson/console/ConsoleNote.java index d796bb8138dc..2bcb7caeb0ac 100644 --- a/core/src/main/java/hudson/console/ConsoleNote.java +++ b/core/src/main/java/hudson/console/ConsoleNote.java @@ -43,7 +43,7 @@ import java.io.OutputStream; import java.io.Serializable; import java.io.Writer; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; @@ -326,11 +326,11 @@ public static void skip(DataInputStream in) throws IOException { * Preamble of the encoded form. ANSI escape sequence to stop echo back * plus a few magic characters. */ - public static final byte[] PREAMBLE = PREAMBLE_STR.getBytes(Charset.defaultCharset()); + public static final byte[] PREAMBLE = PREAMBLE_STR.getBytes(StandardCharsets.UTF_8); /** * Post amble is the ANSI escape sequence that brings back the echo. */ - public static final byte[] POSTAMBLE = POSTAMBLE_STR.getBytes(Charset.defaultCharset()); + public static final byte[] POSTAMBLE = POSTAMBLE_STR.getBytes(StandardCharsets.UTF_8); /** * Locates the preamble in the given buffer. From 0684c9ca31848618d1354ffdc0ba3a80ebbae6ee Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:36:27 -0800 Subject: [PATCH 09/18] Let us use ASCII here perhaps --- core/src/main/java/hudson/model/UpdateCenter.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 58eb105d8d27..a023c3764bd1 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -69,6 +69,7 @@ import java.net.URLConnection; import java.net.UnknownHostException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; import java.nio.file.InvalidPathException; @@ -2025,11 +2026,11 @@ private static VerificationResult verifyChecksums(String expectedDigest, String } if (caseSensitive) { - if (MessageDigest.isEqual(expectedDigest.getBytes(Charset.defaultCharset()), actualDigest.getBytes(Charset.defaultCharset()))) { + if (MessageDigest.isEqual(expectedDigest.getBytes(StandardCharsets.US_ASCII), actualDigest.getBytes(Charset.defaultCharset()))) { return VerificationResult.PASS; } } else { - if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(Charset.defaultCharset()), actualDigest.toLowerCase().getBytes(Charset.defaultCharset()))) { + if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(StandardCharsets.US_ASCII), actualDigest.toLowerCase().getBytes(Charset.defaultCharset()))) { return VerificationResult.PASS; } } From 7e51696f5810f6e41d8418c7f8635c9f56ffa76f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:38:41 -0800 Subject: [PATCH 10/18] Use UTF-8 in HudsonAuthenticationEntryPoint --- .../java/hudson/security/HudsonAuthenticationEntryPoint.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java index cf70b2f8ecd3..3acab22bf065 100644 --- a/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java +++ b/core/src/main/java/hudson/security/HudsonAuthenticationEntryPoint.java @@ -31,7 +31,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URLEncoder; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -101,7 +101,7 @@ public void commence(HttpServletRequest req, HttpServletResponse rsp, Authentica PrintWriter out; try { - out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream(), Charset.defaultCharset())); + out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream(), StandardCharsets.UTF_8)); } catch (IllegalStateException e) { out = rsp.getWriter(); } From 265558fd398c26f04dd84357b0017968b5f92343 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:40:16 -0800 Subject: [PATCH 11/18] Use UTF-8 in DefaultCrumbIssuer --- .../main/java/hudson/security/csrf/DefaultCrumbIssuer.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java index 215656053db1..1efad8bbc560 100644 --- a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java +++ b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java @@ -10,7 +10,6 @@ import hudson.Util; import hudson.model.ModelObject; import hudson.model.PersistentDescriptor; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -84,8 +83,8 @@ protected synchronized String issueCrumb(ServletRequest request, String salt) { buffer.append(req.getSession().getId()); } - md.update(buffer.toString().getBytes(Charset.defaultCharset())); - return Util.toHexString(md.digest(salt.getBytes(Charset.defaultCharset()))); + md.update(buffer.toString().getBytes(StandardCharsets.UTF_8)); + return Util.toHexString(md.digest(salt.getBytes(StandardCharsets.UTF_8))); } } return null; From d6904a221ff5205e5fc1b428bdce46fdad6dd392 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:42:56 -0800 Subject: [PATCH 12/18] Salt is a hex-string, so US_ASCII works --- core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java index 1efad8bbc560..a5ec4a26ad72 100644 --- a/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java +++ b/core/src/main/java/hudson/security/csrf/DefaultCrumbIssuer.java @@ -84,7 +84,7 @@ protected synchronized String issueCrumb(ServletRequest request, String salt) { } md.update(buffer.toString().getBytes(StandardCharsets.UTF_8)); - return Util.toHexString(md.digest(salt.getBytes(StandardCharsets.UTF_8))); + return Util.toHexString(md.digest(salt.getBytes(StandardCharsets.US_ASCII))); } } return null; From 7d47fa18c7b71f04e2e2d2681c2399a9503eb757 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:45:31 -0800 Subject: [PATCH 13/18] Deprecate CompressedFile#loadAsString --- core/src/main/java/hudson/util/CompressedFile.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 5cc858d9daa0..648972716144 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -102,7 +102,9 @@ public InputStream read() throws IOException { /** * Loads the file content as a string. + * @deprecated removed without replacement */ + @Deprecated public String loadAsString() throws IOException { long sizeGuess; if(file.exists()) From b14c62453c53c2b4dda68ce566f458ab5ea7314c Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:47:47 -0800 Subject: [PATCH 14/18] Use UTF-8 in ConsistentHash --- core/src/main/java/hudson/util/ConsistentHash.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/util/ConsistentHash.java b/core/src/main/java/hudson/util/ConsistentHash.java index 001d1f0b6af3..3a2d983f3c0c 100644 --- a/core/src/main/java/hudson/util/ConsistentHash.java +++ b/core/src/main/java/hudson/util/ConsistentHash.java @@ -24,7 +24,7 @@ package hudson.util; import hudson.util.Iterators.DuplicateFilterIterator; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -305,7 +305,7 @@ private synchronized void refreshTable() { private int digest(String s) { try { MessageDigest messageDigest = createMessageDigest(); - messageDigest.update(s.getBytes(Charset.defaultCharset())); + messageDigest.update(s.getBytes(StandardCharsets.UTF_8)); byte[] digest = messageDigest.digest(); // 16 bytes -> 4 bytes From ebbc6923522fafdf06bb5ca02f7d71f39aa5c082 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:48:31 -0800 Subject: [PATCH 15/18] Use UTF-8 in LogTaskListener --- core/src/main/java/hudson/util/LogTaskListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/util/LogTaskListener.java b/core/src/main/java/hudson/util/LogTaskListener.java index d94e2141ddfd..7d696ada4632 100644 --- a/core/src/main/java/hudson/util/LogTaskListener.java +++ b/core/src/main/java/hudson/util/LogTaskListener.java @@ -32,7 +32,7 @@ import java.io.OutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -90,7 +90,7 @@ public void flush() throws IOException { if (baos.size() > 0) { LogRecord lr; try { - lr = new LogRecord(level, baos.toString(Charset.defaultCharset().name())); + lr = new LogRecord(level, baos.toString(StandardCharsets.UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } From ae5a020498e005d84164997b4d61e39e4f0d88e6 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:49:46 -0800 Subject: [PATCH 16/18] Use ASCII for DefaultConfidentialStore --- .../main/java/jenkins/security/DefaultConfidentialStore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/jenkins/security/DefaultConfidentialStore.java b/core/src/main/java/jenkins/security/DefaultConfidentialStore.java index 45e729bcac9c..49513a02ef60 100644 --- a/core/src/main/java/jenkins/security/DefaultConfidentialStore.java +++ b/core/src/main/java/jenkins/security/DefaultConfidentialStore.java @@ -8,7 +8,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.security.GeneralSecurityException; @@ -152,5 +152,5 @@ public byte[] randomBytes(int size) { return random; } - private static final byte[] MAGIC = "::::MAGIC::::".getBytes(Charset.defaultCharset()); + private static final byte[] MAGIC = "::::MAGIC::::".getBytes(StandardCharsets.US_ASCII); } From af9dcccabe5129b4fc6f39cbd5f9fa86aa478521 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 20 Dec 2021 13:54:11 -0800 Subject: [PATCH 17/18] Missed one --- core/src/main/java/hudson/model/UpdateCenter.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index a023c3764bd1..6047a012a294 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -68,7 +68,6 @@ import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; @@ -2026,11 +2025,11 @@ private static VerificationResult verifyChecksums(String expectedDigest, String } if (caseSensitive) { - if (MessageDigest.isEqual(expectedDigest.getBytes(StandardCharsets.US_ASCII), actualDigest.getBytes(Charset.defaultCharset()))) { + if (MessageDigest.isEqual(expectedDigest.getBytes(StandardCharsets.US_ASCII), actualDigest.getBytes(StandardCharsets.US_ASCII))) { return VerificationResult.PASS; } } else { - if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(StandardCharsets.US_ASCII), actualDigest.toLowerCase().getBytes(Charset.defaultCharset()))) { + if (MessageDigest.isEqual(expectedDigest.toLowerCase().getBytes(StandardCharsets.US_ASCII), actualDigest.toLowerCase().getBytes(StandardCharsets.US_ASCII))) { return VerificationResult.PASS; } } From a7ff6add7a5872a69bd854d4215cf2cdbe5f6638 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 22 Dec 2021 10:50:42 -0800 Subject: [PATCH 18/18] Get a new build --- core/src/main/java/hudson/FilePath.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 43bf94b50df5..cadb8cc257e9 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -3612,5 +3612,4 @@ public boolean accept(File file) { } private static final long serialVersionUID = 1L; } - }