Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+99
@@ -0,0 +1,99 @@
|
||||
import type { TFunction } from "i18next";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { createFeedback } from "../db/feedback.db.server";
|
||||
import NotificationService from "~/modules/notifications/services/.server/NotificationService";
|
||||
import { feedbackRedactionMarkers } from "./FeedbackRedaction.server";
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: {
|
||||
feedback: {
|
||||
count: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../db/feedback.db.server", () => ({
|
||||
createFeedback: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/modules/notifications/services/.server/NotificationService", () => ({
|
||||
default: {
|
||||
sendToRoles: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
const countFeedbackMock = vi.mocked(db.feedback.count);
|
||||
const createFeedbackMock = vi.mocked(createFeedback);
|
||||
const sendToRolesMock = vi.mocked(NotificationService.sendToRoles);
|
||||
const t = ((key: string) => key) as TFunction;
|
||||
|
||||
describe("FeedbackServer.send", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
countFeedbackMock.mockResolvedValue(0);
|
||||
createFeedbackMock.mockResolvedValue({ id: "feedback_1" } as Awaited<ReturnType<typeof createFeedback>>);
|
||||
});
|
||||
|
||||
it("redacts sensitive message and URL values before notification and persistence", async () => {
|
||||
const rawEmail = "owner@example.com";
|
||||
const rawBearer = "secret-token-value";
|
||||
const rawHexSecret = "abcdefabcdefabcdefabcdefabcdefabcdefabcd";
|
||||
const rawQueryToken = "raw-token-123";
|
||||
const rawClientSecret = "super-secret-value";
|
||||
const message = `Login failed for ${rawEmail} using Bearer ${rawBearer}; trace ${rawHexSecret}.`;
|
||||
const fromUrl = `https://app.example.com/feedback?token=${rawQueryToken}&next=/home&client_secret=${rawClientSecret}`;
|
||||
|
||||
const { default: FeedbackServer } = await import("./Feedback.server");
|
||||
|
||||
await FeedbackServer.send({
|
||||
t,
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
message,
|
||||
fromUrl,
|
||||
});
|
||||
|
||||
expect(sendToRolesMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
notification: expect.objectContaining({
|
||||
message: expect.stringContaining(feedbackRedactionMarkers.email),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(createFeedbackMock).toHaveBeenCalledWith({
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
message: expect.stringContaining(feedbackRedactionMarkers.secret),
|
||||
fromUrl: expect.stringContaining(`token=${feedbackRedactionMarkers.token}`),
|
||||
});
|
||||
|
||||
const notifiedPayload = JSON.stringify(sendToRolesMock.mock.calls[0]);
|
||||
const createdPayload = JSON.stringify(createFeedbackMock.mock.calls[0]);
|
||||
for (const rawSecret of [rawEmail, rawBearer, rawHexSecret, rawQueryToken, rawClientSecret]) {
|
||||
expect(notifiedPayload).not.toContain(rawSecret);
|
||||
expect(createdPayload).not.toContain(rawSecret);
|
||||
}
|
||||
expect(notifiedPayload).toContain(`Bearer ${feedbackRedactionMarkers.token}`);
|
||||
expect(createdPayload).toContain(`client_secret=${feedbackRedactionMarkers.token}`);
|
||||
expect(createdPayload).toContain("next=/home");
|
||||
});
|
||||
|
||||
it("rejects feedback when the redacted trimmed message is empty", async () => {
|
||||
const { default: FeedbackServer } = await import("./Feedback.server");
|
||||
|
||||
await expect(
|
||||
FeedbackServer.send({
|
||||
t,
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
message: " ",
|
||||
fromUrl: "/acme?token=raw-token-123",
|
||||
}),
|
||||
).rejects.toThrow("Message is required");
|
||||
|
||||
expect(sendToRolesMock).not.toHaveBeenCalled();
|
||||
expect(createFeedbackMock).not.toHaveBeenCalled();
|
||||
expect(countFeedbackMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
import { TFunction } from "i18next";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { createFeedback } from "../db/feedback.db.server";
|
||||
import NotificationService from "~/modules/notifications/services/.server/NotificationService";
|
||||
import { redactFeedbackText } from "./FeedbackRedaction.server";
|
||||
|
||||
const MAX_FEEDBACK_PER_DAY = 3;
|
||||
|
||||
async function send({
|
||||
t,
|
||||
tenantId,
|
||||
userId,
|
||||
message,
|
||||
fromUrl,
|
||||
}: {
|
||||
t: TFunction;
|
||||
tenantId: string | null;
|
||||
userId: string | null;
|
||||
message: string;
|
||||
fromUrl: string;
|
||||
}) {
|
||||
const redactedMessage = redactFeedbackText(message);
|
||||
const redactedFromUrl = redactFeedbackText(fromUrl);
|
||||
if (!redactedMessage) {
|
||||
throw Error("Message is required");
|
||||
}
|
||||
|
||||
if (tenantId) {
|
||||
const feedbackToday = await db.feedback.count({ where: { tenantId } });
|
||||
if (feedbackToday >= MAX_FEEDBACK_PER_DAY) {
|
||||
throw Error(t("feedback.limitReached"));
|
||||
}
|
||||
} else if (userId) {
|
||||
const feedbackToday = await db.feedback.count({ where: { userId } });
|
||||
if (feedbackToday >= MAX_FEEDBACK_PER_DAY) {
|
||||
throw Error(t("feedback.limitReached"));
|
||||
}
|
||||
}
|
||||
await NotificationService.sendToRoles({
|
||||
channel: "admin-users",
|
||||
tenantId: null,
|
||||
notification: {
|
||||
message: "[New Feedback] " + redactedMessage,
|
||||
},
|
||||
});
|
||||
return await createFeedback({ tenantId, userId, message: redactedMessage, fromUrl: redactedFromUrl });
|
||||
}
|
||||
|
||||
async function deleteMany({ ids }: { ids: string[] }) {
|
||||
return await db.feedback.deleteMany({
|
||||
where: {
|
||||
id: { in: ids },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
send,
|
||||
deleteMany,
|
||||
};
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { feedbackRedactionMarkers, redactFeedbackText } from "./FeedbackRedaction.server";
|
||||
|
||||
describe("redactFeedbackText", () => {
|
||||
test("trims and preserves benign feedback text", () => {
|
||||
expect(redactFeedbackText(" The export button is hard to find on mobile. ")).toBe("The export button is hard to find on mobile.");
|
||||
expect(redactFeedbackText(null)).toBe("");
|
||||
expect(redactFeedbackText(undefined)).toBe("");
|
||||
});
|
||||
|
||||
test("redacts email addresses", () => {
|
||||
const redacted = redactFeedbackText("Please contact alex.user+demo@example.com about this bug.");
|
||||
|
||||
expect(redacted).toContain(feedbackRedactionMarkers.email);
|
||||
expect(redacted).not.toContain("alex.user+demo@example.com");
|
||||
});
|
||||
|
||||
test("redacts bearer tokens and api-key style values", () => {
|
||||
const bearer = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
|
||||
const apiKey = "api_key=sk_live_1234567890abcdef";
|
||||
const token = "accessToken: app-token-1234567890";
|
||||
const redacted = redactFeedbackText(`${bearer} ${apiKey} ${token}`);
|
||||
|
||||
expect(redacted).toContain(`Bearer ${feedbackRedactionMarkers.token}`);
|
||||
expect(redacted).toContain(`api_key=${feedbackRedactionMarkers.token}`);
|
||||
expect(redacted).toContain(`accessToken: ${feedbackRedactionMarkers.token}`);
|
||||
expect(redacted).not.toContain("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9");
|
||||
expect(redacted).not.toContain("sk_live_1234567890abcdef");
|
||||
expect(redacted).not.toContain("app-token-1234567890");
|
||||
});
|
||||
|
||||
test("redacts sensitive query parameter values", () => {
|
||||
const url = "https://app.example.com/feedback?token=raw-token-123&next=/home&client_secret=super-secret-value";
|
||||
const redacted = redactFeedbackText(url);
|
||||
|
||||
expect(redacted).toContain(`token=${feedbackRedactionMarkers.token}`);
|
||||
expect(redacted).toContain(`client_secret=${feedbackRedactionMarkers.token}`);
|
||||
expect(redacted).toContain("next=/home");
|
||||
expect(redacted).not.toContain("raw-token-123");
|
||||
expect(redacted).not.toContain("super-secret-value");
|
||||
});
|
||||
|
||||
test("redacts long hex and base64-like secrets", () => {
|
||||
const hexSecret = "0123456789abcdef0123456789abcdef01234567";
|
||||
const base64Secret = "AbCdEfGhIjKlMnOpQrStUvWxYz1234567890+/==";
|
||||
const redacted = redactFeedbackText(`hex ${hexSecret} base64 ${base64Secret}`);
|
||||
|
||||
expect(redacted).toContain(feedbackRedactionMarkers.secret);
|
||||
expect(redacted).not.toContain(hexSecret);
|
||||
expect(redacted).not.toContain(base64Secret);
|
||||
});
|
||||
|
||||
test("redacts mixed sensitive feedback while preserving context", () => {
|
||||
const email = "owner@example.com";
|
||||
const token = "Bearer secret-token-value";
|
||||
const hexSecret = "abcdefabcdefabcdefabcdefabcdefabcdefabcd";
|
||||
const redacted = redactFeedbackText(`Login failed for ${email} using ${token}; trace ${hexSecret}.`);
|
||||
|
||||
expect(redacted).toBe(
|
||||
`Login failed for ${feedbackRedactionMarkers.email} using Bearer ${feedbackRedactionMarkers.token}; trace ${feedbackRedactionMarkers.secret}.`,
|
||||
);
|
||||
expect(redacted).not.toContain(email);
|
||||
expect(redacted).not.toContain("secret-token-value");
|
||||
expect(redacted).not.toContain(hexSecret);
|
||||
});
|
||||
});
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { feedbackRedactionMarkers, redactFeedbackText } from "./FeedbackRedaction";
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
const REDACTED_EMAIL = "[REDACTED_EMAIL]";
|
||||
const REDACTED_TOKEN = "[REDACTED_TOKEN]";
|
||||
const REDACTED_SECRET = "[REDACTED_SECRET]";
|
||||
|
||||
const EMAIL_PATTERN = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi;
|
||||
const BEARER_TOKEN_PATTERN = /\bBearer\s+[^\s,;"')]+/gi;
|
||||
const SECRET_QUERY_VALUE_PATTERN = /([?&](?:api[_-]?key|access[_-]?token|auth[_-]?token|token|secret|client[_-]?secret|key)=)([^&\s]+)/gi;
|
||||
const KEY_VALUE_TOKEN_PATTERN = /\b((?:api[_-]?key|access[_-]?token|auth[_-]?token|token|secret|client[_-]?secret)\s*[:=]\s*)([^&\s,;"')]+)/gi;
|
||||
const HEX_SECRET_PATTERN = /\b[a-f0-9]{32,}\b/gi;
|
||||
const BASE64_SECRET_PATTERN = /\b(?=[A-Za-z0-9+/=_-]{32,}\b)(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[A-Za-z0-9+/=_-]{32,}\b/g;
|
||||
|
||||
export function redactFeedbackText(value: string | null | undefined): string {
|
||||
return (value ?? "")
|
||||
.trim()
|
||||
.replace(EMAIL_PATTERN, REDACTED_EMAIL)
|
||||
.replace(BEARER_TOKEN_PATTERN, `Bearer ${REDACTED_TOKEN}`)
|
||||
.replace(SECRET_QUERY_VALUE_PATTERN, `$1${REDACTED_TOKEN}`)
|
||||
.replace(KEY_VALUE_TOKEN_PATTERN, `$1${REDACTED_TOKEN}`)
|
||||
.replace(HEX_SECRET_PATTERN, REDACTED_SECRET)
|
||||
.replace(BASE64_SECRET_PATTERN, REDACTED_SECRET);
|
||||
}
|
||||
|
||||
export const feedbackRedactionMarkers = {
|
||||
email: REDACTED_EMAIL,
|
||||
token: REDACTED_TOKEN,
|
||||
secret: REDACTED_SECRET,
|
||||
};
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const createSupportTicketMock = vi.fn();
|
||||
const sendToRolesMock = vi.fn();
|
||||
|
||||
const t = ((key: string, fallback?: string) => fallback ?? key) as never;
|
||||
|
||||
describe("SupportTicketsServer", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
vi.doMock("../db/supportTickets.db.server", () => ({ createSupportTicket: createSupportTicketMock }));
|
||||
vi.doMock("~/modules/notifications/services/.server/NotificationService", () => ({
|
||||
default: { sendToRoles: sendToRolesMock },
|
||||
}));
|
||||
createSupportTicketMock.mockImplementation(async (data) => ({ id: "ticket_1", ...data }));
|
||||
});
|
||||
|
||||
it("creates redacted inline tickets and notifies platform admins", async () => {
|
||||
const { default: SupportTicketsServer } = await import("./SupportTickets.server");
|
||||
|
||||
await expect(
|
||||
SupportTicketsServer.createInline({
|
||||
t,
|
||||
tenantId: "tenant_1",
|
||||
userId: "user_1",
|
||||
subject: "Need help with token=sk_live_secret",
|
||||
message: "My api_key=abc123 should not leak",
|
||||
fromUrl: "/acme/dashboard?token=abc",
|
||||
}),
|
||||
).resolves.toMatchObject({ id: "ticket_1", source: "inline", tenantId: "tenant_1", userId: "user_1" });
|
||||
|
||||
expect(createSupportTicketMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
source: "inline",
|
||||
subject: expect.not.stringContaining("sk_live_secret"),
|
||||
message: expect.not.stringContaining("abc123"),
|
||||
fromUrl: expect.not.stringContaining("token=abc"),
|
||||
}),
|
||||
);
|
||||
expect(sendToRolesMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
channel: "admin-users",
|
||||
tenantId: null,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("creates idempotency-addressable inbound email tickets", async () => {
|
||||
const { default: SupportTicketsServer } = await import("./SupportTickets.server");
|
||||
|
||||
await SupportTicketsServer.createFromInboundEmail({
|
||||
tenantId: "tenant_1",
|
||||
fromEmail: "customer@example.test",
|
||||
subject: "Inbound support",
|
||||
message: "Please help",
|
||||
inboundMessageId: "mail_1",
|
||||
});
|
||||
|
||||
expect(createSupportTicketMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
source: "email",
|
||||
inboundMessageId: "mail_1",
|
||||
fromEmail: "[REDACTED_EMAIL]",
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
import { TFunction } from "i18next";
|
||||
import NotificationService from "~/modules/notifications/services/.server/NotificationService";
|
||||
import { createSupportTicket } from "../db/supportTickets.db.server";
|
||||
import { redactFeedbackText } from "./FeedbackRedaction.server";
|
||||
|
||||
const MAX_SUBJECT_LENGTH = 160;
|
||||
|
||||
function normalizeSubject(subject: string, message: string) {
|
||||
const redactedSubject = redactFeedbackText(subject).trim();
|
||||
if (redactedSubject) {
|
||||
return redactedSubject.slice(0, MAX_SUBJECT_LENGTH);
|
||||
}
|
||||
return redactFeedbackText(message).trim().slice(0, MAX_SUBJECT_LENGTH) || "Support request";
|
||||
}
|
||||
|
||||
async function createInline({
|
||||
t,
|
||||
tenantId,
|
||||
userId,
|
||||
subject,
|
||||
message,
|
||||
fromUrl,
|
||||
}: {
|
||||
t: TFunction;
|
||||
tenantId: string | null;
|
||||
userId: string | null;
|
||||
subject: string;
|
||||
message: string;
|
||||
fromUrl: string;
|
||||
}) {
|
||||
const redactedMessage = redactFeedbackText(message);
|
||||
const redactedFromUrl = redactFeedbackText(fromUrl);
|
||||
if (!redactedMessage) {
|
||||
throw Error(t("feedback.messageRequired"));
|
||||
}
|
||||
const ticket = await createSupportTicket({
|
||||
tenantId,
|
||||
userId,
|
||||
source: "inline",
|
||||
subject: normalizeSubject(subject, redactedMessage),
|
||||
message: redactedMessage,
|
||||
fromUrl: redactedFromUrl,
|
||||
});
|
||||
await NotificationService.sendToRoles({
|
||||
channel: "admin-users",
|
||||
tenantId: null,
|
||||
notification: {
|
||||
message: `[New Support Ticket] ${ticket.subject}`,
|
||||
},
|
||||
});
|
||||
return ticket;
|
||||
}
|
||||
|
||||
async function createFromInboundEmail({
|
||||
tenantId,
|
||||
fromEmail,
|
||||
subject,
|
||||
message,
|
||||
inboundMessageId,
|
||||
}: {
|
||||
tenantId: string | null;
|
||||
fromEmail: string;
|
||||
subject: string;
|
||||
message: string;
|
||||
inboundMessageId: string;
|
||||
}) {
|
||||
const redactedMessage = redactFeedbackText(message);
|
||||
if (!redactedMessage) {
|
||||
throw Error("Message is required");
|
||||
}
|
||||
return await createSupportTicket({
|
||||
tenantId,
|
||||
userId: null,
|
||||
source: "email",
|
||||
subject: normalizeSubject(subject, redactedMessage),
|
||||
message: redactedMessage,
|
||||
fromEmail: redactFeedbackText(fromEmail),
|
||||
inboundMessageId,
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
createInline,
|
||||
createFromInboundEmail,
|
||||
};
|
||||
Reference in new issue
Block a user