Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolved issue #40 #1

Merged
merged 1 commit into from
Jan 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
34 changes: 34 additions & 0 deletions src/assets/app/tests/testIfAbleToRunExternalFile.js
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 9 additions & 0 deletions src/jni/NativeScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,15 @@ void NativeScriptRuntime::RequireCallback(const v8::FunctionCallbackInfo<v8::Val
ExceptionUtil::GetInstance()->HandleInvalidState(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);

Expand Down
6 changes: 3 additions & 3 deletions src/src/com/tns/Require.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down