Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+76
-95
@@ -1,48 +1,53 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: {
|
||||
ipAddress: {
|
||||
findUnique: vi.fn(),
|
||||
upsert: vi.fn(),
|
||||
},
|
||||
ipAddressLog: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
},
|
||||
vi.mock("@hivegrid/db/pretenant-security.server", () => ({
|
||||
appendIpAddressLogCapability: vi.fn(),
|
||||
findIpAddressCapability: vi.fn(),
|
||||
upsertIpAddressCapability: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/cache.server", () => ({
|
||||
cachified: vi.fn(({ getFreshValue }) => getFreshValue()),
|
||||
clearCacheKey: vi.fn(),
|
||||
}));
|
||||
vi.mock("~/utils/server/IpUtils", () => ({ getClientIPAddress: vi.fn(() => "203.0.113.10") }));
|
||||
vi.mock("~/utils/server/IpUtils", () => ({ getTrustedClientIpOrNull: vi.fn(() => "203.0.113.10") }));
|
||||
vi.mock("~/utils/db/blacklist.db.server", () => ({
|
||||
addBlacklistAttempt: vi.fn(),
|
||||
addToBlacklist: vi.fn(),
|
||||
findInBlacklist: vi.fn(() => null),
|
||||
}));
|
||||
// AD-22: GeoIP/Hashing wird hermetisch gemockt — hashIp ist deterministisch, Land-Auflösung ein No-op.
|
||||
// AD-22: Keep GeoIP and hashing hermetic while preserving deterministic hashes.
|
||||
vi.mock("./geoip.server", () => ({
|
||||
hashIp: (ip: string) => `hash(${ip})`,
|
||||
hashIp: (ip: string) => `hash-${ip.length}`,
|
||||
lookupCountry: vi.fn(async () => null),
|
||||
}));
|
||||
|
||||
import IpAddressServiceServer from "./IpAddressService.server";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { appendIpAddressLogCapability, findIpAddressCapability, upsertIpAddressCapability } from "@hivegrid/db/pretenant-security.server";
|
||||
import { addBlacklistAttempt, findInBlacklist } from "~/utils/db/blacklist.db.server";
|
||||
import IpAddressServiceServer from "./IpAddressService.server";
|
||||
|
||||
const RAW_IP = "203.0.113.10";
|
||||
const HASHED_IP = `hash(${RAW_IP})`;
|
||||
const HASHED_IP = `hash-${RAW_IP.length}`;
|
||||
const ipRecord = {
|
||||
id: "ip_1",
|
||||
ip: HASHED_IP,
|
||||
provider: "",
|
||||
type: "",
|
||||
countryCode: "",
|
||||
countryName: "",
|
||||
regionCode: "",
|
||||
regionName: "",
|
||||
city: "",
|
||||
zipCode: "",
|
||||
latitude: null,
|
||||
longitude: null,
|
||||
metadata: "null",
|
||||
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
} as Awaited<ReturnType<typeof upsertIpAddressCapability>>;
|
||||
|
||||
const dbMock = db as unknown as {
|
||||
ipAddress: {
|
||||
findUnique: ReturnType<typeof vi.fn>;
|
||||
upsert: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
ipAddressLog: {
|
||||
create: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
};
|
||||
const appendLogMock = vi.mocked(appendIpAddressLogCapability);
|
||||
const findIpAddressMock = vi.mocked(findIpAddressCapability);
|
||||
const upsertIpAddressMock = vi.mocked(upsertIpAddressCapability);
|
||||
const findInBlacklistMock = vi.mocked(findInBlacklist);
|
||||
const addBlacklistAttemptMock = vi.mocked(addBlacklistAttempt);
|
||||
|
||||
@@ -50,103 +55,79 @@ describe("IpAddressServiceServer", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
findInBlacklistMock.mockResolvedValue(null);
|
||||
dbMock.ipAddress.findUnique.mockResolvedValue(null);
|
||||
dbMock.ipAddress.upsert.mockResolvedValue({
|
||||
id: "ip_1",
|
||||
findIpAddressMock.mockResolvedValue(null);
|
||||
upsertIpAddressMock.mockResolvedValue(ipRecord);
|
||||
appendLogMock.mockResolvedValue({ id: "log_1" } as Awaited<ReturnType<typeof appendIpAddressLogCapability>>);
|
||||
});
|
||||
|
||||
it("persists only the IP hash through pre-tenant capabilities (AD-22)", async () => {
|
||||
const request = new Request("https://admin.hivegrid.de/login");
|
||||
|
||||
await expect(IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })).resolves.toBeUndefined();
|
||||
|
||||
expect(findIpAddressMock).toHaveBeenCalledWith(HASHED_IP);
|
||||
expect(upsertIpAddressMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
ip: HASHED_IP,
|
||||
metadata: "null",
|
||||
})
|
||||
);
|
||||
expect(appendLogMock).toHaveBeenCalledWith({
|
||||
ipAddressId: "ip_1",
|
||||
ip: HASHED_IP,
|
||||
provider: "",
|
||||
type: "",
|
||||
countryCode: "",
|
||||
countryName: "",
|
||||
regionCode: "",
|
||||
regionName: "",
|
||||
city: "",
|
||||
zipCode: "",
|
||||
latitude: null,
|
||||
longitude: null,
|
||||
metadata: "null",
|
||||
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
url: "/login",
|
||||
action: "login",
|
||||
description: "admin@example.com",
|
||||
metadata: null,
|
||||
error: null,
|
||||
success: true,
|
||||
});
|
||||
dbMock.ipAddressLog.create.mockResolvedValue({ id: "log_1" });
|
||||
expect(JSON.stringify(upsertIpAddressMock.mock.calls)).not.toContain(RAW_IP);
|
||||
expect(JSON.stringify(appendLogMock.mock.calls)).not.toContain(RAW_IP);
|
||||
});
|
||||
|
||||
it("speichert nur den IP-Hash, nie die Klartext-IP (AD-22)", async () => {
|
||||
it("does not replace the request IP with a fixed development address", async () => {
|
||||
const request = new Request("https://admin.hivegrid.de/login");
|
||||
|
||||
await expect(
|
||||
IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })
|
||||
).resolves.toBeUndefined();
|
||||
await expect(IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })).resolves.toBeUndefined();
|
||||
|
||||
expect(dbMock.ipAddress.upsert).toHaveBeenCalledWith({
|
||||
where: { ip: HASHED_IP },
|
||||
update: {},
|
||||
create: expect.objectContaining({ ip: HASHED_IP }),
|
||||
});
|
||||
const logArgs = dbMock.ipAddressLog.create.mock.calls[0][0];
|
||||
expect(logArgs.data.ip).toBe(HASHED_IP);
|
||||
expect(logArgs.data).toMatchObject({ ipAddressId: "ip_1", url: "/login", action: "login", success: true });
|
||||
expect(upsertIpAddressMock).toHaveBeenCalledWith(expect.objectContaining({ ip: HASHED_IP }));
|
||||
expect(upsertIpAddressMock).not.toHaveBeenCalledWith(expect.objectContaining({ ip: "hash-14" }));
|
||||
});
|
||||
|
||||
it("ersetzt die Request-IP nicht durch eine fixe Dev-Adresse", async () => {
|
||||
const request = new Request("https://admin.hivegrid.de/login");
|
||||
|
||||
await expect(
|
||||
IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
expect(dbMock.ipAddress.upsert).toHaveBeenCalledWith(expect.objectContaining({ where: { ip: HASHED_IP } }));
|
||||
expect(dbMock.ipAddress.upsert).not.toHaveBeenCalledWith(expect.objectContaining({ where: { ip: "hash(189.203.100.82)" } }));
|
||||
});
|
||||
|
||||
it("speichert stabile blacklisted-Fehlercodes mit gehashter IP", async () => {
|
||||
it("persists stable blacklisted error codes with a hashed IP", async () => {
|
||||
findInBlacklistMock.mockResolvedValue({ id: "blacklist_1" } as never);
|
||||
const request = new Request("https://admin.hivegrid.de/login");
|
||||
|
||||
await expect(
|
||||
IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })
|
||||
).rejects.toThrow("blacklisted");
|
||||
await expect(IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })).rejects.toThrow("blacklisted");
|
||||
|
||||
expect(addBlacklistAttemptMock).toHaveBeenCalledWith({ id: "blacklist_1" });
|
||||
expect(dbMock.ipAddressLog.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
expect(appendLogMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
ip: HASHED_IP,
|
||||
error: "blacklisted",
|
||||
success: false,
|
||||
}),
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("speichert stabile unauthorized-Fehlercodes für provider-abgelehnte IP-Log-Zeilen", async () => {
|
||||
dbMock.ipAddress.findUnique.mockResolvedValue({
|
||||
id: "ip_1",
|
||||
ip: HASHED_IP,
|
||||
it("persists stable unauthorized codes for provider-rejected IPs", async () => {
|
||||
findIpAddressMock.mockResolvedValue({
|
||||
...ipRecord,
|
||||
provider: "ipapi.is",
|
||||
type: "",
|
||||
countryCode: "",
|
||||
countryName: "",
|
||||
regionCode: "",
|
||||
regionName: "",
|
||||
city: "",
|
||||
zipCode: "",
|
||||
latitude: null,
|
||||
longitude: null,
|
||||
metadata: JSON.stringify({ is_crawler: true }),
|
||||
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
||||
});
|
||||
const request = new Request("https://admin.hivegrid.de/login");
|
||||
|
||||
await expect(
|
||||
IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })
|
||||
).rejects.toThrow("unauthorized");
|
||||
await expect(IpAddressServiceServer.log(request, { action: "login", description: "admin@example.com" })).rejects.toThrow("unauthorized");
|
||||
|
||||
expect(dbMock.ipAddressLog.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
expect(upsertIpAddressMock).not.toHaveBeenCalled();
|
||||
expect(appendLogMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
ip: HASHED_IP,
|
||||
error: "unauthorized",
|
||||
success: false,
|
||||
}),
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
+32
-65
@@ -1,10 +1,10 @@
|
||||
import { getClientIPAddress } from "~/utils/server/IpUtils";
|
||||
import { getTrustedClientIpOrNull } from "~/utils/server/IpUtils";
|
||||
import { IpAddressDto } from "../dtos/IpAddressDto";
|
||||
import { cachified, clearCacheKey } from "~/utils/cache.server";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { addBlacklistAttempt, addToBlacklist, findInBlacklist } from "~/utils/db/blacklist.db.server";
|
||||
import { createScopedLogger } from "~/utils/logger.server";
|
||||
import type { IpAddress } from "@hivegrid/db/prisma-browser";
|
||||
import { appendIpAddressLogCapability, findIpAddressCapability, upsertIpAddressCapability } from "@hivegrid/db/pretenant-security.server";
|
||||
import { hashIp, lookupCountry } from "./geoip.server";
|
||||
|
||||
const ipAddressLogger = createScopedLogger("ipAddress");
|
||||
@@ -25,8 +25,8 @@ async function log(
|
||||
block?: IpAddressLogErrorCode;
|
||||
}
|
||||
) {
|
||||
const ip = getClientIPAddress(request)?.toString() || LOCAL_REQUEST_IP;
|
||||
// AD-22: nur der Pseudonym-Hash wird persistiert, nie die Klartext-IP.
|
||||
const ip = getTrustedClientIpOrNull(request) || LOCAL_REQUEST_IP;
|
||||
// AD-22: Persist only the pseudonymous hash, never the plain-text IP address.
|
||||
const storedIp = hashIp(ip);
|
||||
const ipAddress = await getOrCreateIpAddressLookup(ip);
|
||||
if (!ipAddress) {
|
||||
@@ -35,57 +35,49 @@ async function log(
|
||||
|
||||
const validationError = getIpAddressValidationError(ipAddress);
|
||||
|
||||
// Blacklist-Abgleich auf der Roh-IP — reine Laufzeit-Security-Liste, wird nicht in den Logs persistiert.
|
||||
// Match the runtime security list against the raw IP without persisting it in logs.
|
||||
let blacklistedIp = await findInBlacklist("ip", ip);
|
||||
if (!blacklistedIp && block) {
|
||||
blacklistedIp = await addToBlacklist({ type: "ip", value: ip });
|
||||
}
|
||||
const error = blacklistedIp ? "blacklisted" : validationError ?? block ?? null;
|
||||
const error = blacklistedIp ? "blacklisted" : (validationError ?? block ?? null);
|
||||
if (error) {
|
||||
if (blacklistedIp) {
|
||||
await addBlacklistAttempt(blacklistedIp);
|
||||
}
|
||||
await db.ipAddressLog.create({
|
||||
data: {
|
||||
ipAddressId: ipAddress.id,
|
||||
ip: storedIp,
|
||||
url: new URL(request.url).pathname,
|
||||
action: action,
|
||||
description,
|
||||
metadata: null,
|
||||
error,
|
||||
success: false,
|
||||
},
|
||||
await appendIpAddressLogCapability({
|
||||
ipAddressId: ipAddress.id,
|
||||
ip: storedIp,
|
||||
url: new URL(request.url).pathname,
|
||||
action,
|
||||
description,
|
||||
metadata: null,
|
||||
error,
|
||||
success: false,
|
||||
});
|
||||
throw Error(error);
|
||||
} else {
|
||||
await db.ipAddressLog.create({
|
||||
data: {
|
||||
ipAddressId: ipAddress.id || null,
|
||||
ip: storedIp,
|
||||
url: new URL(request.url).pathname,
|
||||
action,
|
||||
description,
|
||||
metadata: metadata ? JSON.stringify(metadata, null, 2) : null,
|
||||
error: null,
|
||||
success: true,
|
||||
},
|
||||
await appendIpAddressLogCapability({
|
||||
ipAddressId: ipAddress.id || null,
|
||||
ip: storedIp,
|
||||
url: new URL(request.url).pathname,
|
||||
action,
|
||||
description,
|
||||
metadata: metadata ? JSON.stringify(metadata, null, 2) : null,
|
||||
error: null,
|
||||
success: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function getOrCreateIpAddressLookup(ip: string): Promise<IpAddressDto | null> {
|
||||
// AD-22: Schlüssel und gespeicherter Wert ist der Pseudonym-Hash; die Land-Auflösung passiert
|
||||
// lokal (DB-IP Lite) über die Roh-IP, die unsere Infra nie verlässt.
|
||||
// AD-22: Use the pseudonymous hash as key and resolve the country locally with DB-IP Lite.
|
||||
const ipHash = hashIp(ip);
|
||||
const cacheKey = `ipAddress:${ipHash}`;
|
||||
const item = await cachified({
|
||||
key: cacheKey,
|
||||
disabled: isLocalRequestIp(ip),
|
||||
getFreshValue: () =>
|
||||
db.ipAddress.findUnique({
|
||||
where: { ip: ipHash },
|
||||
}),
|
||||
getFreshValue: () => findIpAddressCapability(ipHash),
|
||||
});
|
||||
if (item) {
|
||||
return toIpAddressDto(item);
|
||||
@@ -93,8 +85,7 @@ async function getOrCreateIpAddressLookup(ip: string): Promise<IpAddressDto | nu
|
||||
|
||||
const geo = await lookupCountry(ip);
|
||||
|
||||
const dto: IpAddressDto = {
|
||||
id: "",
|
||||
const createdIp = await upsertIpAddressCapability({
|
||||
ip: ipHash,
|
||||
provider: geo ? "local-db" : "",
|
||||
type: "",
|
||||
@@ -106,32 +97,9 @@ async function getOrCreateIpAddressLookup(ip: string): Promise<IpAddressDto | nu
|
||||
zipCode: "",
|
||||
latitude: null,
|
||||
longitude: null,
|
||||
metadata: null,
|
||||
};
|
||||
|
||||
const createdIp = await db.ipAddress
|
||||
.upsert({
|
||||
where: { ip: ipHash },
|
||||
update: {},
|
||||
create: {
|
||||
ip: ipHash,
|
||||
provider: dto.provider,
|
||||
type: dto.type,
|
||||
countryCode: dto.countryCode,
|
||||
countryName: dto.countryName,
|
||||
regionCode: dto.regionCode,
|
||||
regionName: dto.regionName,
|
||||
city: dto.city,
|
||||
zipCode: dto.zipCode,
|
||||
latitude: dto.latitude,
|
||||
longitude: dto.longitude,
|
||||
metadata: JSON.stringify(dto.metadata, null, 2),
|
||||
},
|
||||
})
|
||||
.then((created) => {
|
||||
clearCacheKey(cacheKey);
|
||||
return created;
|
||||
});
|
||||
metadata: JSON.stringify(null),
|
||||
});
|
||||
clearCacheKey(cacheKey);
|
||||
|
||||
return toIpAddressDto(createdIp);
|
||||
}
|
||||
@@ -199,9 +167,8 @@ function getIpAddressValidationError(ipAddress: IpAddressDto): IpAddressLogError
|
||||
}
|
||||
|
||||
/**
|
||||
* Threat-Flags-Form des früheren ipapi.is-Providers. Bleibt als Vertrag für `getIpAddressValidationError`
|
||||
* bestehen (greift nur, wenn ein Datensatz noch ipapi.is-Metadaten trägt); neue Lookups setzen keine
|
||||
* Metadaten mehr (AD-22, lokale Land-Auflösung).
|
||||
* Threat-flag shape from the former ipapi.is provider. It remains the contract for legacy records;
|
||||
* new local country lookups no longer persist provider metadata (AD-22).
|
||||
*/
|
||||
type IpApiIsDto = {
|
||||
ip: string;
|
||||
|
||||
Reference in new issue
Block a user