Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 02c988ab6b75
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e1eb184094f7
Choose a head ref
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Aug 25, 2016

  1. Backport Change script file restriction from 9000

    Daniel Smith committed Aug 25, 2016
    Copy the full SHA
    5c4dcff View commit details

Commits on Aug 26, 2016

  1. Merge pull request #4115 from jellymann/fix-stdin-bug-1_7

    Backport "Change script file restriction" from 9000
    kares authored Aug 26, 2016
    Copy the full SHA
    e1eb184 View commit details
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -412,7 +412,7 @@ public InputStream getScriptSource() {
final String script = getScriptFileName();
FileResource resource = JRubyFile.createRestrictedResource(getCurrentDirectory(), getScriptFileName());
if (resource != null && resource.exists()) {
if (resource.canRead()) {
if (resource.canRead() && !resource.isDirectory()) {
if (isXFlag()) {
// search for a shebang line and
// return the script between shebang and __END__ or CTRL-Z (0x1A)
71 changes: 69 additions & 2 deletions core/src/test/java/org/jruby/test/TestRubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (C) 2008 Ola Bini <ola.bini@gmail.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
@@ -27,8 +27,12 @@
***** END LICENSE BLOCK *****/
package org.jruby.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLClassLoader;
import java.util.Locale;

import org.jruby.exceptions.MainExitException;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.runtime.load.LoadService;
@@ -41,7 +45,7 @@ public class TestRubyInstanceConfig extends TestRubyBase {
public TestRubyInstanceConfig(String name) {
super(name);
}

private RubyInstanceConfig config;

@Override
@@ -87,4 +91,67 @@ public void testSettingNewLoaderWillCreateNewClassLoader() throws Exception {
config.setLoader(new URLClassLoader(new java.net.URL[0], beforeCL));
assertTrue("setting a new classloader this is different, should create a new classcache", beforeCC != config.getClassCache());
}

public void testGetScriptSource() throws Exception {
config.setCurrentDirectory("uri:classloader:/test_dir");
config.setScriptFileName("test_script.rb");

String scriptSource = inputStreamToString(config.getScriptSource());

assertEquals(scriptSource, "puts \"Hello World\"\n");
}

public void testGetScriptSourceWithSTDIN() throws Exception {
config.setScriptFileName(getSTDINPath());

assertNotNull(config.getScriptSource());
}

public void testGetScriptSourceWithDirectory() throws Exception {
config.setCurrentDirectory("uri:classloader:/somedir");
config.setScriptFileName("dir_with_listing");

try {
config.getScriptSource();
fail("Should throw FileNotFoundException");
} catch (MainExitException ex) {
assertTrue(ex.getMessage().indexOf("(Not a file)") > -1);
}
}

public void testGetScriptSourceWithNonexistentFile() throws Exception {
config.setCurrentDirectory("uri:classloader:/somedir");
config.setScriptFileName("non_existing.rb");

try {
config.getScriptSource();
fail("Should throw FileNotFoundException");
} catch (MainExitException ex) {
assertTrue(ex.getMessage().indexOf("(No such file or directory)") > -1);
}
}

private String inputStreamToString(InputStream inputStream) throws Exception {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}

private String getSTDINPath() {
String osName = System.getProperty("os.name").toLowerCase(Locale.US);
if (osName.indexOf("windows") > -1) {
return "CON";
}
if (osName.indexOf("openvms") > -1) {
return "/sys$input";
}
if (osName.indexOf("mac") > -1) {
return "/dev/fd/0";
}
return "/dev/stdin";
}
}
1 change: 1 addition & 0 deletions core/src/test/resources/test_dir/test_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello World"