Skip to content
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
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var g = SG();
/*!
* Adds dynamically-updated docs as /explorer
*/
var deprecated = require('depd')('loopback-explorer');
var url = require('url');
var path = require('path');
var urlJoin = require('./lib/url-join');
Expand Down Expand Up @@ -133,9 +134,19 @@ function mountSwagger(loopbackApplication, swaggerApp, opts) {
}

function setupCors(swaggerApp, remotes) {
var corsOptions = remotes.options && remotes.options.cors ||
{ origin: true, credentials: true };
var corsOptions = remotes.options && remotes.options.cors;
if (corsOptions === false)
return;

deprecated(g.f(
'The built-in CORS middleware provided by loopback-component-explorer ' +
'was deprecated. See %s for more details.',
'https://docs.strongloop.com/display/public/LB/Security+considerations'
));

if (corsOptions === undefined) {
corsOptions = { origin: true, credentials: true };
}

// TODO(bajtos) Skip CORS when remotes.options.cors === false
swaggerApp.use(cors(corsOptions));
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"cors": "^2.7.1",
"debug": "^2.2.0",
"depd": "^1.1.0",
"lodash": "^3.10.0",
"loopback-swagger": "^2.1.0",
"strong-globalize": "^2.6.2",
Expand Down
12 changes: 11 additions & 1 deletion test/explorer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('explorer', function() {
it('should serve correct swagger-ui config', function(done) {
var app = loopback();
app.set('restApiRoot', '/rest-api-root');
app.set('remoting', { cors: false });
configureRestApiAndExplorer(app);

request(app)
Expand All @@ -108,6 +109,7 @@ describe('explorer', function() {
// if the basePath ends with a slash too, an incorrect URL is produced
var app = loopback();
app.set('restApiRoot', '/apis/');
app.set('remoting', { cors: false });
configureRestApiAndExplorer(app);

request(app)
Expand All @@ -129,6 +131,7 @@ describe('explorer', function() {
var app;
beforeEach(function setupExplorerWithUiDirs() {
app = loopback();
app.set('remoting', { cors: false });
explorer(app, {
uiDirs: [path.resolve(__dirname, 'fixtures', 'dummy-swagger-ui')],
});
Expand Down Expand Up @@ -160,6 +163,7 @@ describe('explorer', function() {
var app;
beforeEach(function setupExplorerWithoutUI() {
app = loopback();
app.set('remoting', { cors: false });
explorer(app, {
swaggerUI: false,
});
Expand Down Expand Up @@ -195,6 +199,7 @@ describe('explorer', function() {
var app;
beforeEach(function() {
app = loopback();
app.set('remoting', { cors: false });
var Product = loopback.PersistedModel.extend('product');
Product.attachTo(loopback.memory());
app.model(Product);
Expand All @@ -216,6 +221,7 @@ describe('explorer', function() {
var app;
beforeEach(function() {
app = loopback();
app.set('remoting', { cors: false });
});

it('should allow `uiDirs` to be defined as an Array', function(done) {
Expand Down Expand Up @@ -246,6 +252,7 @@ describe('explorer', function() {
describe('Cross-origin resource sharing', function() {
it('allows cross-origin requests by default', function(done) {
var app = loopback();
process.once('deprecation', function() { /* ignore */ });
configureRestApiAndExplorer(app, '/explorer');

request(app)
Expand All @@ -258,7 +265,7 @@ describe('explorer', function() {

it('can be disabled by configuration', function(done) {
var app = loopback();
app.set('remoting', { cors: { origin: false }});
app.set('remoting', { cors: false });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is needed to fix npm test when using LB 3.x. Before, the tests were failing because remoting.cors was rejected by LB 3.x runtime.

configureRestApiAndExplorer(app, '/explorer');

request(app)
Expand All @@ -277,6 +284,7 @@ describe('explorer', function() {

it('updates swagger object when a new model is added', function(done) {
var app = loopback();
app.set('remoting', { cors: false });
configureRestApiAndExplorer(app, '/explorer');

// Ensure the swagger object was built
Expand Down Expand Up @@ -310,6 +318,7 @@ describe('explorer', function() {

it('updates swagger object when a remote method is disabled', function(done) {
var app = loopback();
app.set('remoting', { cors: false });
configureRestApiAndExplorer(app, '/explorer');

// Ensure the swagger object was built
Expand Down Expand Up @@ -344,6 +353,7 @@ describe('explorer', function() {
function givenLoopBackAppWithExplorer(explorerBase) {
return function(done) {
var app = this.app = loopback();
app.set('remoting', { cors: false });
configureRestApiAndExplorer(app, explorerBase);

done();
Expand Down