Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

This commit fixes blackberry/BB10-WebWorks-Framework#123 that was

introduced when we added  plugins. By doing so we changed the URI
addresses used for our APIs. To address this, the bridge plugin was
changed to a default plugin.

This changes the plugin loading logic, it now will use the default
plugin when the one given is undefined instead of throwing a 404.

This also includes changes so that the default plugin will throw a 404
if something doesn't exist and only after this check for Whitelisting.

Fixes code to not work based on exception
  • Loading branch information...
commit d6f46d8425fb87018b2374b73433fd24bbf6e458 1 parent 6e3ee97
Jeffrey Heifetz authored
View
1  .jshintrc
@@ -10,6 +10,7 @@
//Webworks
"qnx", "chrome",
"blackberry", "JNEXT",
+ "frameworkModules",
//Node
"__dirname", "GLOBAL",
"process", "exports",
View
37 lib/plugins/bridge.js
@@ -1,37 +0,0 @@
-/*
- * Copyright 2010-2011 Research In Motion Limited.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-var Whitelist = require("../policy/whitelist").Whitelist,
- whitelist = new Whitelist();
-
-module.exports = {
- exec: function (request, succ, fail, args, env) {
- var extension = request.params.ext,
- method = request.params.method;
-
- if (whitelist.isFeatureAllowed(request.origin, extension)) {
- try {
- require('../../ext/' + extension + '/index')[method](succ, fail, args, env); // need to use require id, ../.. causes problems
- } catch (e) {
- console.log(e);
- fail(-1, "Feature not found", 404);
- }
- } else {
- console.log("Feature denied by whitelist: " + extension);
- fail(-1, "Feature denied by whitelist", 403);
- }
- }
-};
View
42 lib/plugins/default.js
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2010-2012 Research In Motion Limited.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var Whitelist = require("../policy/whitelist").Whitelist,
+ whitelist = new Whitelist();
+
+module.exports = {
+ exec: function (request, succ, fail, args, env) {
+ var extPath = "ext/" + request.params.ext + "/index",
+ method = request.params.method,
+ extension;
+
+ if (frameworkModules.indexOf(extPath + ".js") !== -1) {
+ extension = require("../../" + extPath);
+ if (extension[method]) {
+ if (whitelist.isFeatureAllowed(request.origin, request.params.ext)) {
+ extension[method](succ, fail, args, env);
+ } else {
+ console.log("Feature denied by whitelist: " + extension);
+ fail(-1, "Feature denied by whitelist", 403);
+ }
+ } else {
+ fail(-1, "Method for " + request.params.ext + " not found", 404);
+ }
+ } else {
+ fail(-1, "Extension not found", 404);
+ }
+ }
+};
View
4 lib/public/window-webworks.js
@@ -1,5 +1,5 @@
/*
-* Copyright 2010-2011 Research In Motion Limited.
+* Copyright 2010-2012 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -162,7 +162,7 @@
request.send(null);
execFunc = function (success, fail, service, action, args, sync) {
- var uri = "bridge/exec/" + service + "/" + action,
+ var uri = service + "/" + action,
request = new RemoteFunctionCall(uri),
name;
View
39 lib/server.js
@@ -1,5 +1,5 @@
/*
- * Copyright 2011 Research In Motion Limited.
+ * Copyright 2011-2012 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,12 +14,42 @@
* limitations under the License.
*/
+var DEFAULT_SERVICE = "default",
+ DEFAULT_ACTION = "exec";
+
+
+function rebuildRequest(req) {
+ return {
+ params : {
+ service : DEFAULT_SERVICE,
+ action : DEFAULT_ACTION,
+ ext : req.params.service,
+ method : req.params.action && req.params.action.indexOf("?") >= 0 ? req.params.action.split("?")[0] : req.params.action,
+ args : req.params.action && req.params.action.indexOf("?") >= 0 ? req.params.action.split("?")[1] : null
+ },
+ body : req.body,
+ origin : req.origin
+ };
+}
+
module.exports = {
handle: function (req, res) {
try {
- var plugin = require("./plugins/" + req.params.service),
- params = req.params.args && req.params.args.split("&"),
+ var pluginName = "lib/plugins/" + req.params.service,
+ plugin,
+ params,
args = {};
+
+ if (frameworkModules.indexOf(pluginName + ".js") === -1) {
+ pluginName = "lib/plugins/" + DEFAULT_SERVICE;
+ req = rebuildRequest(req);
+ }
+
+ //Updating because some versions of node only work with relative paths
+ pluginName = pluginName.replace('lib', '.');
+
+ plugin = require(pluginName);
+ params = req.params.args && req.params.args.split("&");
if (params) {
params.forEach(function (param) {
@@ -51,8 +81,7 @@ module.exports = {
"request": req,
"response": res
});
- }
- catch (e) {
+ } catch (e) {
console.log(e);
res.send(404, "can't find the stuff");
}
View
40 test/unit/lib/plugins/bridge.js → test/unit/lib/plugins/default.js
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-describe("bridge", function () {
- var bridge = require('../../../../lib/plugins/bridge'),
+describe("default plugin", function () {
+ var bridge = require('../../../../lib/plugins/default'),
Whitelist = require('../../../../lib/policy/whitelist').Whitelist,
testExtension = require("../../../../ext/blackberry.app/index");
@@ -28,6 +28,7 @@ describe("bridge", function () {
beforeEach(function () {
req = {
+ origin: "http://www.origin.com",
params: {}
};
res = {
@@ -36,32 +37,49 @@ describe("bridge", function () {
succ = jasmine.createSpy();
fail = jasmine.createSpy();
args = {};
+ GLOBAL.frameworkModules = ["ext/blackberry.app/index.js"];
});
- it("checks if the feature is white listed", function () {
- spyOn(Whitelist.prototype, "isFeatureAllowed").andReturn(true);
+ afterEach(function () {
+ delete GLOBAL.frameworkModules;
+ });
- bridge.exec(req, res, succ, fail, args);
- expect(Whitelist.prototype.isFeatureAllowed).toHaveBeenCalled();
+ it("returns 404 if the extension is not found", function () {
+ req.params.ext = "NotAnExt";
+ bridge.exec(req, succ, fail, args);
+ expect(fail).toHaveBeenCalledWith(-1, jasmine.any(String), 404);
+ });
+
+ it("returns 404 if the method is not found", function () {
+ req.params.ext = "blackberry.app";
+ req.params.method = "NotAMethod";
+ bridge.exec(req, succ, fail, args);
+ expect(fail).toHaveBeenCalledWith(-1, jasmine.any(String), 404);
});
- it("returns 404 if the feature is not found", function () {
+ it("checks if the feature is white listed if it exists", function () {
spyOn(Whitelist.prototype, "isFeatureAllowed").andReturn(true);
+ spyOn(testExtension, "author");
- req.params.ext = "IDoNotExist";
+ req.params.ext = "blackberry.app";
+ req.params.method = "author";
- bridge.exec(req, succ, fail, args);
- expect(fail).toHaveBeenCalledWith(-1, jasmine.any(String), 404);
+ bridge.exec(req, res, succ, fail, args);
+ expect(Whitelist.prototype.isFeatureAllowed).toHaveBeenCalledWith(req.origin, req.params.ext);
});
+
it("returns 403 if the feature is not white listed", function () {
spyOn(Whitelist.prototype, "isFeatureAllowed").andReturn(false);
+ req.params.ext = "blackberry.app";
+ req.params.method = "author";
+
bridge.exec(req, succ, fail, args);
expect(fail).toHaveBeenCalledWith(-1, jasmine.any(String), 403);
});
- it("calls the action method of the feature", function () {
+ it("calls the method of the extension", function () {
var env = {"request": req, "response": res};
spyOn(Whitelist.prototype, "isFeatureAllowed").andReturn(true);
View
44 test/unit/lib/server.js
@@ -16,9 +16,11 @@
describe("server", function () {
var server = require('../../../lib/server'),
- plugin = require("../../../lib/plugins/extensions"),
+ plugin = require("../../../lib/plugins/default"),
+ extensionPlugin = require("../../../lib/plugins/extensions"),
Whitelist = require("../../../lib/policy/whitelist").Whitelist,
- applicationAPIServer = require("../../../ext/blackberry.app/index");
+ applicationAPIServer = require("../../../ext/blackberry.app/index"),
+ DEFAULT_SERVICE = "exec";
beforeEach(function () {
spyOn(console, "log");
@@ -38,18 +40,24 @@ describe("server", function () {
res = {
send: jasmine.createSpy()
};
+ GLOBAL.frameworkModules = ['ext/blackberry.app/index.js', 'lib/plugins/extensions.js', 'lib/plugins/default.js'];
});
- it("returns 404 if the plugin doesn't exist", function () {
+ afterEach(function () {
+ delete GLOBAL.frameworkModules;
+ });
+
+ it("calls the default plugin if the service doesn't exist", function () {
+ spyOn(plugin, DEFAULT_SERVICE);
req.params.service = "not";
req.params.action = "here";
server.handle(req, res);
- expect(res.send).toHaveBeenCalledWith(404, jasmine.any(String));
+ expect(plugin[DEFAULT_SERVICE]).toHaveBeenCalled();
});
it("returns 404 if the action doesn't exist", function () {
- req.params.service = "extensions";
+ req.params.service = "default";
req.params.action = "ThisActionDoesNotExist";
server.handle(req, res);
@@ -57,22 +65,22 @@ describe("server", function () {
});
it("calls the action method on the plugin", function () {
- spyOn(plugin, "get");
+ spyOn(extensionPlugin, "get");
req.params.service = "extensions";
req.params.action = "get";
server.handle(req, res);
- expect(plugin.get).toHaveBeenCalled();
+ expect(extensionPlugin.get).toHaveBeenCalled();
});
it("returns the result and code 1 when success callback called", function () {
- spyOn(plugin, "get").andCallFake(function (request, succ, fail, body) {
+ spyOn(plugin, "exec").andCallFake(function (request, succ, fail, body) {
succ(["MyFeatureId"]);
});
- req.params.service = "extensions";
- req.params.action = "get";
+ req.params.service = "default";
+ req.params.action = "exec";
server.handle(req, res);
expect(res.send).toHaveBeenCalledWith(200, {
@@ -82,12 +90,12 @@ describe("server", function () {
});
it("returns the result and code -1 when fail callback called", function () {
- spyOn(plugin, "get").andCallFake(function (request, succ, fail, body) {
+ spyOn(plugin, "exec").andCallFake(function (request, succ, fail, body) {
fail(-1, "ErrorMessage");
});
- req.params.service = "extensions";
- req.params.action = "get";
+ req.params.service = "default";
+ req.params.action = "exec";
server.handle(req, res);
expect(res.send).toHaveBeenCalledWith(200, {
@@ -104,7 +112,7 @@ describe("server", function () {
beforeEach(function () {
req = {
params: {
- service: "bridge",
+ service: "default",
action: "exec",
ext: "blackberry.app",
method: "author",
@@ -115,11 +123,17 @@ describe("server", function () {
host: ""
},
url: "",
- body: ""
+ body: "",
+ origin: ""
};
res = {
send: jasmine.createSpy()
};
+ GLOBAL.frameworkModules = ['ext/blackberry.app/index.js', 'lib/plugins/extensions.js', 'lib/plugins/default.js'];
+ });
+
+ afterEach(function () {
+ delete GLOBAL.frameworkModules;
});
it("checks if the feature is white listed", function () {
Please sign in to comment.
Something went wrong with that request. Please try again.