Skip to content

Commit 00f72d2

Browse files
committed
Require a
collection-wide /things:readwrite grant to PATCH or POST /things - closes #3332
1 parent ea50ccb commit 00f72d2

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎src/jwt-middleware.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,25 @@ export function scopeAllowsRequest(scope: string | undefined, request: express.R
7979
const access = parts[1];
8080
const readwrite = access === Constants.READWRITE;
8181
path = parts[0];
82+
83+
if (requestPath === Constants.THINGS_PATH) {
84+
const allowedCollectionRead =
85+
path === Constants.THINGS_PATH || path.startsWith(Constants.THINGS_PATH);
86+
if (!allowedCollectionRead) {
87+
continue;
88+
}
89+
if (request.method === 'GET' || request.method === 'OPTIONS') {
90+
return true;
91+
}
92+
return path === Constants.THINGS_PATH && readwrite;
93+
}
94+
8295
const allowedDirect = requestPath.startsWith(path);
83-
const allowedThings =
84-
requestPath === Constants.THINGS_PATH && path.startsWith(Constants.THINGS_PATH);
8596
// Allow access to media only if scope covers all things
8697
const allowedMedia =
8798
requestPath.startsWith(Constants.MEDIA_PATH) && path === Constants.THINGS_PATH;
8899

89-
if (allowedDirect || allowedThings || allowedMedia) {
100+
if (allowedDirect || allowedMedia) {
90101
if (!readwrite && request.method !== 'GET' && request.method !== 'OPTIONS') {
91102
return false;
92103
}

‎src/test/integration/oauth-test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,78 @@ describe('oauth/', function () {
508508
expect(err.get('www-authenticate')).toEqual(expect.stringContaining('insufficient_scope'));
509509
});
510510

511+
it('rejects collection writes for a scoped thing token', async () => {
512+
setupOAuth();
513+
await addDevice();
514+
await addDevice({
515+
...TEST_THING,
516+
id: 'test-2',
517+
title: 'test-2',
518+
});
519+
520+
const res = await chai
521+
.request(server)
522+
.get(
523+
`${Constants.OAUTH_PATH}/allow?${qs.stringify({
524+
response_type: 'code',
525+
client_id: CLIENT_ID,
526+
scope: '/things/test-1:readwrite',
527+
state: REQUEST_STATE,
528+
})}`
529+
)
530+
.set('Accept', 'application/json')
531+
.set(...headerAuth(userJWT));
532+
533+
const jwt = res.body.token.access_token;
534+
535+
const listRes = await chai
536+
.request(server)
537+
.get(Constants.THINGS_PATH)
538+
.set('Accept', 'application/json')
539+
.set(...headerAuth(jwt));
540+
expect(listRes.status).toEqual(200);
541+
expect(listRes.body.length).toEqual(1);
542+
expect(listRes.body[0].href).toEqual('/things/test-1');
543+
544+
const pinErr = await chai
545+
.request(server)
546+
.patch(Constants.THINGS_PATH)
547+
.set('Accept', 'application/json')
548+
.set(...headerAuth(jwt))
549+
.send({ thingId: 'test-2', pin: '1234' });
550+
expect(pinErr.status).toEqual(403);
551+
expect(pinErr.header).toHaveProperty('www-authenticate');
552+
expect(pinErr.get('www-authenticate')).toEqual(expect.stringContaining('insufficient_scope'));
553+
554+
const credentialsErr = await chai
555+
.request(server)
556+
.patch(Constants.THINGS_PATH)
557+
.set('Accept', 'application/json')
558+
.set(...headerAuth(jwt))
559+
.send({ thingId: 'test-2', username: 'user', password: 'pass' });
560+
expect(credentialsErr.status).toEqual(403);
561+
562+
const createErr = await chai
563+
.request(server)
564+
.post(Constants.THINGS_PATH)
565+
.set('Accept', 'application/json')
566+
.set(...headerAuth(jwt))
567+
.send({
568+
id: 'test-3',
569+
title: 'test-3',
570+
'@context': ['https://www.w3.org/2022/wot/td/v1.1', 'https://webthings.io/schemas'],
571+
'@type': ['OnOffSwitch'],
572+
properties: {
573+
power: {
574+
'@type': 'OnOffProperty',
575+
type: 'boolean',
576+
value: false,
577+
},
578+
},
579+
});
580+
expect(createErr.status).toEqual(403);
581+
});
582+
511583
it('rejects use of authorization code as access token', async () => {
512584
setupOAuth({}, function customCallbackHandler(req: express.Request, res: express.Response) {
513585
const code = req.query.code;

0 commit comments

Comments
 (0)