Merge pull request #121 from sdgx/reliance-on-default-encoding

Reliance on default encoding
This commit is contained in:
Michael Bayne
2018-08-30 14:13:11 -07:00
committed by GitHub
9 changed files with 31 additions and 29 deletions
@@ -42,6 +42,7 @@ import com.threerings.getdown.util.*;
import com.threerings.getdown.util.Base64; import com.threerings.getdown.util.Base64;
import static com.threerings.getdown.Log.log; import static com.threerings.getdown.Log.log;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Parses and provide access to the information contained in the <code>getdown.txt</code> * Parses and provide access to the information contained in the <code>getdown.txt</code>
@@ -1241,7 +1242,7 @@ public class Application
if (_latest != null) { if (_latest != null) {
try (InputStream in = ConnectionUtil.open(_latest, 0, 0).getInputStream(); try (InputStream in = ConnectionUtil.open(_latest, 0, 0).getInputStream();
InputStreamReader reader = new InputStreamReader(in); InputStreamReader reader = new InputStreamReader(in, UTF_8);
BufferedReader bin = new BufferedReader(reader)) { BufferedReader bin = new BufferedReader(reader)) {
for (String[] pair : Config.parsePairs(bin, Config.createOpts(false))) { for (String[] pair : Config.parsePairs(bin, Config.createOpts(false))) {
if (pair[0].equals("version")) { if (pair[0].equals("version")) {
@@ -1563,8 +1564,8 @@ public class Application
} else { } else {
File signatureFile = downloadFile(path + SIGNATURE_SUFFIX); File signatureFile = downloadFile(path + SIGNATURE_SUFFIX);
byte[] signature = null; byte[] signature = null;
try (FileReader reader = new FileReader(signatureFile)) { try (FileInputStream signatureStream = new FileInputStream(signatureFile)) {
signature = StreamUtil.toByteArray(new FileInputStream(signatureFile)); signature = StreamUtil.toByteArray(signatureStream);
} finally { } finally {
FileUtil.deleteHarder(signatureFile); // delete the file regardless FileUtil.deleteHarder(signatureFile); // delete the file regardless
} }
@@ -19,6 +19,7 @@ import com.threerings.getdown.util.Config;
import com.threerings.getdown.util.ProgressObserver; import com.threerings.getdown.util.ProgressObserver;
import static com.threerings.getdown.Log.log; import static com.threerings.getdown.Log.log;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Manages the <code>digest.txt</code> file and the computing and processing of digests for an * Manages the <code>digest.txt</code> file and the computing and processing of digests for an
@@ -111,7 +112,7 @@ public class Digest
} }
// finally compute and append the digest for the file contents // finally compute and append the digest for the file contents
MessageDigest md = getMessageDigest(version); MessageDigest md = getMessageDigest(version);
byte[] contents = data.toString().getBytes("UTF-8"); byte[] contents = data.toString().getBytes(UTF_8);
String filename = digestFile(version); String filename = digestFile(version);
pout.println(filename + " = " + StringUtil.hexlate(md.digest(contents))); pout.println(filename + " = " + StringUtil.hexlate(md.digest(contents)));
} }
@@ -168,7 +169,7 @@ public class Digest
// we've reached the end, validate our contents // we've reached the end, validate our contents
MessageDigest md = getMessageDigest(version); MessageDigest md = getMessageDigest(version);
byte[] contents = data.toString().getBytes("UTF-8"); byte[] contents = data.toString().getBytes(UTF_8);
String hash = StringUtil.hexlate(md.digest(contents)); String hash = StringUtil.hexlate(md.digest(contents));
if (!hash.equals(_metaDigest)) { if (!hash.equals(_metaDigest)) {
String err = MessageUtil.tcompose("m.invalid_digest_file", _metaDigest, hash); String err = MessageUtil.tcompose("m.invalid_digest_file", _metaDigest, hash);
@@ -14,6 +14,8 @@ import java.security.Signature;
import com.threerings.getdown.data.Digest; import com.threerings.getdown.data.Digest;
import com.threerings.getdown.util.Base64; import com.threerings.getdown.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Produces a signed hash of the appbase, appname, and image path to ensure that signed copies of * Produces a signed hash of the appbase, appname, and image path to ensure that signed copies of
* Getdown are not hijacked to run malicious code. * Getdown are not hijacked to run malicious code.
@@ -45,7 +47,7 @@ public class AppletParamSigner
PrivateKey key = (PrivateKey)store.getKey(alias, keypass.toCharArray()); PrivateKey key = (PrivateKey)store.getKey(alias, keypass.toCharArray());
Signature sig = Signature.getInstance(Digest.sigAlgorithm(Digest.VERSION)); Signature sig = Signature.getInstance(Digest.sigAlgorithm(Digest.VERSION));
sig.initSign(key); sig.initSign(key);
sig.update(params.getBytes()); sig.update(params.getBytes(UTF_8));
String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT); String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT);
System.out.println("<param name=\"appbase\" value=\"" + appbase + "\" />"); System.out.println("<param name=\"appbase\" value=\"" + appbase + "\" />");
System.out.println("<param name=\"appname\" value=\"" + appname + "\" />"); System.out.println("<param name=\"appname\" value=\"" + appname + "\" />");
@@ -23,6 +23,8 @@ import com.threerings.getdown.data.Digest;
import com.threerings.getdown.data.Resource; import com.threerings.getdown.data.Resource;
import com.threerings.getdown.util.Base64; import com.threerings.getdown.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Handles the generation of the digest.txt file. * Handles the generation of the digest.txt file.
*/ */
@@ -120,7 +122,7 @@ public class Digester
// Write out the signature // Write out the signature
String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT); String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT);
signatureOutput.write(signed.getBytes("utf8")); signatureOutput.write(signed.getBytes(UTF_8));
} }
} }
} }
@@ -45,6 +45,8 @@ import java.io.*;
import java.util.*; import java.util.*;
import java.util.jar.*; import java.util.jar.*;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* JarDiff is able to create a jar file containing the delta between two jar files (old and new). * JarDiff is able to create a jar file containing the delta between two jar files (old and new).
* The delta jar file can then be applied to the old jar file to reconstruct the new jar file. * The delta jar file can then be applied to the old jar file to reconstruct the new jar file.
@@ -229,7 +231,7 @@ public class JarDiff implements JarDiffCodes
} }
jos.putNextEntry(new JarEntry(INDEX_NAME)); jos.putNextEntry(new JarEntry(INDEX_NAME));
byte[] bytes = writer.toString().getBytes("UTF-8"); byte[] bytes = writer.toString().getBytes(UTF_8);
jos.write(bytes, 0, bytes.length); jos.write(bytes, 0, bytes.length);
} }
@@ -27,6 +27,8 @@ import java.util.jar.JarOutputStream;
import com.threerings.getdown.util.ProgressObserver; import com.threerings.getdown.util.ProgressObserver;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Applies a jardiff patch to a jar file. * Applies a jardiff patch to a jar file.
*/ */
@@ -182,7 +184,7 @@ public class JarDiffPatcher implements JarDiffCodes
} }
LineNumberReader indexReader = LineNumberReader indexReader =
new LineNumberReader(new InputStreamReader(is, "UTF-8")); new LineNumberReader(new InputStreamReader(is, UTF_8));
String line = indexReader.readLine(); String line = indexReader.readLine();
if (line == null || !line.equals(VERSION_HEADER)) { if (line == null || !line.equals(VERSION_HEADER)) {
throw new IOException("jardiff.error.badheader: " + line); throw new IOException("jardiff.error.badheader: " + line);
@@ -16,7 +16,7 @@
package com.threerings.getdown.util; package com.threerings.getdown.util;
import java.io.UnsupportedEncodingException; import static java.nio.charset.StandardCharsets.US_ASCII;
/** /**
* Utilities for encoding and decoding the Base64 representation of * Utilities for encoding and decoding the Base64 representation of
@@ -107,7 +107,7 @@ public class Base64 {
* if any are present, there must be the correct number of them. * if any are present, there must be the correct number of them.
* *
* @param str the input String to decode, which is converted to * @param str the input String to decode, which is converted to
* bytes using the default charset * bytes using ASCII
* @param flags controls certain features of the decoded output. * @param flags controls certain features of the decoded output.
* Pass {@code DEFAULT} to decode standard Base64. * Pass {@code DEFAULT} to decode standard Base64.
* *
@@ -115,7 +115,7 @@ public class Base64 {
* incorrect padding * incorrect padding
*/ */
public static byte[] decode(String str, int flags) { public static byte[] decode(String str, int flags) {
return decode(str.getBytes(), flags); return decode(str.getBytes(US_ASCII), flags);
} }
/** /**
@@ -452,12 +452,7 @@ public class Base64 {
* adheres to RFC 2045. * adheres to RFC 2045.
*/ */
public static String encodeToString(byte[] input, int flags) { public static String encodeToString(byte[] input, int flags) {
try { return new String(encode(input, flags), US_ASCII);
return new String(encode(input, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
} }
/** /**
@@ -473,12 +468,7 @@ public class Base64 {
* adheres to RFC 2045. * adheres to RFC 2045.
*/ */
public static String encodeToString(byte[] input, int offset, int len, int flags) { public static String encodeToString(byte[] input, int offset, int len, int flags) {
try { return new String(encode(input, offset, len, flags), US_ASCII);
return new String(encode(input, offset, len, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
} }
/** /**
@@ -10,10 +10,11 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLDecoder; import java.net.URLDecoder;
import com.threerings.getdown.data.SysProps; import com.threerings.getdown.data.SysProps;
import com.threerings.getdown.util.Base64; import com.threerings.getdown.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ConnectionUtil public class ConnectionUtil
{ {
/** /**
@@ -49,7 +50,7 @@ public class ConnectionUtil
// Remove any percent-encoding in the username/password // Remove any percent-encoding in the username/password
userInfo = URLDecoder.decode(userInfo, "UTF-8"); userInfo = URLDecoder.decode(userInfo, "UTF-8");
// Now base64 encode the auth info and make it a single line // Now base64 encode the auth info and make it a single line
String encoded = Base64.encodeToString(userInfo.getBytes("UTF-8"), Base64.DEFAULT). String encoded = Base64.encodeToString(userInfo.getBytes(UTF_8), Base64.DEFAULT).
replaceAll("\\n","").replaceAll("\\r", ""); replaceAll("\\n","").replaceAll("\\r", "");
conn.setRequestProperty("Authorization", "Basic " + encoded); conn.setRequestProperty("Authorization", "Basic " + encoded);
} }
@@ -9,7 +9,6 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
@@ -20,6 +19,7 @@ import com.samskivert.util.StringUtil;
import com.threerings.getdown.data.SysProps; import com.threerings.getdown.data.SysProps;
import static com.threerings.getdown.Log.log; import static com.threerings.getdown.Log.log;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* Version related utilities. * Version related utilities.
@@ -33,7 +33,7 @@ public class VersionUtil
{ {
long fileVersion = -1; long fileVersion = -1;
try (BufferedReader bin = try (BufferedReader bin =
new BufferedReader(new InputStreamReader(new FileInputStream(vfile)))) { new BufferedReader(new InputStreamReader(new FileInputStream(vfile), UTF_8))) {
String vstr = bin.readLine(); String vstr = bin.readLine();
if (!StringUtil.isBlank(vstr)) { if (!StringUtil.isBlank(vstr)) {
fileVersion = Long.parseLong(vstr); fileVersion = Long.parseLong(vstr);
@@ -81,7 +81,8 @@ public class VersionUtil
*/ */
public static long readReleaseVersion (File relfile, String versRegex) public static long readReleaseVersion (File relfile, String versRegex)
{ {
try (BufferedReader in = new BufferedReader(new FileReader(relfile))) { try (BufferedReader in =
new BufferedReader(new InputStreamReader(new FileInputStream(relfile), UTF_8))) {
String line = null, relvers = null; String line = null, relvers = null;
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
if (line.startsWith("JAVA_VERSION=")) { if (line.startsWith("JAVA_VERSION=")) {