Skip to content

Commit 6d920d8

Browse files
committed
Merge pull request #1 from NativeScript/plamen5kov/issue#40
resolved issue #40
2 parents 4095245 + 3af5526 commit 6d920d8

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

src/assets/app/mainpage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require("./tests/stringConversionTests");
1111
require("./tests/testsForTypescript");
1212
require("./tests/testGC");
1313
require("./tests/testsMemoryManagement");
14+
require("./tests/testIfAbleToRunExternalFile");
1415

1516
var MainActivity = com.tns.NativeScriptActivity.extends({
1617
onCreate: function() {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var Assert = function(condition, failMessage) {
2+
if (condition == false) {
3+
fail(failMessage);
4+
}
5+
}
6+
7+
var When_file_outside_the_project_folder_is_required_it_should_fail = function() {
8+
9+
Log("When_file_outside_the_project_folder_is_required_it_should_throw_IllegalAccessException");
10+
11+
var illegalAccesExceptionCaught = false;
12+
var fileSeparator = "/";
13+
var nonExistingFileName = "nonExistingFile";
14+
var nonExistingFileExtension = ".js";
15+
16+
//create a file in external storage
17+
var pathToExternalStorage = android.os.Environment.getExternalStorageDirectory().toString();
18+
var appDirectory = new java.io.File(pathToExternalStorage + fileSeparator + nonExistingFileName + nonExistingFileExtension);
19+
appDirectory.mkdirs();
20+
21+
try
22+
{
23+
//try to require it with absolute path (requireing files with absolute path should not be possible)
24+
require(pathToExternalStorage + fileSeparator + nonExistingFileName);
25+
}
26+
catch(e)
27+
{
28+
exceptionCaught = true;
29+
}
30+
31+
Assert(exceptionCaught === true, "When_file_outside_the_project_folder_is_required_it_should_fail FAILED: Exception(illegal access) should be thrown");
32+
}
33+
34+
When_file_outside_the_project_folder_is_required_it_should_fail();

src/jni/NativeScriptRuntime.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,15 @@ void NativeScriptRuntime::RequireCallback(const v8::FunctionCallbackInfo<v8::Val
862862
ExceptionUtil::GetInstance()->HandleInvalidState(exception, false);
863863
return;
864864
}
865+
if (modulePath == "EXTERNAL_FILE_ERROR")
866+
{
867+
// module not found
868+
stringstream ss;
869+
ss << "Module \"" << moduleName << "\" is located on the external storage. Modules can be private application files ONLY";
870+
string exception = ss.str();
871+
ExceptionUtil::GetInstance()->HandleInvalidState(exception, false);
872+
return;
873+
}
865874

866875
auto it = loadedModules.find(modulePath);
867876

src/src/com/tns/Require.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static String getModulePath(String moduleName, String callingModuleName)
109109
File projectRootDir = new File(RootPackageDir);
110110
if (isFileExternal(file, projectRootDir))
111111
{
112-
throw new IllegalAccessError();
112+
return "EXTERNAL_FILE_ERROR";
113113
}
114114
else
115115
{
@@ -128,12 +128,12 @@ private static boolean isFileExternal(File source, File target)
128128

129129
while (currentParentDir != null)
130130
{
131-
currentParentDir = currentParentDir.getParentFile();
132-
133131
if (currentParentDir.equals(target))
134132
{
135133
return false;
136134
}
135+
136+
currentParentDir = currentParentDir.getParentFile();
137137
}
138138

139139
return true;

0 commit comments

Comments
 (0)