Skip to content

Commit 07efb6b

Browse files
fix: always allow heartbeat path
1 parent ed170bb commit 07efb6b

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ To try it out, go to your favourite HLS/MPEG-DASH video player such as `https://
7070

7171
### Environmental Variales
7272

73-
| VARIABLE | TYPE | DESCRIPTION |
74-
| -------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------- |
75-
| `JWT_SECRET` | string | Enables jwt authentication for all endpoints and logs requests from users, provide token with the `token` query parameter. |
76-
| `LOAD_PARAMS_FROM_AWS_SSM` | boolean | Load manifest url params from AWS SSM, [see below](#load-manifest-url-params-from-aws-ssm-parameter-store-instead) |
77-
| `AWS_REGION` | string | AWS region for SSM parameters, no effect if `LOAD_PARAMS_FROM_AWS_SSM` is false |
78-
| `AWS_SSM_PARAM_KEY` | string | Key for AWS SSM params, no effect if `LOAD_PARAMS_FROM_AWS_SSM` is false |
73+
| VARIABLE | TYPE | DESCRIPTION |
74+
| -------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
75+
| `JWT_SECRET` | string | Enables jwt authentication for all endpoints and logs requests from users, provide token with the `token` query parameter. Heartbeat path unaffected. |
76+
| `LOAD_PARAMS_FROM_AWS_SSM` | boolean | Load manifest url params from AWS SSM, [see below](#load-manifest-url-params-from-aws-ssm-parameter-store-instead) |
77+
| `AWS_REGION` | string | AWS region for SSM parameters, no effect if `LOAD_PARAMS_FROM_AWS_SSM` is false |
78+
| `AWS_SSM_PARAM_KEY` | string | Key for AWS SSM params, no effect if `LOAD_PARAMS_FROM_AWS_SSM` is false |
7979

8080
### Load Manifest url params from AWS SSM parameter store instead
8181

src/server.test.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,27 @@ describe('Chaos Stream Proxy server', () => {
3232
}
3333
);
3434

35-
it('requires token when running with env JWT_SECRET set', async () => {
35+
it('requires token when running with env JWT_SECRET set, except for heartbeat path', async () => {
36+
// Arrange
3637
process.env.JWT_SECRET = 'somesecret';
3738
const appInternal = fastify();
3839
registerRoutes(appInternal);
39-
const invalidResponse = await appInternal.inject('/?token=invalid');
40-
expect(invalidResponse.statusCode).toEqual(401);
40+
41+
// Act
42+
const invalidResponse = await appInternal.inject(
43+
'/api/v2/manifests/dash/proxy-master.mpd?token=invalid'
44+
);
4145

4246
const validResponse = await appInternal.inject(
43-
'/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb21wYW55IjoidGVzdGNvbXBhbnkiLCJlbWFpbCI6InRlc3RAZW1haWwuY29tIiwiaWF0IjoxNjg2MTUzMzU5fQ.wHnzxMdoPZlzdU0GDCzEwd5lnEmq-rX2Ew0yODxqlzg'
47+
'/api/v2/manifests/dash/proxy-master.mpd?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb21wYW55IjoidGVzdGNvbXBhbnkiLCJlbWFpbCI6InRlc3RAZW1haWwuY29tIiwiaWF0IjoxNjg2MTUzMzU5fQ.wHnzxMdoPZlzdU0GDCzEwd5lnEmq-rX2Ew0yODxqlzg'
4448
);
45-
expect(validResponse.statusCode).toEqual(200);
49+
50+
const allowHeartbeatAlways = await appInternal.inject('/');
51+
52+
// Assert
53+
expect(invalidResponse.statusCode).toEqual(401);
54+
expect(validResponse.statusCode).toEqual(400);
55+
expect(allowHeartbeatAlways.statusCode).toEqual(200);
4656
});
4757

4858
it('ignores token when running without env JWT_SECRET set', async () => {

src/shared/utils.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -305,21 +305,24 @@ export function authenticateToken(app: FastifyInstance): void {
305305
app.addHook(
306306
'onRequest',
307307
async (request, reply): Promise<RequestPayload> => {
308-
const token = request.query['token'];
309-
if (token == undefined)
310-
return reply.code(401).send({ error: 'No token provided' });
311-
try {
312-
const censoredUrl = request.url.replace(token, 'TOKEN');
313-
const decoded = jwt.verify(token, secret) as JwtToken;
314-
awsLogger.info(
315-
{ path: censoredUrl },
316-
request['awsLambda']?.['context'] as Context,
317-
decoded
318-
);
319-
} catch (err) {
320-
return reply
321-
.code(401)
322-
.send({ error: 'Invalid authentication token' });
308+
const path = request.raw.url.split('?')[0];
309+
if (path != '/') {
310+
const token = request.query['token'];
311+
if (token == undefined)
312+
return reply.code(401).send({ error: 'No token provided' });
313+
try {
314+
const censoredUrl = request.url.replace(token, 'TOKEN');
315+
const decoded = jwt.verify(token, secret) as JwtToken;
316+
awsLogger.info(
317+
{ path: censoredUrl },
318+
request['awsLambda']?.['context'] as Context,
319+
decoded
320+
);
321+
} catch (err) {
322+
return reply
323+
.code(401)
324+
.send({ error: 'Invalid authentication token' });
325+
}
323326
}
324327
}
325328
);

0 commit comments

Comments
 (0)