Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the backend connectivity by removing whitelist and keeping Cors #40

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 1 addition & 43 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,9 @@ dotenv.config({
})
const app = express();

// Define the array of whitelisted IP addresses , the loopback will be allowed automatically
const whitelist = ['192.168.1.89', '172.21.0.1'];

// Middleware to check if the request's IP is in the whitelist
const ipWhitelistMiddleware = (req, res, next) => {
let clientIp = req.headers['x-real-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log("request from IP = "+clientIp);
// If the clientIp is in the IPv6 format "::ffff:", extract the IPv4 part
if (clientIp && clientIp.startsWith('::ffff:')) {
clientIp = clientIp.substr(7);
}

if (isLoopbackIp(clientIp) || (isValidIp(clientIp) && whitelist.includes(clientIp)) || isHealthCheck(req.url)) {
// If the IP is the loopback address or in the whitelist, allow the request to proceed
next();
} else {
// If the IP is not in the whitelist, send a 403 Forbidden response
res.status(403).json({ error: 'Access denied. IP address not whitelisted.' });
}
};

// Helper function to check if an IP address is the loopback address
function isLoopbackIp(ip) {
return ip === '::1' || ip === '127.0.0.1' || ip === '::ffff:127.0.0.1';
}

// Helper function to let health checks through
function isHealthCheck(path) {
return path === '/health';
}

// Helper function to validate if an IP address is valid
function isValidIp(ip) {
// Regular expression to validate IPv4 and IPv6 addresses, excluding loopback addresses
const ipv4Pattern = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const ipv6Pattern = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
return ipv4Pattern.test(ip) || ipv6Pattern.test(ip);
}

// Apply IP whitelist middleware before CORS middleware
app.use(ipWhitelistMiddleware);

// Configure CORS options
const corsOptions = {
origin: 'http://localhost:3000', // Replace with the actual frontend domain
origin: ['http://localhost:3000', 'https://nr-results-exam-test-frontend.apps.silver.devops.gov.bc.ca/'],
methods: ['GET','POST'], // Specify the allowed HTTP methods
};
app.use(express.json());
Expand Down
4 changes: 2 additions & 2 deletions frontend/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VITE_MAIN_VERSION = v1.0.0
VITE_USER_POOLS_ID= ca-central-1_FxagSNQa7
VITE_USER_POOLS_WEB_CLIENT_ID= 43hjhdf5dvi17opb0jgucfpvm7
VITE_USER_POOLS_WEB_CLIENT_ID= 3g6n2ha1loi4kp1jhaq359vrvb
VITE_CHES_FROM_EMAIL = [email protected]
VITE_CHES_ADMIN_EMAIL = [email protected]
VITE_ZONE = DEV
VITE_ZONE = DEV