fix(security): harden Contact Us CRM task endpoint
This commit is contained in:
@@ -9,39 +9,210 @@ const WEBAPI_URL =
|
||||
process.env.RELAY_ROOT ||
|
||||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
||||
|
||||
const teamMap = {
|
||||
plq: "ec48450c-7e26-ec11-a97f-00224800e98c", //PET
|
||||
ipq: "7fb9e598-7d26-ec11-a97f-00224800e98c", // Infrastructure
|
||||
pcpq: "8ffabad9-7c26-ec11-a97f-00224800e98c", // Casework
|
||||
pi: "5554882f-4606-ec11-a97f-00224800d374", // Digital Support Team
|
||||
appadvice: "cc3d987e-7c26-ec11-a97f-00224800e98c",
|
||||
repadvice: "cc3d987e-7c26-ec11-a97f-00224800e98c",
|
||||
ceq: "4fddd7e8-7d26-ec11-a97f-00224800e98c", // Inspector Scheduling
|
||||
other: "8ffabad9-7c26-ec11-a97f-00224800e98c", // Casework
|
||||
postdec: "1357e457-7e26-ec11-a97f-00224800e98c", // SLT Team
|
||||
comp: "1357e457-7e26-ec11-a97f-00224800e98c" // SLT Team
|
||||
};
|
||||
|
||||
const allowedSubjects = {
|
||||
plq: [
|
||||
"Questions about Planning legislation or policy",
|
||||
"Planning legislation query",
|
||||
"Ymholiad deddfwriaeth gynllunio"
|
||||
],
|
||||
ipq: [
|
||||
"Questions or comments about applications for major infrastructure projects",
|
||||
"Infrastructure procedure query",
|
||||
"Ymholiad gweithdrefn seilwaith"
|
||||
],
|
||||
pcpq: [
|
||||
"Questions or comments about planning and environmental appeals",
|
||||
"Planning casework procedure query",
|
||||
"Ymholiad gweithdrefn gwaith achos cynllunio"
|
||||
],
|
||||
pi: ["Query about using the Casework Portal", "Portal Issues", "Materion Porth"],
|
||||
appadvice: [
|
||||
"Advice How to submit an appeal",
|
||||
"Appeal submission advice",
|
||||
"Cyngor cyflwyno apêl"
|
||||
],
|
||||
repadvice: [
|
||||
"Advice How to comment on an appeal or application",
|
||||
"Representation submission advice",
|
||||
"Cyngor cyflwyno sylwadau"
|
||||
],
|
||||
ceq: [
|
||||
"Queries about forthcoming Hearing or Inquiry event",
|
||||
"Casework Events Query",
|
||||
"Ymholiad Digwyddiadau Gwaith Achos"
|
||||
],
|
||||
other: ["Other enquiry for PEDW", "Any Other Enquiries", "Unrhyw Ymholiadau Eraill"],
|
||||
postdec: [
|
||||
"Issues or questions about a case after the decision is issued",
|
||||
"Post Decision",
|
||||
"Ar ôl Penderfyniad"
|
||||
],
|
||||
comp: [
|
||||
"General Complaints of Compliments",
|
||||
"Complaints/Complements",
|
||||
"Cwynion/Canmoliaethau"
|
||||
]
|
||||
};
|
||||
|
||||
const allowedFields = new Set([
|
||||
"contactEmail",
|
||||
"contactSubject",
|
||||
"contactbody",
|
||||
"typeOfContact"
|
||||
]);
|
||||
|
||||
const emailRegex =
|
||||
/^(?!.*(?:.+\.)?(?:burpcollaborator\.net|interactsh\.com|mailinator\.com|10minutemail\.com|yopmail\.com|trashmail\.com)$)(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}|(?:\[(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:.+)])$/;
|
||||
|
||||
const hasTrustedOrigin = (req) => {
|
||||
const requestOrigin = req?.headers?.origin;
|
||||
|
||||
if (typeof requestOrigin !== "string" || requestOrigin === "null") {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const requestUrl = new URL(requestOrigin);
|
||||
if (requestOrigin !== requestUrl.origin) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const trustedOrigins = [process.env.NEXTAUTH_URL, process.env.CY_API_ROOT]
|
||||
.filter(
|
||||
(configuredUrl) =>
|
||||
typeof configuredUrl === "string" && configuredUrl.length > 0
|
||||
)
|
||||
.map((configuredUrl) => new URL(configuredUrl).origin);
|
||||
|
||||
return trustedOrigins.includes(requestUrl.origin);
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const validationError = (res, code, message) =>
|
||||
respondError(res, { status: 400, code, message });
|
||||
|
||||
export default async function ApiProxy(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
res.setHeader("Allow", "POST");
|
||||
return respondError(res, {
|
||||
status: 405,
|
||||
code: "METHOD_NOT_ALLOWED",
|
||||
message: "Method not allowed"
|
||||
});
|
||||
}
|
||||
|
||||
const contentType = req.headers?.["content-type"];
|
||||
if (
|
||||
typeof contentType !== "string" ||
|
||||
contentType.split(";", 1)[0].trim().toLowerCase() !== "application/json"
|
||||
) {
|
||||
return respondError(res, {
|
||||
status: 415,
|
||||
code: "UNSUPPORTED_MEDIA_TYPE",
|
||||
message: "Content-Type must be application/json"
|
||||
});
|
||||
}
|
||||
|
||||
if (!hasTrustedOrigin(req)) {
|
||||
return respondError(res, {
|
||||
status: 403,
|
||||
code: "FORBIDDEN",
|
||||
message: "Request not permitted"
|
||||
});
|
||||
}
|
||||
|
||||
const createTaskBody = req.body;
|
||||
|
||||
if (
|
||||
createTaskBody === null ||
|
||||
typeof createTaskBody !== "object" ||
|
||||
Array.isArray(createTaskBody)
|
||||
) {
|
||||
return validationError(
|
||||
res,
|
||||
"INVALID_REQUEST_BODY",
|
||||
"Request body must be a JSON object"
|
||||
);
|
||||
}
|
||||
|
||||
if (Object.keys(createTaskBody).some((field) => !allowedFields.has(field))) {
|
||||
return validationError(
|
||||
res,
|
||||
"UNSUPPORTED_REQUEST_FIELD",
|
||||
"Request contains an unsupported field"
|
||||
);
|
||||
}
|
||||
|
||||
const contactEmail = sanitizeString(createTaskBody?.contactEmail);
|
||||
const contactSubject = sanitizeString(createTaskBody?.contactSubject);
|
||||
const contactBody = sanitizeString(createTaskBody?.contactbody);
|
||||
|
||||
if (!isNonEmptyString(contactEmail) || !isNonEmptyString(contactSubject)) {
|
||||
return respondError(res, {
|
||||
status: 400,
|
||||
code: "CONTACT_EMAIL_AND_SUBJECT_REQUIRED",
|
||||
message: "contactEmail and contactSubject are required"
|
||||
});
|
||||
if (
|
||||
typeof createTaskBody.contactEmail !== "string" ||
|
||||
!isNonEmptyString(contactEmail) ||
|
||||
contactEmail.length > 200 ||
|
||||
!emailRegex.test(contactEmail)
|
||||
) {
|
||||
return validationError(
|
||||
res,
|
||||
"INVALID_CONTACT_EMAIL",
|
||||
"contactEmail must be a valid email address of 200 characters or fewer"
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof createTaskBody.typeOfContact !== "string" ||
|
||||
!Object.prototype.hasOwnProperty.call(teamMap, createTaskBody.typeOfContact)
|
||||
) {
|
||||
return validationError(
|
||||
res,
|
||||
"INVALID_CONTACT_TYPE",
|
||||
"typeOfContact is not supported"
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof createTaskBody.contactbody !== "string" ||
|
||||
!isNonEmptyString(contactBody) ||
|
||||
contactBody.length > 800
|
||||
) {
|
||||
return validationError(
|
||||
res,
|
||||
"INVALID_CONTACT_BODY",
|
||||
"contactbody must contain between 1 and 800 characters"
|
||||
);
|
||||
}
|
||||
|
||||
const contactValue = createTaskBody.typeOfContact;
|
||||
if (
|
||||
typeof createTaskBody.contactSubject !== "string" ||
|
||||
!allowedSubjects[contactValue].includes(contactSubject)
|
||||
) {
|
||||
return validationError(
|
||||
res,
|
||||
"INVALID_CONTACT_SUBJECT",
|
||||
"contactSubject does not match typeOfContact"
|
||||
);
|
||||
}
|
||||
|
||||
const queryUrl = "tasks";
|
||||
const token = await getToken();
|
||||
|
||||
const contactValue = createTaskBody.typeOfContact;
|
||||
|
||||
const teamMap = {
|
||||
plq: "ec48450c-7e26-ec11-a97f-00224800e98c", //PET
|
||||
ipq: "7fb9e598-7d26-ec11-a97f-00224800e98c", // Infrastructure
|
||||
pcpq: "8ffabad9-7c26-ec11-a97f-00224800e98c", // Casework
|
||||
pi: "5554882f-4606-ec11-a97f-00224800d374", // Digital Support Team
|
||||
appadvice: "cc3d987e-7c26-ec11-a97f-00224800e98c",
|
||||
repadvice: "cc3d987e-7c26-ec11-a97f-00224800e98c",
|
||||
ceq: "4fddd7e8-7d26-ec11-a97f-00224800e98c", // Inspector Scheduling
|
||||
other: "8ffabad9-7c26-ec11-a97f-00224800e98c", // Casework
|
||||
postdec: "1357e457-7e26-ec11-a97f-00224800e98c", // SLT Team
|
||||
comp: "1357e457-7e26-ec11-a97f-00224800e98c" // SLT Team
|
||||
};
|
||||
|
||||
let teamId = teamMap[contactValue];
|
||||
|
||||
const payload = {
|
||||
|
||||
@@ -4866,56 +4866,389 @@ test("patchcase catch path returns CASE_PATCH_FAILED", async () => {
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "CASE_PATCH_FAILED");
|
||||
});
|
||||
|
||||
test("createcrmtask returns CONTACT_EMAIL_AND_SUBJECT_REQUIRED when required fields missing", async () => {
|
||||
const trustedContactOrigin = "https://portal.example.test";
|
||||
const trustedWelshContactOrigin = "https://porth.example.test";
|
||||
|
||||
const contactSubjects = {
|
||||
plq: {
|
||||
en: "Questions about Planning legislation or policy",
|
||||
cy: "Ymholiad deddfwriaeth gynllunio"
|
||||
},
|
||||
ipq: {
|
||||
en: "Questions or comments about applications for major infrastructure projects",
|
||||
cy: "Ymholiad gweithdrefn seilwaith"
|
||||
},
|
||||
pcpq: {
|
||||
en: "Questions or comments about planning and environmental appeals",
|
||||
cy: "Ymholiad gweithdrefn gwaith achos cynllunio"
|
||||
},
|
||||
pi: { en: "Query about using the Casework Portal", cy: "Materion Porth" },
|
||||
appadvice: { en: "Advice How to submit an appeal", cy: "Cyngor cyflwyno apêl" },
|
||||
repadvice: {
|
||||
en: "Advice How to comment on an appeal or application",
|
||||
cy: "Cyngor cyflwyno sylwadau"
|
||||
},
|
||||
ceq: {
|
||||
en: "Queries about forthcoming Hearing or Inquiry event",
|
||||
cy: "Ymholiad Digwyddiadau Gwaith Achos"
|
||||
},
|
||||
other: { en: "Other enquiry for PEDW", cy: "Unrhyw Ymholiadau Eraill" },
|
||||
postdec: {
|
||||
en: "Issues or questions about a case after the decision is issued",
|
||||
cy: "Ar ôl Penderfyniad"
|
||||
},
|
||||
comp: {
|
||||
en: "General Complaints of Compliments",
|
||||
cy: "Cwynion/Canmoliaethau"
|
||||
}
|
||||
};
|
||||
|
||||
const contactTeamIds = {
|
||||
plq: "ec48450c-7e26-ec11-a97f-00224800e98c",
|
||||
ipq: "7fb9e598-7d26-ec11-a97f-00224800e98c",
|
||||
pcpq: "8ffabad9-7c26-ec11-a97f-00224800e98c",
|
||||
pi: "5554882f-4606-ec11-a97f-00224800d374",
|
||||
appadvice: "cc3d987e-7c26-ec11-a97f-00224800e98c",
|
||||
repadvice: "cc3d987e-7c26-ec11-a97f-00224800e98c",
|
||||
ceq: "4fddd7e8-7d26-ec11-a97f-00224800e98c",
|
||||
other: "8ffabad9-7c26-ec11-a97f-00224800e98c",
|
||||
postdec: "1357e457-7e26-ec11-a97f-00224800e98c",
|
||||
comp: "1357e457-7e26-ec11-a97f-00224800e98c"
|
||||
};
|
||||
|
||||
const validContactTaskRequest = (overrides = {}) => ({
|
||||
method: "POST",
|
||||
headers: {
|
||||
origin: trustedContactOrigin,
|
||||
"content-type": "application/json; charset=utf-8"
|
||||
},
|
||||
body: {
|
||||
contactEmail: "user@example.com",
|
||||
contactSubject: contactSubjects.plq.en,
|
||||
contactbody: "Please help with my enquiry.",
|
||||
typeOfContact: "plq"
|
||||
},
|
||||
...overrides
|
||||
});
|
||||
|
||||
const loadContactTaskEndpoint = (overrides = {}) => {
|
||||
let tokenCalls = 0;
|
||||
const relayCalls = [];
|
||||
const axios =
|
||||
overrides.axios ||
|
||||
(async (config) => {
|
||||
relayCalls.push(config);
|
||||
return { data: { taskId: "crm-1" } };
|
||||
});
|
||||
|
||||
const mod = loadModule("pages/api/endpoint/createcrmtask_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
isNonEmptyString: () => false,
|
||||
sanitizeString: (v) => v,
|
||||
getToken: async () => ({ access_token: "token" }),
|
||||
hashAPIPath: () => "&hash=expected",
|
||||
axios: async () => ({ data: {} }),
|
||||
consoleLogger: () => {}
|
||||
isNonEmptyString: (value) =>
|
||||
typeof value === "string" && value.trim().length > 0,
|
||||
sanitizeString: (value) =>
|
||||
typeof value === "string" ? value.trim() : "",
|
||||
getToken: async () => {
|
||||
tokenCalls += 1;
|
||||
return { access_token: "token" };
|
||||
},
|
||||
hashAPIPath: () => "?hash=expected",
|
||||
axios,
|
||||
consoleLogger: () => {},
|
||||
process: {
|
||||
env: {
|
||||
NEXTAUTH_URL: trustedContactOrigin,
|
||||
CY_API_ROOT: trustedWelshContactOrigin,
|
||||
RELAY_ROOT: "https://relay.example/"
|
||||
}
|
||||
},
|
||||
...overrides
|
||||
});
|
||||
|
||||
const req = { body: {} };
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
return {
|
||||
mod,
|
||||
getTokenCalls: () => tokenCalls,
|
||||
getRelayCalls: () => relayCalls
|
||||
};
|
||||
};
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(
|
||||
res.state.jsonBody.error.code,
|
||||
"CONTACT_EMAIL_AND_SUBJECT_REQUIRED"
|
||||
);
|
||||
const assertContactTaskRejectedBeforeRelay = async ({ req, status, code }) => {
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, status);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, code);
|
||||
assert.strictEqual(loaded.getTokenCalls(), 0);
|
||||
assert.strictEqual(loaded.getRelayCalls().length, 0);
|
||||
};
|
||||
|
||||
test("createcrmtask accepts a valid anonymous public Contact Us POST", async () => {
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(validContactTaskRequest(), res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
assert.strictEqual(loaded.getTokenCalls(), 1);
|
||||
assert.strictEqual(loaded.getRelayCalls().length, 1);
|
||||
});
|
||||
|
||||
test("createcrmtask accepts a valid portal Contact Us payload", async () => {
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const req = validContactTaskRequest({
|
||||
headers: {
|
||||
origin: trustedWelshContactOrigin,
|
||||
"content-type": "application/json"
|
||||
},
|
||||
body: {
|
||||
contactEmail: "portal.user@example.com",
|
||||
contactSubject: contactSubjects.repadvice.cy,
|
||||
contactbody: "Mae angen cymorth arnaf.",
|
||||
typeOfContact: "repadvice"
|
||||
}
|
||||
});
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
assert.strictEqual(loaded.getTokenCalls(), 1);
|
||||
assert.strictEqual(loaded.getRelayCalls().length, 1);
|
||||
});
|
||||
|
||||
test("createcrmtask preserves all category-to-team assignments", async () => {
|
||||
for (const [typeOfContact, teamId] of Object.entries(contactTeamIds)) {
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const req = validContactTaskRequest({
|
||||
body: {
|
||||
contactEmail: "user@example.com",
|
||||
contactSubject: contactSubjects[typeOfContact].en,
|
||||
contactbody: "Please help with my enquiry.",
|
||||
typeOfContact
|
||||
}
|
||||
});
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
const payload = JSON.parse(loaded.getRelayCalls()[0].data);
|
||||
assert.strictEqual(payload["ownerid@odata.bind"], `/teams(${teamId})`);
|
||||
}
|
||||
});
|
||||
|
||||
for (const method of ["GET", "PUT", "DELETE"]) {
|
||||
test(`createcrmtask rejects ${method} before CRM processing`, async () => {
|
||||
const req = validContactTaskRequest({ method });
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 405);
|
||||
assert.strictEqual(res.state.headers.allow, "POST");
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "METHOD_NOT_ALLOWED");
|
||||
assert.strictEqual(loaded.getTokenCalls(), 0);
|
||||
assert.strictEqual(loaded.getRelayCalls().length, 0);
|
||||
});
|
||||
}
|
||||
|
||||
for (const contentType of [
|
||||
"application/x-www-form-urlencoded",
|
||||
"text/plain",
|
||||
"multipart/form-data; boundary=test"
|
||||
]) {
|
||||
test(`createcrmtask rejects ${contentType} before CRM processing`, async () => {
|
||||
const req = validContactTaskRequest({
|
||||
headers: { origin: trustedContactOrigin, "content-type": contentType }
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 415,
|
||||
code: "UNSUPPORTED_MEDIA_TYPE"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("createcrmtask rejects an untrusted Origin before CRM processing", async () => {
|
||||
const req = validContactTaskRequest({
|
||||
headers: {
|
||||
origin: "https://attacker.example",
|
||||
"content-type": "application/json"
|
||||
}
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({ req, status: 403, code: "FORBIDDEN" });
|
||||
});
|
||||
|
||||
test("createcrmtask accepts each configured trusted Origin", async () => {
|
||||
for (const origin of [trustedContactOrigin, trustedWelshContactOrigin]) {
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const req = validContactTaskRequest({
|
||||
headers: { origin, "content-type": "application/json" }
|
||||
});
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
assert.strictEqual(loaded.getTokenCalls(), 1);
|
||||
}
|
||||
});
|
||||
|
||||
test("createcrmtask conservatively rejects a missing Origin", async () => {
|
||||
const req = validContactTaskRequest({ headers: { "content-type": "application/json" } });
|
||||
await assertContactTaskRejectedBeforeRelay({ req, status: 403, code: "FORBIDDEN" });
|
||||
});
|
||||
|
||||
for (const body of [null, [], "not-an-object"]) {
|
||||
test(`createcrmtask rejects malformed body ${JSON.stringify(body)}`, async () => {
|
||||
const req = validContactTaskRequest({ body });
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 400,
|
||||
code: "INVALID_REQUEST_BODY"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
for (const contactEmail of [undefined, "invalid", `${"a".repeat(190)}@example.com`]) {
|
||||
test(`createcrmtask rejects invalid email ${String(contactEmail).slice(0, 30)}`, async () => {
|
||||
const req = validContactTaskRequest({
|
||||
body: {
|
||||
contactEmail,
|
||||
contactSubject: contactSubjects.plq.en,
|
||||
contactbody: "Details",
|
||||
typeOfContact: "plq"
|
||||
}
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 400,
|
||||
code: "INVALID_CONTACT_EMAIL"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
for (const typeOfContact of [undefined, "unsupported"]) {
|
||||
test(`createcrmtask rejects category ${String(typeOfContact)}`, async () => {
|
||||
const req = validContactTaskRequest({
|
||||
body: {
|
||||
contactEmail: "user@example.com",
|
||||
contactSubject: contactSubjects.plq.en,
|
||||
contactbody: "Details",
|
||||
typeOfContact
|
||||
}
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 400,
|
||||
code: "INVALID_CONTACT_TYPE"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
for (const contactbody of [undefined, " ", "a".repeat(801)]) {
|
||||
test(`createcrmtask rejects invalid message ${String(contactbody).slice(0, 20)}`, async () => {
|
||||
const req = validContactTaskRequest({
|
||||
body: {
|
||||
contactEmail: "user@example.com",
|
||||
contactSubject: contactSubjects.plq.en,
|
||||
contactbody,
|
||||
typeOfContact: "plq"
|
||||
}
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 400,
|
||||
code: "INVALID_CONTACT_BODY"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("createcrmtask rejects caller-supplied CRM ownership fields", async () => {
|
||||
for (const field of ["ownerid", "ownerid@odata.bind", "teamId"]) {
|
||||
const req = validContactTaskRequest({
|
||||
body: { ...validContactTaskRequest().body, [field]: "attacker-owner" }
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 400,
|
||||
code: "UNSUPPORTED_REQUEST_FIELD"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("createcrmtask accepts existing English and Welsh subjects for every category", async () => {
|
||||
for (const [typeOfContact, subjects] of Object.entries(contactSubjects)) {
|
||||
for (const contactSubject of [subjects.en, subjects.cy]) {
|
||||
const loaded = loadContactTaskEndpoint();
|
||||
const req = validContactTaskRequest({
|
||||
body: {
|
||||
contactEmail: "user@example.com",
|
||||
contactSubject,
|
||||
contactbody: "Details",
|
||||
typeOfContact
|
||||
}
|
||||
});
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
const payload = JSON.parse(loaded.getRelayCalls()[0].data);
|
||||
assert.strictEqual(payload.subject, `Contact Us Enquiry - ${contactSubject}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("createcrmtask rejects an arbitrary or mismatched subject before CRM processing", async () => {
|
||||
for (const contactSubject of ["Arbitrary subject", contactSubjects.ipq.en]) {
|
||||
const req = validContactTaskRequest({
|
||||
body: { ...validContactTaskRequest().body, contactSubject }
|
||||
});
|
||||
await assertContactTaskRejectedBeforeRelay({
|
||||
req,
|
||||
status: 400,
|
||||
code: "INVALID_CONTACT_SUBJECT"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("createcrmtask catch path returns CRM_TASK_CREATE_FAILED", async () => {
|
||||
let tokenCalls = 0;
|
||||
const mod = loadModule("pages/api/endpoint/createcrmtask_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
isNonEmptyString: () => true,
|
||||
sanitizeString: (v) => v,
|
||||
getToken: async () => ({ access_token: "token" }),
|
||||
hashAPIPath: () => "&hash=expected",
|
||||
isNonEmptyString: (value) =>
|
||||
typeof value === "string" && value.trim().length > 0,
|
||||
sanitizeString: (value) =>
|
||||
typeof value === "string" ? value.trim() : "",
|
||||
getToken: async () => {
|
||||
tokenCalls += 1;
|
||||
return { access_token: "token" };
|
||||
},
|
||||
hashAPIPath: () => "?hash=expected",
|
||||
axios: async () => {
|
||||
throw new Error("create failed");
|
||||
},
|
||||
consoleLogger: () => {}
|
||||
});
|
||||
|
||||
const req = {
|
||||
body: {
|
||||
contactEmail: "user@test.local",
|
||||
contactSubject: "Need help",
|
||||
contactbody: "Details",
|
||||
typeOfContact: "plq"
|
||||
consoleLogger: () => {},
|
||||
process: {
|
||||
env: {
|
||||
NEXTAUTH_URL: trustedContactOrigin,
|
||||
CY_API_ROOT: trustedWelshContactOrigin,
|
||||
RELAY_ROOT: "https://relay.example/"
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
|
||||
await mod.default(validContactTaskRequest(), res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "CRM_TASK_CREATE_FAILED");
|
||||
assert.strictEqual(tokenCalls, 1);
|
||||
});
|
||||
|
||||
test("gethash returns UNAUTHENTICATED when session missing", async () => {
|
||||
|
||||
Reference in New Issue
Block a user