From c8f9373cddc98058b6bc5898c1dd44426c20e2c6 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Fri, 30 Oct 2026 11:37:44 +0000 Subject: [PATCH 1/2] Limit project link device ACLs to the device's own instance - Device subscriptions to p//in and p//res, and device publishes to p//out, now use checkDeviceAssignedToProject so a device only uses its own instance's topics - Return only id and name from GET /teams/:teamId/projects for device tokens, as already done for project tokens - Add ACL tests for wildcard, cross-team and dev: response/broadcast topics --- forge/ee/lib/projectComms/index.js | 10 +- forge/routes/api/team.js | 4 +- test/unit/forge/comms/authRoutes_spec.js | 190 ++++++++++++++++++++++- test/unit/forge/routes/api/team_spec.js | 4 + 4 files changed, 200 insertions(+), 8 deletions(-) diff --git a/forge/ee/lib/projectComms/index.js b/forge/ee/lib/projectComms/index.js index ff23e50d00..8d2a8a8153 100644 --- a/forge/ee/lib/projectComms/index.js +++ b/forge/ee/lib/projectComms/index.js @@ -81,10 +81,11 @@ module.exports.init = function (app) { ) // Receive messages sent to this device (instance owned) // - ff/v1//p//in/+/# + // project = the instance the device is assigned to app.comms.aclManager.addACL( 'device', 'sub', - { topic: /^ff\/v1\/([^/]+)\/p\/(?!dev:)([^/]+)\/in\/[^/]+($|\/.*$)$/, verify: 'checkDeviceCanAccessProject' } + { topic: /^ff\/v1\/([^/]+)\/p\/(?!dev:)([^/]+)\/in\/[^/]+($|\/.*$)$/, verify: 'checkDeviceAssignedToProject' } ) // Receive messages sent to this device (app owned) // - ff/v1//p/dev:/in/+/# @@ -95,10 +96,11 @@ module.exports.init = function (app) { ) // Receive link-call response messages sent to this device (instance owned) // - ff/v1//p//res/+/# + // project = the instance the device is assigned to app.comms.aclManager.addACL( 'device', 'sub', - { topic: /^ff\/v1\/([^/]+)\/p\/(?!dev:)([^/]+)\/res\/[^/]+($|\/.*$)$/, verify: 'checkDeviceCanAccessProject', deviceOwnerType: 'instance' } + { topic: /^ff\/v1\/([^/]+)\/p\/(?!dev:)([^/]+)\/res\/[^/]+($|\/.*$)$/, verify: 'checkDeviceAssignedToProject' } ) // Receive link-call response messages sent to this device (app owned) // - ff/v1//p/dev:/res/+/# @@ -121,12 +123,12 @@ module.exports.init = function (app) { 'pub', { topic: /^ff\/v1\/([^/]+)\/p\/dev:([^/]+)\/in\/[^/]+($|\/.*$)/, verify: 'checkDeviceIsAssigned' } ) - // Send broadcast messages (from instance owned devices in the team) + // Send broadcast messages (from instance owned devices, as the instance they are assigned to) // - ff/v1//p//out/+/# app.comms.aclManager.addACL( 'device', 'pub', - { topic: /^ff\/v1\/([^/]+)\/p\/(?!dev:)([^/]+)\/out\/[^/]+($|\/.*$)/, verify: 'checkDeviceCanAccessProject', deviceOwnerType: 'instance' } + { topic: /^ff\/v1\/([^/]+)\/p\/(?!dev:)([^/]+)\/out\/[^/]+($|\/.*$)/, verify: 'checkDeviceAssignedToProject' } ) // Send broadcast messages (from app owned devices in the team) // - ff/v1//p/dev:/out/+/# diff --git a/forge/routes/api/team.js b/forge/routes/api/team.js index accf61238b..7688193911 100644 --- a/forge/routes/api/team.js +++ b/forge/routes/api/team.js @@ -418,8 +418,8 @@ module.exports = async function (app) { includeSettings: true, includeMeta }) - if (request.session.ownerType === 'project') { - // This request is from a project token. Filter the list to return + if (request.session.ownerType === 'project' || request.session.ownerType === 'device') { + // This request is from a project or device token. Filter the list to return // the minimal information needed result = result.map(e => { return { id: e.id, name: e.name } diff --git a/test/unit/forge/comms/authRoutes_spec.js b/test/unit/forge/comms/authRoutes_spec.js index decadf0922..8da7670c74 100644 --- a/test/unit/forge/comms/authRoutes_spec.js +++ b/test/unit/forge/comms/authRoutes_spec.js @@ -611,6 +611,76 @@ describe('Broker Auth API', async function () { topic: 'ff/v1/abc/p/dev:some-device/in/foo' }) }) + it('allows project to publish to an app owned device response topic', async function () { + await allowWrite({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/dev:some-device/res/foo' + }) + }) + it('prevents project from subscribing to another projects response topic', async function () { + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/another-project/res/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/another-project/res-random/foo' + }) + }) + it('prevents project from subscribing to inbox or response topics with a wildcard', async function () { + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/+/in/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: '$share/xyz/ff/v1/abc/p/+/in/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/+/res/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/+/res-random/foo' + }) + }) + it('prevents project from publishing broadcast as an app owned device', async function () { + await denyWrite({ + username: 'project:abc:xyz', + topic: 'ff/v1/abc/p/dev:some-device/out/foo' + }) + }) + it('prevents project from using another teams project link topics', async function () { + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/other-team/p/another-project/out/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: '$share/xyz/ff/v1/other-team/p/+/out/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/other-team/p/xyz/in/foo' + }) + await denyRead({ + username: 'project:abc:xyz', + topic: 'ff/v1/other-team/p/xyz/res/foo' + }) + await denyWrite({ + username: 'project:abc:xyz', + topic: 'ff/v1/other-team/p/xyz/out/foo' + }) + await denyWrite({ + username: 'project:abc:xyz', + topic: 'ff/v1/other-team/p/another-project/in/foo' + }) + await denyWrite({ + username: 'project:abc:xyz', + topic: 'ff/v1/other-team/p/another-project/res/foo' + }) + }) describe('application scoped broadcast', async function () { let projectUsername let appTopicPrefix @@ -874,6 +944,24 @@ describe('Broker Auth API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/in/foo` }) }) + it('cannot subscribe to another project inbox', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectB.id}/in/foo` + }) + }) + it('can subscribe to project response if assigned', async function () { + await allowRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/res/foo` + }) + }) + it('cannot subscribe to another project response', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectB.id}/res/foo` + }) + }) it('can subscribe to project broadcast if assigned', async function () { await allowRead({ username: deviceUsername, @@ -898,6 +986,12 @@ describe('Broker Auth API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/out/foo` }) }) + it('cannot publish to another project output', async function () { + await denyWrite({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectB.id}/out/foo` + }) + }) it('can publish to project response if assigned', async function () { await allowWrite({ username: deviceUsername, @@ -946,6 +1040,43 @@ describe('Broker Auth API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/a/${TestObjects.ApplicationA.hashid}/p/${TestObjects.ProjectB.id}/out/foo` }) }) + it('can subscribe to app owned device broadcast', async function () { + await allowRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceB.hashid}/out/foo` + }) + }) + it('cannot subscribe to inbox or response topics with a wildcard', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/+/in/foo` + }) + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/+/res/foo` + }) + }) + it('cannot subscribe to app owned device response topic', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceA.hashid}/res/foo` + }) + }) + it('cannot publish broadcast as an app owned device', async function () { + await denyWrite({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceA.hashid}/out/foo` + }) + }) + it('cannot publish to project inbox in another team', async function () { + const otherTeam = await factory.createTeam({ name: 'other-team' }) + const otherProject = await app.db.models.Project.create({ name: 'other-team-project', type: '', url: '' }) + await otherTeam.addProject(otherProject) + await denyWrite({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${otherProject.id}/in/foo` + }) + }) }) describe('assigned to application', async function () { before(async function () { @@ -978,12 +1109,18 @@ describe('Broker Auth API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/a/${TestObjects.ApplicationA.hashid}/command` }) }) - it('can subscribe to project inbox', async function () { - await allowRead({ + it('cannot subscribe to project inbox', async function () { + await denyRead({ username: deviceUsername, topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/in/foo` }) }) + it('cannot subscribe to project response', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/res/foo` + }) + }) it('can subscribe to project broadcast', async function () { await allowRead({ username: deviceUsername, @@ -1020,6 +1157,12 @@ describe('Broker Auth API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:invalid dev id/out/foo` }) }) + it('cannot publish broadcast as a project', async function () { + await denyWrite({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/out/foo` + }) + }) it('can publish broadcast with `dev:` topic prefix', async function () { await allowWrite({ username: deviceUsername, @@ -1089,6 +1232,49 @@ describe('Broker Auth API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/a/${TestObjects.ApplicationA.hashid}/p/${TestObjects.ProjectA.id}/out/foo` }) }) + it('can subscribe to own response topic', async function () { + await allowRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceA.hashid}/res/foo` + }) + }) + it('cannot subscribe to another device response topic', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceB.hashid}/res/foo` + }) + }) + it('can subscribe to app owned device broadcast', async function () { + await allowRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceB.hashid}/out/foo` + }) + }) + it('cannot subscribe to inbox or response topics with a wildcard', async function () { + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/+/in/foo` + }) + await denyRead({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/+/res/foo` + }) + }) + it('cannot publish broadcast as another device', async function () { + await denyWrite({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/dev:${TestObjects.DeviceB.hashid}/out/foo` + }) + }) + it('cannot publish to project inbox in another team', async function () { + const otherTeam = await factory.createTeam({ name: 'other-team-2' }) + const otherProject = await app.db.models.Project.create({ name: 'other-team-project-2', type: '', url: '' }) + await otherTeam.addProject(otherProject) + await denyWrite({ + username: deviceUsername, + topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${otherProject.id}/in/foo` + }) + }) }) describe('device log topics', async function () { it('can publish its logs', async function () { diff --git a/test/unit/forge/routes/api/team_spec.js b/test/unit/forge/routes/api/team_spec.js index d495ebf3f9..6200a81393 100644 --- a/test/unit/forge/routes/api/team_spec.js +++ b/test/unit/forge/routes/api/team_spec.js @@ -860,6 +860,10 @@ describe('Team API', function () { const result = response.json() result.should.have.property('projects').and.be.an.Array() result.projects.should.have.a.property('length', 4) + // Only the minimal information is returned to a device + result.projects.forEach(project => { + Object.keys(project).should.eql(['id', 'name']) + }) }) it('Device can not get a list of team projects without a valid token', async function () { From 967ae9e7963bd54363ee8dba335ba01b4e3d60b0 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Fri, 30 Oct 2026 13:04:55 +0000 Subject: [PATCH 2/2] test: Expect app owned devices to be denied instance inbox in v2 auth spec --- test/unit/forge/comms/authRoutesV2_spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/forge/comms/authRoutesV2_spec.js b/test/unit/forge/comms/authRoutesV2_spec.js index d948cb18c3..c860ca5168 100644 --- a/test/unit/forge/comms/authRoutesV2_spec.js +++ b/test/unit/forge/comms/authRoutesV2_spec.js @@ -895,8 +895,8 @@ describe('Broker Auth v2 API', async function () { topic: `ff/v1/${TestObjects.ATeam.hashid}/a/${TestObjects.ApplicationA.hashid}/command` }) }) - it('can subscribe to project inbox', async function () { - await allowRead({ + it('cannot subscribe to project inbox', async function () { + await denyRead({ username: deviceUsername, topic: `ff/v1/${TestObjects.ATeam.hashid}/p/${TestObjects.ProjectA.id}/in/foo` })