Skip to content
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
6 changes: 5 additions & 1 deletion frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}
const nextConfig = {
env: {
NEXT_PUBLIC_API_KEY: process.env.NEXT_PUBLIC_API_KEY,
},
};

module.exports = nextConfig
15 changes: 14 additions & 1 deletion frontend/user_login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, useEffect } from "react";
import axios from "axios";

const BASE_URL = process.env.NEXT_PUBLIC_BACKEND_URL || "/api";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P0] Avoid exposing server API key to the browser

Declaring NEXT_PUBLIC_API_KEY and attaching it to Axios defaults means the admin API key is bundled into the client and sent on every request. In backend/middleware/authMiddleware.js the middleware grants access when either an x-api-key or a JWT is present, so anyone who loads the site can read the API key from the bundle and call /admin/* routes without logging in. This effectively bypasses authentication for the admin API. The key should stay server-side or the middleware must require a token even when a key is present.

Useful? React with 👍 / 👎.

const API_KEY = process.env.NEXT_PUBLIC_API_KEY;

export default function AuthPage() {
const [form, setForm] = useState({ email: "", password: "" });
Expand All @@ -15,6 +16,10 @@ export default function AuthPage() {
useEffect(() => {
const token = localStorage.getItem("token");
if (token) {
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
if (API_KEY) {
axios.defaults.headers.common["x-api-key"] = API_KEY;
}
setAdminView(true);
fetchUsers();
fetchKeys();
Expand All @@ -27,8 +32,16 @@ export default function AuthPage() {

const handleLogin = async () => {
try {
const res = await axios.post(`${BASE_URL}/auth/login`, form);
const res = await axios.post(
`${BASE_URL}/auth/login`,
form,
API_KEY ? { headers: { "x-api-key": API_KEY } } : undefined
);
localStorage.setItem("token", res.data.token);
axios.defaults.headers.common["Authorization"] = `Bearer ${res.data.token}`;
if (API_KEY) {
axios.defaults.headers.common["x-api-key"] = API_KEY;
}
setMessage("Login successful!");
setAdminView(true);
fetchUsers();
Expand Down