From 3af552610a5caaa2238f06dcf63e85401dfcbcac Mon Sep 17 00:00:00 2001 From: plamen5kov Date: Fri, 16 Jan 2015 13:19:31 +0200 Subject: [PATCH] resolved issue #40 --- src/assets/app/mainpage.js | 1 + .../app/tests/testIfAbleToRunExternalFile.js | 34 +++++++++++++++++++ src/jni/NativeScriptRuntime.cpp | 9 +++++ src/src/com/tns/Require.java | 6 ++-- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/assets/app/tests/testIfAbleToRunExternalFile.js diff --git a/src/assets/app/mainpage.js b/src/assets/app/mainpage.js index 5e41e135c..1459cc9bc 100644 --- a/src/assets/app/mainpage.js +++ b/src/assets/app/mainpage.js @@ -11,6 +11,7 @@ require("./tests/stringConversionTests"); require("./tests/testsForTypescript"); require("./tests/testGC"); require("./tests/testsMemoryManagement"); +require("./tests/testIfAbleToRunExternalFile"); var MainActivity = com.tns.NativeScriptActivity.extends({ onCreate: function() { diff --git a/src/assets/app/tests/testIfAbleToRunExternalFile.js b/src/assets/app/tests/testIfAbleToRunExternalFile.js new file mode 100644 index 000000000..abf542c53 --- /dev/null +++ b/src/assets/app/tests/testIfAbleToRunExternalFile.js @@ -0,0 +1,34 @@ +var Assert = function(condition, failMessage) { + if (condition == false) { + fail(failMessage); + } +} + +var When_file_outside_the_project_folder_is_required_it_should_fail = function() { + + Log("When_file_outside_the_project_folder_is_required_it_should_throw_IllegalAccessException"); + + var illegalAccesExceptionCaught = false; + var fileSeparator = "/"; + var nonExistingFileName = "nonExistingFile"; + var nonExistingFileExtension = ".js"; + + //create a file in external storage + var pathToExternalStorage = android.os.Environment.getExternalStorageDirectory().toString(); + var appDirectory = new java.io.File(pathToExternalStorage + fileSeparator + nonExistingFileName + nonExistingFileExtension); + appDirectory.mkdirs(); + + try + { + //try to require it with absolute path (requireing files with absolute path should not be possible) + require(pathToExternalStorage + fileSeparator + nonExistingFileName); + } + catch(e) + { + exceptionCaught = true; + } + + Assert(exceptionCaught === true, "When_file_outside_the_project_folder_is_required_it_should_fail FAILED: Exception(illegal access) should be thrown"); +} + +When_file_outside_the_project_folder_is_required_it_should_fail(); diff --git a/src/jni/NativeScriptRuntime.cpp b/src/jni/NativeScriptRuntime.cpp index 29d14d682..c8ca7ac6e 100644 --- a/src/jni/NativeScriptRuntime.cpp +++ b/src/jni/NativeScriptRuntime.cpp @@ -862,6 +862,15 @@ void NativeScriptRuntime::RequireCallback(const v8::FunctionCallbackInfoHandleInvalidState(exception, false); return; } + if (modulePath == "EXTERNAL_FILE_ERROR") + { + // module not found + stringstream ss; + ss << "Module \"" << moduleName << "\" is located on the external storage. Modules can be private application files ONLY"; + string exception = ss.str(); + ExceptionUtil::GetInstance()->HandleInvalidState(exception, false); + return; + } auto it = loadedModules.find(modulePath); diff --git a/src/src/com/tns/Require.java b/src/src/com/tns/Require.java index 544145a2f..1eea179e2 100644 --- a/src/src/com/tns/Require.java +++ b/src/src/com/tns/Require.java @@ -109,7 +109,7 @@ public static String getModulePath(String moduleName, String callingModuleName) File projectRootDir = new File(RootPackageDir); if (isFileExternal(file, projectRootDir)) { - throw new IllegalAccessError(); + return "EXTERNAL_FILE_ERROR"; } else { @@ -128,12 +128,12 @@ private static boolean isFileExternal(File source, File target) while (currentParentDir != null) { - currentParentDir = currentParentDir.getParentFile(); - if (currentParentDir.equals(target)) { return false; } + + currentParentDir = currentParentDir.getParentFile(); } return true;