Skip to content

Commit f991b22

Browse files
Craigacpjhalexand
authored andcommitted
Stopping the various methods that use URLs from reading things remotely.
1 parent aaeedaf commit f991b22

File tree

6 files changed

+154
-37
lines changed

6 files changed

+154
-37
lines changed

olcut-config-edn/src/main/java/com/oracle/labs/mlrg/olcut/config/edn/EdnLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.oracle.labs.mlrg.olcut.config.SerializedObject;
4040
import com.oracle.labs.mlrg.olcut.config.property.SimpleProperty;
4141
import com.oracle.labs.mlrg.olcut.config.io.URLLoader;
42+
import com.oracle.labs.mlrg.olcut.util.IOUtil;
4243
import us.bpsm.edn.EdnException;
4344
import us.bpsm.edn.Keyword;
4445
import us.bpsm.edn.Symbol;
@@ -174,6 +175,8 @@ public final void load(URL url) throws ConfigLoaderException {
174175
() -> {
175176
if (url.getProtocol().equals("file")) {
176177
workingDir = new File(url.getFile()).getParent();
178+
} else if (IOUtil.isDisallowedProtocol(url)) {
179+
throw new ConfigLoaderException("Unable to load configurations from URLs with protocol: " + url.getProtocol());
177180
} else {
178181
workingDir = "";
179182
}

olcut-config-json/src/main/java/com/oracle/labs/mlrg/olcut/config/json/JsonLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.labs.mlrg.olcut.config.property.SimpleProperty;
4646
import com.oracle.labs.mlrg.olcut.config.io.URLLoader;
4747
import com.oracle.labs.mlrg.olcut.config.SerializedObject;
48+
import com.oracle.labs.mlrg.olcut.util.IOUtil;
4849

4950
import java.io.File;
5051
import java.io.IOException;
@@ -101,6 +102,8 @@ public final void load(URL url) throws ConfigLoaderException {
101102
() -> {
102103
if (url.getProtocol().equals("file")) {
103104
workingDir = new File(url.getFile()).getParent();
105+
} else if (IOUtil.isDisallowedProtocol(url)) {
106+
throw new ConfigLoaderException("Unable to load configurations from URLs with protocol: " + url.getProtocol());
104107
} else {
105108
workingDir = "";
106109
}

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/config/ConfigurationManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.oracle.labs.mlrg.olcut.config.property.Property;
4242
import com.oracle.labs.mlrg.olcut.config.property.SimpleProperty;
4343
import com.oracle.labs.mlrg.olcut.config.xml.XMLConfigFactory;
44+
import com.oracle.labs.mlrg.olcut.util.IOUtil;
4445
import com.oracle.labs.mlrg.olcut.util.Pair;
4546

4647
import javax.management.MBeanServer;
@@ -642,6 +643,9 @@ private static URL findURL(String input, String argumentName) {
642643
}
643644
}
644645
}
646+
if (IOUtil.isDisallowedProtocol(url)) {
647+
throw new ConfigLoaderException("Unable to load configurations from URLs with protocol: " + url.getProtocol());
648+
}
645649
return url;
646650
}
647651
);

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/config/xml/SAXLoader.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import javax.xml.parsers.ParserConfigurationException;
4848
import javax.xml.parsers.SAXParserFactory;
4949

50-
import com.oracle.labs.mlrg.olcut.config.Configurable;
5150
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoader;
5251
import com.oracle.labs.mlrg.olcut.config.io.ConfigLoaderException;
5352
import com.oracle.labs.mlrg.olcut.config.ConfigurationData;
@@ -59,6 +58,7 @@
5958
import com.oracle.labs.mlrg.olcut.config.SerializedObject;
6059
import com.oracle.labs.mlrg.olcut.config.property.SimpleProperty;
6160
import com.oracle.labs.mlrg.olcut.config.io.URLLoader;
61+
import com.oracle.labs.mlrg.olcut.util.IOUtil;
6262
import org.xml.sax.Attributes;
6363
import org.xml.sax.InputSource;
6464
import org.xml.sax.Locator;
@@ -111,13 +111,15 @@ public SAXLoader(URLLoader parent, Map<String, ConfigurationData> rpdMap, Map<St
111111
public final void load(URL url) throws ConfigLoaderException {
112112
AccessController.doPrivileged((PrivilegedAction<Void>)
113113
() -> {
114+
if (url.getProtocol().equals("file")) {
115+
String workingDir = new File(url.getFile()).getParent();
116+
handler.setCurWorkingDir(workingDir);
117+
} else if (IOUtil.isDisallowedProtocol(url)) {
118+
throw new ConfigLoaderException("Unable to load configurations from URLs with protocol: " + url.getProtocol());
119+
} else {
120+
handler.setCurWorkingDir("");
121+
}
114122
try (InputStream is = url.openStream()) {
115-
if (url.getProtocol().equals("file")) {
116-
String workingDir = new File(url.getFile()).getParent();
117-
handler.setCurWorkingDir(workingDir);
118-
} else {
119-
handler.setCurWorkingDir("");
120-
}
121123
innerLoad(is, url.toString());
122124
} catch (IOException e) {
123125
throw new ConfigLoaderException(e, e.getMessage());

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/provenance/ProvenanceUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,18 @@ public static String hashResource(HashType hashType, File file) {
186186
/**
187187
* Hashes a resource stream by reading the bytes and passing them through the
188188
* appropriate {@link MessageDigest}.
189+
* <p>
190+
* If the URL is remote then it logs an error and returns the hash of the URL itself.
189191
* @param hashType The type of hash to perform.
190192
* @param file The URL for the stream.
191193
* @return A hexadecimal string representation of the hash.
192194
*/
193195
public static String hashResource(HashType hashType, URL file) {
194196
MessageDigest md = hashType.getDigest();
197+
if (IOUtil.isDisallowedProtocol(file)) {
198+
logger.severe("Tried to read disallowed URL protocol: '" + file.toString() + "'");
199+
return bytesToHexString(md.digest(file.toString().getBytes(StandardCharsets.UTF_8)));
200+
}
195201
byte[] buffer = new byte[16384];
196202
int count;
197203
try (InputStream bis = new BufferedInputStream(file.openStream())) {

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/util/IOUtil.java

Lines changed: 129 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,47 +116,94 @@ public static List<String> getLines(BufferedReader reader, int count) throws IOE
116116
return lines;
117117
}
118118

119+
/**
120+
* Loads the string as a classpath resource or a path, using UTF-8.
121+
* @param path The path to load.
122+
* @return A buffered reader.
123+
* @throws FileNotFoundException If the path wasn't found.
124+
*/
119125
public static BufferedReader getReader(String path) throws FileNotFoundException {
120126
return getReader(path, StandardCharsets.UTF_8);
121127
}
122128

123-
public static BufferedReader getReader(String path, Charset charSet) throws FileNotFoundException {
124-
return new BufferedReader(new InputStreamReader(getInputStream(path),charSet),BUFFER_SIZE);
129+
/**
130+
* Loads the string as a classpath resource or a path, using the specified charset.
131+
* @param path The path to load.
132+
* @param charset The charset to use.
133+
* @return A buffered reader.
134+
* @throws FileNotFoundException If the path wasn't found.
135+
*/
136+
public static BufferedReader getReader(String path, Charset charset) throws FileNotFoundException {
137+
return new BufferedReader(new InputStreamReader(getInputStream(path),charset),BUFFER_SIZE);
125138
}
126139

127-
public static BufferedReader getReader(URI uri, Charset charSet) throws IOException {
128-
InputStream is = uri.toURL().openStream();
129-
return new BufferedReader(new InputStreamReader(is,charSet),BUFFER_SIZE);
140+
/**
141+
* This method converts the URI into a URL, and applies the
142+
* protocol check to see if it's http or https. If it is then an exception is thrown, otherwise
143+
* the stream is opened. Figures out if the stream is zipped using the magic bytes.
144+
* @param uri The URI to load.
145+
* @param charset The charset to use.
146+
* @return A buffered reader from the input stream.
147+
* @throws IOException If the URI failed to open.
148+
*/
149+
public static BufferedReader getReader(URI uri, Charset charset) throws IOException {
150+
return getReader(uri.toURL(),charset);
130151
}
131152

132-
public static BufferedReader getReader(Path path, Charset charSet) throws IOException {
133-
InputStream is = new FileInputStream(path.toFile());
134-
return new BufferedReader(new InputStreamReader(is,charSet),BUFFER_SIZE);
153+
/**
154+
* This method applies the
155+
* protocol check to see if the URL is http or https. If it is then an exception is thrown, otherwise
156+
* the stream is opened. Figures out if the stream is zipped using the magic bytes.
157+
* @param url The URL to load.
158+
* @param charset The charset to use.
159+
* @return A buffered reader from the input stream.
160+
* @throws IOException If the URI failed to open.
161+
*/
162+
public static BufferedReader getReader(URL url, Charset charset) throws IOException {
163+
if (isDisallowedProtocol(url)) {
164+
throw new IllegalArgumentException("Tried to read disallowed URL protocol: '" + url.toString() + "'");
165+
}
166+
return getReader(url.openStream(),charset);
167+
}
168+
169+
/**
170+
* Opens a buffered reader on the specified path with the specified charset. Figures out if the stream is zipped using the magic bytes.
171+
* @param path The path to read.
172+
* @param charset The charset to use.
173+
* @return A buffered reader.
174+
* @throws IOException If the path failed to open.
175+
*/
176+
public static BufferedReader getReader(Path path, Charset charset) throws IOException {
177+
return getReader(new FileInputStream(path.toFile()),charset);
135178
}
136179

137180
/**
138181
* Makes a reader wrapped around the string. Figures out if the stream is zipped using the magic bytes.
139182
* @param filename The input filename.
140-
* @param charSet The charset to use.
183+
* @param charset The charset to use.
141184
* @return A BufferedReader wrapped around the appropriate stream.
142185
* @throws FileNotFoundException If the file can't be read.
143186
* @throws IOException If an error occurred when opening the file.
144187
*/
145-
public static BufferedReader getReader(String filename, String charSet) throws FileNotFoundException, IOException {
146-
return getReader(new File(filename), charSet);
188+
public static BufferedReader getReader(String filename, String charset) throws FileNotFoundException, IOException {
189+
return getReader(new File(filename), charset);
147190
}
148191

149192
/**
150193
* Makes a reader wrapped around the file. Figures out if the stream is zipped using the magic bytes.
151194
* @param file The file to read.
152-
* @param charSet The charset to use.
195+
* @param charset The charset to use.
153196
* @return A BufferedReader wrapped around the appropriate stream.
154197
* @throws FileNotFoundException If the file can't be read.
155198
* @throws IOException If an error occurred when opening the file.
156199
*/
157-
public static BufferedReader getReader(File file, String charSet) throws FileNotFoundException, IOException {
158-
InputStream stream = wrapGZIPStream(new FileInputStream(file));
159-
return new BufferedReader(new InputStreamReader(stream,charSet));
200+
public static BufferedReader getReader(File file, String charset) throws FileNotFoundException, IOException {
201+
return getReader(new FileInputStream(file),Charset.forName(charset));
202+
}
203+
204+
private static BufferedReader getReader(InputStream stream, Charset charset) throws IOException {
205+
InputStream wrappedStream = wrapGZIPStream(stream);
206+
return new BufferedReader(new InputStreamReader(wrappedStream,charset),BUFFER_SIZE);
160207
}
161208

162209
/**
@@ -246,12 +293,12 @@ public static String toString(String path) throws IOException {
246293
return toString(path, StandardCharsets.UTF_8);
247294
}
248295

249-
public static String toString(String path, Charset charSet) throws IOException {
250-
String str = fromResource(path, charSet);
296+
public static String toString(String path, Charset charset) throws IOException {
297+
String str = fromResource(path, charset);
251298
if (str != null) {
252299
return str;
253300
} else {
254-
str = fromFile(path, charSet);
301+
str = fromFile(path, charset);
255302
if (str != null) {
256303
return str;
257304
} else {
@@ -260,35 +307,58 @@ public static String toString(String path, Charset charSet) throws IOException {
260307
}
261308
}
262309

263-
public static String fromResource(String path, Charset charSet) {
264-
return fromInputStream(IOUtil.class.getResourceAsStream(path), charSet);
310+
public static String fromResource(String path, Charset charset) {
311+
return fromInputStream(IOUtil.class.getResourceAsStream(path), charset);
265312
}
266313

267314
public static String fromPath(Path path) throws FileNotFoundException {
268315
return fromFile(path.toFile(), StandardCharsets.UTF_8);
269316
}
270317

271-
public static String fromPath(Path path, Charset charSet) throws FileNotFoundException {
272-
return fromFile(path.toFile(), charSet);
318+
public static String fromPath(Path path, Charset charset) throws FileNotFoundException {
319+
return fromFile(path.toFile(), charset);
273320
}
274321

275-
public static String fromFile(String path, Charset charSet) throws FileNotFoundException {
276-
return fromFile(new File(path), charSet);
322+
public static String fromFile(String path, Charset charset) throws FileNotFoundException {
323+
return fromFile(new File(path), charset);
277324
}
278325

279-
public static String fromFile(File file, Charset charSet) throws FileNotFoundException {
326+
public static String fromFile(File file, Charset charset) throws FileNotFoundException {
280327
if (file.length() == 0) {
281328
return "";
282329
}
283-
return fromInputStream(new FileInputStream(file), charSet);
330+
return fromInputStream(new FileInputStream(file), charset);
284331
}
285332

286-
public static String fromUri(URI uri, Charset charSet) throws IOException {
287-
return fromInputStream(uri.toURL().openStream(), charSet);
333+
/**
334+
* Reads the location specified by a URI into a String. Checks to see if the URI is
335+
* remote first, and throws IllegalArgumentException if it is.
336+
* @param uri The URI to read.
337+
* @param charset The charset to use.
338+
* @return The String contents of the URI.
339+
* @throws IOException If the URI failed to load or if it wasn't convertible to a URL.
340+
*/
341+
public static String fromUri(URI uri, Charset charset) throws IOException {
342+
return fromUrl(uri.toURL(),charset);
343+
}
344+
345+
/**
346+
* Reads the location specified by a URL into a String. Checks to see if the URL is
347+
* remote first, and throws IllegalArgumentException if it is.
348+
* @param url The URL to read.
349+
* @param charset The charset to use.
350+
* @return The String contents of the URL.
351+
* @throws IOException If the URL failed to load.
352+
*/
353+
public static String fromUrl(URL url, Charset charset) throws IOException {
354+
if (isDisallowedProtocol(url)) {
355+
throw new IllegalArgumentException("Tried to read disallowed URL protocol: '" + url.toString() + "'");
356+
}
357+
return fromInputStream(url.openStream(),charset);
288358
}
289359

290-
private static String fromInputStream(InputStream in, Charset charSet) {
291-
try (Scanner scanner = new Scanner(new BufferedInputStream(in,BUFFER_SIZE),charSet.name())) {
360+
private static String fromInputStream(InputStream in, Charset charset) {
361+
try (Scanner scanner = new Scanner(new BufferedInputStream(in,BUFFER_SIZE),charset.name())) {
292362
return scanner.useDelimiter("\\Z").next();
293363
}
294364
}
@@ -456,6 +526,10 @@ public static Iterator<Path> getPaths(List<String> fileNames, Path parentPath) {
456526
public static InputStream getInputStreamForLocation(String location) {
457527
URL url = getURLForLocation(location);
458528
if (url != null) {
529+
if (isDisallowedProtocol(url)) {
530+
logger.severe("Tried to open a disallowed URL protocol: " + url.toString());
531+
return null;
532+
}
459533
try {
460534
InputStream ret = url.openStream();
461535

@@ -544,6 +618,31 @@ public static URL getURLForLocation(String location) {
544618
return ret;
545619
}
546620

621+
/**
622+
* Checks the url to see if the protocol is disallowed.
623+
* Disallowed protocols are http, https, ftp. Null
624+
* URLs are also disallowed.
625+
* @param url The URL to check.
626+
* @return True if the protocol is disallowed, the URL is null, or the protocol is null.
627+
*/
628+
public static boolean isDisallowedProtocol(URL url) {
629+
if (url == null) {
630+
return true;
631+
}
632+
String protocol = url.getProtocol();
633+
if (protocol == null) {
634+
return true;
635+
}
636+
switch (protocol) {
637+
case "http":
638+
case "https":
639+
case "ftp":
640+
return true;
641+
default:
642+
return false;
643+
}
644+
}
645+
547646
public static class NamesPathIterator implements Iterator<Path>{
548647

549648
private final Iterator<String> fileNames;

0 commit comments

Comments
 (0)