Skip to content

Commit

Permalink
Merge pull request #306 from FlowFuse/dashboard-iframe
Browse files Browse the repository at this point in the history
Allow Dashboards to be loaded in an iFrame
  • Loading branch information
Steve-Mcl authored Dec 12, 2024
2 parents 10980df + 6c87b60 commit c6e5d88
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/runtimeSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ function getSettingsFile (settings) {
if (settings.settings.dashboardUI !== undefined) {
dashboardSettings.push(`path: '${settings.settings.dashboardUI}'`)
}
if (authMiddlewareRequired) {
if (authMiddlewareRequired && settings.settings.dashboardIFrame) {
dashboardSettings.push('middleware: flowforgeAuthMiddleware.concat(DashboardIFrameMiddleware)')
} else if (authMiddlewareRequired && !settings.settings.dashboardIFrame) {
dashboardSettings.push('middleware: flowforgeAuthMiddleware')
} else if (!authMiddlewareRequired && settings.settings.dashboardIFrame) {
dashboardSettings.push('middleware: [ DashboardIFrameMiddleware ]')
}
projectSettings.dashboardUI = `ui: { ${dashboardSettings.join(', ')}},`
}
Expand Down Expand Up @@ -251,6 +255,10 @@ function getSettingsFile (settings) {

const settingsTemplate = `
${projectSettings.setupAuthMiddleware}
const DashboardIFrameMiddleware = async function(req,res,next) {
res.set("Content-Security-Policy", "frame-ancestors *");
next()
}
module.exports = {
flowFile: 'flows.json',
flowFilePretty: true,
Expand Down
65 changes: 65 additions & 0 deletions test/unit/lib/runtimeSettings_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,71 @@ describe('Runtime Settings', function () {
err.toString().should.match(/Cannot find module '@flowfuse\/nr-auth\/middleware'/)
}
})
it('includes httpNodeMiddleware and dashboard iFrame middleware if flowforge-user auth type set and dashboard ui set to be loaded into iFrame', async function () {
const result = runtimeSettings.getSettingsFile({
baseURL: 'https://BASEURL',
forgeURL: 'https://FORGEURL',
settings: {
dashboardUI: '/foo',
httpNodeAuth: { type: 'flowforge-user' },
dashboardIFrame: true
}
})

const settings = await loadSettings(result)
settings.should.have.property('ui')
settings.ui.should.have.property('path', '/foo')
settings.ui.should.have.property('middleware')
settings.ui.middleware.should.be.an.Array().and.have.length(3)
;(typeof settings.ui.middleware[0]).should.equal('function')
;(typeof settings.ui.middleware[1]).should.equal('function')
;(typeof settings.ui.middleware[2]).should.equal('function') // iFrame middleware
// calling the middleware function should add the CSP header & call next
const headers = {}
let nextCalled = false
const res = {
set: function (header, value) {
headers[header] = value
}
}
const next = function () {
nextCalled = true
}
settings.ui.middleware[2]({}, res, next)
headers.should.have.property('Content-Security-Policy', 'frame-ancestors *')
nextCalled.should.equal(true)
})
it('includes only dashboard iFrame middleware if flowforge-user auth type is not set and dashboard ui set to be loaded into iFrame', async function () {
const result = runtimeSettings.getSettingsFile({
baseURL: 'https://BASEURL',
forgeURL: 'https://FORGEURL',
settings: {
dashboardUI: '/foo',
dashboardIFrame: true
}
})

const settings = await loadSettings(result)
settings.should.have.property('ui')
settings.ui.should.have.property('path', '/foo')
settings.ui.should.have.property('middleware')
settings.ui.middleware.should.be.an.Array().and.have.length(1)
;(typeof settings.ui.middleware[0]).should.equal('function') // iFrame middleware
// calling the middleware function should add the CSP header & call next
const headers = {}
let nextCalled = false
const res = {
set: function (header, value) {
headers[header] = value
}
}
const next = function () {
nextCalled = true
}
settings.ui.middleware[0]({}, res, next)
headers.should.have.property('Content-Security-Policy', 'frame-ancestors *')
nextCalled.should.equal(true)
})
it('includes HA settings when enabled', async function () {
const result = runtimeSettings.getSettingsFile({
baseURL: 'https://BASEURL',
Expand Down

0 comments on commit c6e5d88

Please sign in to comment.