diff --git a/src/jwt-middleware.ts b/src/jwt-middleware.ts index bb4425982..1c1dc5efa 100644 --- a/src/jwt-middleware.ts +++ b/src/jwt-middleware.ts @@ -79,14 +79,25 @@ export function scopeAllowsRequest(scope: string | undefined, request: express.R const access = parts[1]; const readwrite = access === Constants.READWRITE; path = parts[0]; + + if (requestPath === Constants.THINGS_PATH) { + const allowedCollectionRead = + path === Constants.THINGS_PATH || path.startsWith(Constants.THINGS_PATH); + if (!allowedCollectionRead) { + continue; + } + if (request.method === 'GET' || request.method === 'OPTIONS') { + return true; + } + return path === Constants.THINGS_PATH && readwrite; + } + const allowedDirect = requestPath.startsWith(path); - const allowedThings = - requestPath === Constants.THINGS_PATH && path.startsWith(Constants.THINGS_PATH); // Allow access to media only if scope covers all things const allowedMedia = requestPath.startsWith(Constants.MEDIA_PATH) && path === Constants.THINGS_PATH; - if (allowedDirect || allowedThings || allowedMedia) { + if (allowedDirect || allowedMedia) { if (!readwrite && request.method !== 'GET' && request.method !== 'OPTIONS') { return false; } diff --git a/src/test/integration/oauth-test.ts b/src/test/integration/oauth-test.ts index a86f1aed5..ecbd9ab8b 100644 --- a/src/test/integration/oauth-test.ts +++ b/src/test/integration/oauth-test.ts @@ -508,6 +508,78 @@ describe('oauth/', function () { expect(err.get('www-authenticate')).toEqual(expect.stringContaining('insufficient_scope')); }); + it('rejects collection writes for a scoped thing token', async () => { + setupOAuth(); + await addDevice(); + await addDevice({ + ...TEST_THING, + id: 'test-2', + title: 'test-2', + }); + + const res = await chai + .request(server) + .get( + `${Constants.OAUTH_PATH}/allow?${qs.stringify({ + response_type: 'code', + client_id: CLIENT_ID, + scope: '/things/test-1:readwrite', + state: REQUEST_STATE, + })}` + ) + .set('Accept', 'application/json') + .set(...headerAuth(userJWT)); + + const jwt = res.body.token.access_token; + + const listRes = await chai + .request(server) + .get(Constants.THINGS_PATH) + .set('Accept', 'application/json') + .set(...headerAuth(jwt)); + expect(listRes.status).toEqual(200); + expect(listRes.body.length).toEqual(1); + expect(listRes.body[0].href).toEqual('/things/test-1'); + + const pinErr = await chai + .request(server) + .patch(Constants.THINGS_PATH) + .set('Accept', 'application/json') + .set(...headerAuth(jwt)) + .send({ thingId: 'test-2', pin: '1234' }); + expect(pinErr.status).toEqual(403); + expect(pinErr.header).toHaveProperty('www-authenticate'); + expect(pinErr.get('www-authenticate')).toEqual(expect.stringContaining('insufficient_scope')); + + const credentialsErr = await chai + .request(server) + .patch(Constants.THINGS_PATH) + .set('Accept', 'application/json') + .set(...headerAuth(jwt)) + .send({ thingId: 'test-2', username: 'user', password: 'pass' }); + expect(credentialsErr.status).toEqual(403); + + const createErr = await chai + .request(server) + .post(Constants.THINGS_PATH) + .set('Accept', 'application/json') + .set(...headerAuth(jwt)) + .send({ + id: 'test-3', + title: 'test-3', + '@context': ['https://www.w3.org/2022/wot/td/v1.1', 'https://webthings.io/schemas'], + '@type': ['OnOffSwitch'], + properties: { + power: { + '@type': 'OnOffProperty', + type: 'boolean', + value: false, + }, + }, + }); + expect(createErr.status).toEqual(403); + }); + it('rejects use of authorization code as access token', async () => { setupOAuth({}, function customCallbackHandler(req: express.Request, res: express.Response) { const code = req.query.code;