Skip to content

Commit 0bd179d

Browse files
authored
Merge pull request #52 from commjoen/hmac
Hmac for generating materials
2 parents b6cb357 + c503426 commit 0bd179d

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

wrongsecrets-balancer/src/teams/teams.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require('express');
22
const bcrypt = require('bcryptjs');
3+
const crypto = require('crypto');
34
const cryptoRandomString = require('crypto-random-string');
45

56
const Joi = require('@hapi/joi');
@@ -84,6 +85,28 @@ async function interceptAdminLogin(req, res, next) {
8485
return next();
8586
}
8687

88+
/**
89+
* @param {import("express").Request} req
90+
* @param {import("express").Response} res
91+
* @param {import("express").NextFunction} next
92+
*/
93+
async function validateHMAC(req, res, next) {
94+
try {
95+
const { team } = req.params;
96+
const { hmacvalue } = req.body;
97+
const validationValue = crypto
98+
.createHmac('sha256', 'hardcodedkey')
99+
.update(`${team}`, 'utf-8')
100+
.digest('hex');
101+
if (validationValue === hmacvalue) {
102+
return next();
103+
}
104+
res.status(403).send({ message: 'Invalid validation, please stop doing this!' });
105+
} catch (error) {
106+
res.status(500).send({ message: 'Invalid validation, please stop doing this!' });
107+
}
108+
}
109+
87110
/**
88111
* @param {import("express").Request} req
89112
* @param {import("express").Response} res
@@ -506,6 +529,7 @@ const paramsSchema = Joi.object({
506529
.regex(/^[a-z0-9]([-a-z0-9])+[a-z0-9]$/),
507530
});
508531
const bodySchema = Joi.object({
532+
hmacvalue: Joi.string().hex().length(64),
509533
passcode: Joi.string().alphanum().uppercase().length(8),
510534
});
511535

@@ -518,6 +542,7 @@ router.post(
518542
interceptAdminLogin,
519543
joinIfTeamAlreadyExists,
520544
checkIfMaxJuiceShopInstancesIsReached,
545+
validateHMAC,
521546
createTeam
522547
);
523548

wrongsecrets-balancer/src/teams/teams.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ test('create team creates a instance for team via k8s service', async () => {
146146

147147
await request(app)
148148
.post('/balancer/teams/team42/join')
149-
// .expect(200)
149+
.send({ hmacvalue: '4c8dd1f1306727c537aa96f0c59968b719740f2a30ccda92044ea59622565564' })
150+
.expect(200)
150151
.then(({ body }) => {
151152
expect(body.message).toBe('Created Instance');
152153
expect(body.passcode).toMatch(/[a-zA-Z0-9]{7}/);

wrongsecrets-balancer/ui/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wrongsecrets-balancer/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"@formatjs/intl-utils": "^3.8.4",
77
"axios": "^0.27.2",
8+
"crypto-js": "^4.1.1",
89
"promise-retry": "^2.0.1",
910
"react": "^18.2.0",
1011
"react-data-table-component": "^7.5.3",

wrongsecrets-balancer/ui/src/pages/JoinPage.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
22
import axios from 'axios';
33
import { useNavigate, useLocation } from 'react-router-dom';
44
import { FormattedMessage, defineMessages, injectIntl } from 'react-intl';
5+
import cryptoJS from 'crypto-js';
56

67
import styled from 'styled-components';
78

@@ -45,12 +46,15 @@ export const JoinPage = injectIntl(({ intl }) => {
4546
const { formatMessage } = intl;
4647

4748
async function sendJoinRequest() {
48-
if (window.confirm('Are you ready?')){
49+
if (window.confirm('Are you ready?')) {
4950
try {
51+
const hmacvalue = cryptoJS
52+
.HmacSHA256(`${teamname}`, 'hardcodedkey')
53+
.toString(cryptoJS.enc.Hex);
5054
const { data } = await axios.post(`/balancer/teams/${teamname}/join`, {
5155
passcode,
56+
hmacvalue,
5257
});
53-
5458
navigate(`/teams/${teamname}/joined/`, { state: { passcode: data.passcode } });
5559
} catch (error) {
5660
if (
@@ -62,7 +66,7 @@ export const JoinPage = injectIntl(({ intl }) => {
6266
setFailed(true);
6367
}
6468
}
65-
}
69+
}
6670
}
6771

6872
function onSubmit(event) {

0 commit comments

Comments
 (0)