|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +/** |
| 5 | + * Token scope isolation tests. |
| 6 | + * |
| 7 | + * Validates that connect tokens (scoped to the instance prefix) cannot be |
| 8 | + * used to authorize channel subscriptions. Channel tokens scoped to a specific |
| 9 | + * channel should still work as connect tokens since they already prove the |
| 10 | + * holder has access to something within the instance. |
| 11 | + */ |
| 12 | +import { describe, it } from 'node:test'; |
| 13 | +import assert from 'node:assert'; |
| 14 | +import { mintChannelToken, mintConnectToken, validateChannelToken } from './utils.js'; |
| 15 | + |
| 16 | +const SECRET = 'test-secret-key'; |
| 17 | +const INSTANCE_PREFIX = 'myapp-rt'; |
| 18 | +const CHANNEL = `${INSTANCE_PREFIX}/chat/room-1`; |
| 19 | + |
| 20 | +describe('Token scope isolation: connect tokens cannot subscribe to channels', () => { |
| 21 | + it('channel token validates for its own channel', () => { |
| 22 | + const token = mintChannelToken(CHANNEL, SECRET); |
| 23 | + const result = validateChannelToken(token, SECRET, CHANNEL); |
| 24 | + assert.ok(result, 'channel token should validate for its specific channel'); |
| 25 | + assert.strictEqual(result!.channel, CHANNEL); |
| 26 | + }); |
| 27 | + |
| 28 | + it('connect token MUST NOT validate for a channel subscription', () => { |
| 29 | + const connectToken = mintConnectToken(INSTANCE_PREFIX, SECRET); |
| 30 | + // This is the critical assertion: a connect token should NOT authorize |
| 31 | + // subscription to a specific channel under the instance prefix. |
| 32 | + const result = validateChannelToken(connectToken, SECRET, CHANNEL); |
| 33 | + assert.strictEqual(result, null, 'connect token must NOT authorize channel subscriptions'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('connect token should not validate for any sub-channel', () => { |
| 37 | + const connectToken = mintConnectToken(INSTANCE_PREFIX, SECRET); |
| 38 | + // Try several channels under the instance prefix |
| 39 | + const channels = [ |
| 40 | + `${INSTANCE_PREFIX}/events/room-1`, |
| 41 | + `${INSTANCE_PREFIX}/notifications/user-123`, |
| 42 | + `${INSTANCE_PREFIX}/cursors/doc-456`, |
| 43 | + ]; |
| 44 | + for (const ch of channels) { |
| 45 | + const result = validateChannelToken(connectToken, SECRET, ch); |
| 46 | + assert.strictEqual(result, null, `connect token must NOT authorize ${ch}`); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it('channel token works as a connect token (no requestedChannel)', () => { |
| 51 | + const channelToken = mintChannelToken(CHANNEL, SECRET); |
| 52 | + // When validating for connection (no requestedChannel), any valid token |
| 53 | + // from this instance should be accepted |
| 54 | + const result = validateChannelToken(channelToken, SECRET); |
| 55 | + assert.ok(result, 'channel token should be valid as a connect token'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('new-style connect token validates without requestedChannel (connection use)', () => { |
| 59 | + const connectToken = mintConnectToken(INSTANCE_PREFIX, SECRET); |
| 60 | + // Connect tokens should still work for connection establishment |
| 61 | + const result = validateChannelToken(connectToken, SECRET); |
| 62 | + assert.ok(result, 'connect token should validate for connection establishment'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('channel token for one channel cannot subscribe to a different channel', () => { |
| 66 | + const token = mintChannelToken(`${INSTANCE_PREFIX}/chat/room-1`, SECRET); |
| 67 | + const result = validateChannelToken(token, SECRET, `${INSTANCE_PREFIX}/chat/room-2`); |
| 68 | + assert.strictEqual(result, null, 'channel token for room-1 must NOT authorize room-2'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('namespace-level token still works for sub-channels', () => { |
| 72 | + // A token scoped to a namespace (not the instance root) should still |
| 73 | + // authorize sub-channels within that namespace |
| 74 | + const nsToken = mintChannelToken(`${INSTANCE_PREFIX}/chat`, SECRET); |
| 75 | + const result = validateChannelToken(nsToken, SECRET, `${INSTANCE_PREFIX}/chat/room-1`); |
| 76 | + assert.ok(result, 'namespace token should authorize sub-channels'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('instance name containing $connect does not create ambiguity', () => { |
| 80 | + // If an instance is literally named "foo$connect", its connect token |
| 81 | + // channel field is "foo$connect$connect". This must NOT authorize |
| 82 | + // channels under the instance like "foo$connect/cursors/room-1". |
| 83 | + const weirdPrefix = 'foo$connect'; |
| 84 | + const connectToken = mintConnectToken(weirdPrefix, SECRET); |
| 85 | + const channel = `${weirdPrefix}/cursors/room-1`; |
| 86 | + const result = validateChannelToken(connectToken, SECRET, channel); |
| 87 | + assert.strictEqual(result, null, 'connect token for $connect-named instance must NOT authorize its channels'); |
| 88 | + |
| 89 | + // But it still validates for connection establishment |
| 90 | + const connResult = validateChannelToken(connectToken, SECRET); |
| 91 | + assert.ok(connResult, 'connect token should still validate for connection'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments