refactor: improve storage handling, UI feedback, and defaults
- Update default admin email to admin@datastack.dev - Improve storage flush logic with partial success handling - Enhance CSV download reliability using fetch and Blob - Adjust simulator protocol resolution and timing - Fix date range filtering in data API query
This commit is contained in:
1 parent
40a6855724
commit
3ad9b1d4d2
17 files changed
+281
-78
No files matched your search
@@ -24,7 +24,7 @@ import {
|
||||
verifyRequestAdminSession,
|
||||
} from "@/core/auth/adminUsers";
|
||||
import { AuthService, resolveDefaultJwtSecret } from "@/core/auth/jwt";
|
||||
import { isSameOriginRequest } from "@/middleware";
|
||||
import { isSameOriginRequest, middleware } from "@/middleware";
|
||||
|
||||
const ORIGINAL_ENV = { ...process.env };
|
||||
|
||||
@@ -337,4 +337,44 @@ describe("Phase-01 Auth, Secrets, Revocation & Proxy Hardening", () => {
|
||||
});
|
||||
assert.equal(isSameOriginRequest(reqMissingBoth), false);
|
||||
});
|
||||
|
||||
it("SPEC-08: allows headless scripts with x-datastack-admin-client or DataStack-CLI to bypass browser CSRF check", async () => {
|
||||
process.env.DATASTACK_ADMIN_SESSION_SECRET = "test-secret-at-least-32-chars-long-for-session";
|
||||
|
||||
// 1. Without headless header or origin -> 403 CSRF rejected
|
||||
const blockedReq = new NextRequest("http://localhost:3000/api/clients", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
host: "localhost:3000",
|
||||
},
|
||||
});
|
||||
const blockedRes = await middleware(blockedReq);
|
||||
assert.equal(blockedRes.status, 403);
|
||||
const blockedJson = await blockedRes.json();
|
||||
assert.match(blockedJson.error, /cross-origin/i);
|
||||
|
||||
// 2. With x-datastack-admin-client header -> bypasses CSRF (reaches auth check, returns 401 Unauthorized if no cookie)
|
||||
const headlessReq = new NextRequest("http://localhost:3000/api/clients", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
host: "localhost:3000",
|
||||
"x-datastack-admin-client": "true",
|
||||
},
|
||||
});
|
||||
const headlessRes = await middleware(headlessReq);
|
||||
assert.equal(headlessRes.status, 401);
|
||||
const headlessJson = await headlessRes.json();
|
||||
assert.match(headlessJson.error, /authentication required/i);
|
||||
|
||||
// 3. With DataStack-CLI User-Agent -> bypasses CSRF
|
||||
const cliReq = new NextRequest("http://localhost:3000/api/clients", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
host: "localhost:3000",
|
||||
"user-agent": "DataStack-CLI/1.0",
|
||||
},
|
||||
});
|
||||
const cliRes = await middleware(cliReq);
|
||||
assert.equal(cliRes.status, 401);
|
||||
});
|
||||
});
|
||||
@@ -508,4 +508,34 @@ test("BUG-14: GET /api/data searches eventId & clientId + UTC dates, GET /api/ev
|
||||
const eventsJson = await eventsRes.json();
|
||||
assert.equal(eventsJson.events.length, 1);
|
||||
assert.equal(eventsJson.events[0].eventId, "unique-findme-id-99");
|
||||
|
||||
// 6. SPEC-07: extractSafePartitionDate returns null on corrupt timestamp with null fallback
|
||||
assert.equal(CSVStorageManager.extractSafePartitionDate("not-a-timestamp", null), null);
|
||||
assert.equal(CSVStorageManager.extractSafePartitionDate("", null), null);
|
||||
assert.equal(CSVStorageManager.extractSafePartitionDate("2026-11-17T12:00:00.000Z", null), "2026-11-17");
|
||||
|
||||
// Corrupt timestamp in buffer must NOT leak into today's date range query
|
||||
await DataBufferEngine.pushBatchAsync([
|
||||
{
|
||||
clientId: "alpha_cli",
|
||||
eventId: "corrupt-ts-event",
|
||||
timestamp: "corrupted_raw_value",
|
||||
payload: JSON.stringify({ broken: true }),
|
||||
},
|
||||
]);
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
const dateRangeRes = await getDataRoute(
|
||||
new NextRequest(`http://localhost:3000/api/data?search=corrupt-ts-event&from=${today}&to=${today}`),
|
||||
);
|
||||
assert.equal(dateRangeRes.status, 200);
|
||||
const dateRangeJson = await dateRangeRes.json();
|
||||
assert.equal(dateRangeJson.total, 0, "Corrupt timestamp must NOT match date range");
|
||||
|
||||
// But without from/to date filters, corrupt event is still visible for admin inspection
|
||||
const allDataRes = await getDataRoute(
|
||||
new NextRequest("http://localhost:3000/api/data?search=corrupt-ts-event"),
|
||||
);
|
||||
assert.equal(allDataRes.status, 200);
|
||||
const allDataJson = await allDataRes.json();
|
||||
assert.equal(allDataJson.total, 1, "Corrupt timestamp event must be discoverable in unfiltered search");
|
||||
});
|
||||
@@ -79,10 +79,14 @@ class Phase05PythonSdkTests(unittest.TestCase):
|
||||
from datastack_sdk.exceptions import (
|
||||
DataStackOfflineSpoolError as ShimSpoolErr,
|
||||
)
|
||||
from datastack_sdk.heartbeat import HeartbeatMonitor as ShimHeartbeat
|
||||
from datastack_sdk.spool import DiskSpooler as ShimSpooler
|
||||
|
||||
self.assertIs(ShimClient, DataStackClient)
|
||||
self.assertIs(shim_load_config, load_config)
|
||||
self.assertIs(ShimSpoolErr, DataStackOfflineSpoolError)
|
||||
self.assertIs(ShimHeartbeat, HeartbeatMonitor)
|
||||
self.assertIs(ShimSpooler, DiskSpooler)
|
||||
|
||||
def test_client_id_config_env_and_jwt_validation(self) -> None:
|
||||
tok_alpha = _make_jwt({"clientId": "alpha_svc", "sub": "alpha_svc"})
|
||||
|
||||
Reference in new issue
Block a user