feat(api): require an admin token for configuration changes
Group CRUD decides what the scanner sweeps, yet any client that reached the API could change it. Writes now need XEXEU_ADMIN_TOKEN (or a token generated at startup and printed as a ?token= link, like sabia ui), compared in constant time, and a browser Origin must match Host. The dashboard keeps the token for the tab and asks for it on 401. Reads are unchanged. Also track .env.example: '.env*' in .gitignore had kept it out of the repository, although the README tells operators to copy it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
36eadabc7c
commit
90ece32dac
17 files changed
+369
-28
No files matched your search
+34
-3
@@ -4,6 +4,36 @@ const SVG_NS = 'http://www.w3.org/2000/svg';
|
||||
const $ = (selector) => document.querySelector(selector);
|
||||
const $$ = (selector) => document.querySelectorAll(selector);
|
||||
|
||||
// Admin token for the routes that change state. `xexeu serve` prints a link
|
||||
// with `?token=`; it is kept for this tab only and dropped from the address bar.
|
||||
const TOKEN_KEY = 'xexeu.adminToken';
|
||||
{
|
||||
const params = new URLSearchParams(location.search);
|
||||
if (params.has('token')) {
|
||||
sessionStorage.setItem(TOKEN_KEY, params.get('token'));
|
||||
params.delete('token');
|
||||
const query = params.toString();
|
||||
history.replaceState(null, '', location.pathname + (query ? `?${query}` : '') + location.hash);
|
||||
}
|
||||
}
|
||||
|
||||
// fetch() for writes: sends the admin token and, on 401, asks for it once and retries.
|
||||
async function adminFetch(url, options = {}) {
|
||||
const send = () => fetch(url, {
|
||||
...options,
|
||||
headers: { ...options.headers, Authorization: `Bearer ${sessionStorage.getItem(TOKEN_KEY) || ''}` },
|
||||
});
|
||||
let res = await send();
|
||||
if (res.status === 401) {
|
||||
const token = window.prompt(t('Token de administração (exibido no log do xexeu serve ou definido em XEXEU_ADMIN_TOKEN):'));
|
||||
if (token && token.trim()) {
|
||||
sessionStorage.setItem(TOKEN_KEY, token.trim());
|
||||
res = await send();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// DOM Elements
|
||||
const connection = $('#connection');
|
||||
const connectionText = $('#connectionText');
|
||||
@@ -253,6 +283,7 @@ async function fetchData() {
|
||||
connectionText.textContent = t('Conectado');
|
||||
if (state.health && state.health.version) {
|
||||
versionEl.textContent = `Xexéu v${state.health.version}`;
|
||||
$('#versionPill').textContent = `v${state.health.version}`;
|
||||
}
|
||||
if (state.status) {
|
||||
feedsStatusEl.textContent = `${formatInt(state.status.feeds_cve_count || 0)} CVEs`;
|
||||
@@ -1211,13 +1242,13 @@ async function handleGroupFormSubmit(e) {
|
||||
try {
|
||||
let res;
|
||||
if (editingGroupName) {
|
||||
res = await fetch(`/api/config/groups/${encodeURIComponent(editingGroupName)}`, {
|
||||
res = await adminFetch(`/api/config/groups/${encodeURIComponent(editingGroupName)}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
} else {
|
||||
res = await fetch('/api/config/groups', {
|
||||
res = await adminFetch('/api/config/groups', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
@@ -1246,7 +1277,7 @@ async function deleteGroup(name) {
|
||||
if (!confirmed) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/config/groups/${encodeURIComponent(name)}`, {
|
||||
const res = await adminFetch(`/api/config/groups/${encodeURIComponent(name)}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (!res.ok) {
|
||||
|
||||
Reference in new issue
Block a user