Skip to content

Commit ef90e02

Browse files
VolcanoCookiesbirme
authored andcommitted
fix: always allow heartbeat path
1 parent 929eaa7 commit ef90e02

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
### Stateful Mode
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
@@ -358,21 +358,24 @@ export function authenticateToken(app: FastifyInstance): void {
358358
app.addHook(
359359
'onRequest',
360360
async (request, reply): Promise<RequestPayload> => {
361-
const token = request.query['token'];
362-
if (token == undefined)
363-
return reply.code(401).send({ error: 'No token provided' });
364-
try {
365-
const censoredUrl = request.url.replace(token, 'TOKEN');
366-
const decoded = jwt.verify(token, secret) as JwtToken;
367-
awsLogger.info(
368-
{ path: censoredUrl },
369-
request['awsLambda']?.['context'] as Context,
370-
decoded
371-
);
372-
} catch (err) {
373-
return reply
374-
.code(401)
375-
.send({ error: 'Invalid authentication token' });
361+
const path = request.raw.url.split('?')[0];
362+
if (path != '/') {
363+
const token = request.query['token'];
364+
if (token == undefined)
365+
return reply.code(401).send({ error: 'No token provided' });
366+
try {
367+
const censoredUrl = request.url.replace(token, 'TOKEN');
368+
const decoded = jwt.verify(token, secret) as JwtToken;
369+
awsLogger.info(
370+
{ path: censoredUrl },
371+
request['awsLambda']?.['context'] as Context,
372+
decoded
373+
);
374+
} catch (err) {
375+
return reply
376+
.code(401)
377+
.send({ error: 'Invalid authentication token' });
378+
}
376379
}
377380
}
378381
);

0 commit comments

Comments
 (0)