History

Merge pull request #7786 from sethalves/web-scripts-use-local-libs

allow scripts on the web to refer to local libraries with /~/

Brad Hefta-Gaub committed 10 years ago

Commit 754ef072 parents eb209c7 a9897f14 files · +42 −7Browse files

Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 4 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngines.cpp+22−2
@@ -70,6 +70,12 @@ QUrl normalizeScriptURL(const QUrl& rawScriptURL) { · 69 unmodified lines
7070 }
7171 }
7272
73+QString expandScriptPath(const QString& rawPath) {
74+ QStringList splitPath = rawPath.split("/");
75+ QUrl defaultScriptsLoc = defaultScriptsLocation();
76+ return defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/"); // 2 to skip the slashes in /~/
77+}
78+
7379 QUrl expandScriptUrl(const QUrl& rawScriptURL) {
7480 QUrl normalizedScriptURL = normalizeScriptURL(rawScriptURL);
7581 if (normalizedScriptURL.scheme() == "http" ||
@@ -79,9 +85,23 @@ QUrl expandScriptUrl(const QUrl& rawScriptURL) { · 3 unmodified lines
7985 } else if (normalizedScriptURL.scheme() == "file") {
8086 if (normalizedScriptURL.path().startsWith("/~/")) {
8187 QUrl url = normalizedScriptURL;
82− QStringList splitPath = url.path().split("/");
88+ url.setPath(expandScriptPath(url.path()));
89+
90+ // stop something like Script.include(["/~/../Desktop/naughty.js"]); from working
91+ QFileInfo fileInfo(url.toLocalFile());
92+ url = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
93+
8394 QUrl defaultScriptsLoc = defaultScriptsLocation();
84− url.setPath(defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/")); // 2 to skip the slashes in /~/
95+ if (!defaultScriptsLoc.isParentOf(url)) {
96+ qCWarning(scriptengine) << "Script.include() ignoring file path" << rawScriptURL
97+ << "-- outside of standard libraries: "
98+ << url.path()
99+ << defaultScriptsLoc.path();
100+ return rawScriptURL;
101+ }
102+ if (rawScriptURL.path().endsWith("/") && !url.path().endsWith("/")) {
103+ url.setPath(url.path() + "/");
104+ }
85105 return url;
86106 }
87107 return normalizedScriptURL;