Skip to content

Commit 5e8c155

Browse files
committed
require an hmac to generate when we want to create infrastructure as an anti-fuzzing control
1 parent 2484b8b commit 5e8c155

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

wrongsecrets-balancer/src/teams/teams.js

Lines changed: 26 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,29 @@ 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+
}
110+
87111
/**
88112
* @param {import("express").Request} req
89113
* @param {import("express").Response} res
@@ -506,6 +530,7 @@ const paramsSchema = Joi.object({
506530
.regex(/^[a-z0-9]([-a-z0-9])+[a-z0-9]$/),
507531
});
508532
const bodySchema = Joi.object({
533+
hmacvalue: Joi.string().hex().length(64),
509534
passcode: Joi.string().alphanum().uppercase().length(8),
510535
});
511536

@@ -518,6 +543,7 @@ router.post(
518543
interceptAdminLogin,
519544
joinIfTeamAlreadyExists,
520545
checkIfMaxJuiceShopInstancesIsReached,
546+
validateHMAC,
521547
createTeam
522548
);
523549

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: 5 additions & 1 deletion
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

@@ -46,10 +47,13 @@ export const JoinPage = injectIntl(({ intl }) => {
4647

4748
async function sendJoinRequest() {
4849
try {
50+
const hmacvalue = cryptoJS
51+
.HmacSHA256(`${teamname}`, 'hardcodedkey')
52+
.toString(cryptoJS.enc.Hex);
4953
const { data } = await axios.post(`/balancer/teams/${teamname}/join`, {
5054
passcode,
55+
hmacvalue,
5156
});
52-
5357
navigate(`/teams/${teamname}/joined/`, { state: { passcode: data.passcode } });
5458
} catch (error) {
5559
if (

0 commit comments

Comments
 (0)