fix(security): use server-configured PET notification recipient
This commit is contained in:
@@ -436,8 +436,10 @@ const loadContactConfirmationEndpoint = ({ sendEmail } = {}) => {
|
||||
const withContactOrigins = async (fn) => {
|
||||
const previousNextAuthUrl = process.env.NEXTAUTH_URL;
|
||||
const previousCyApiRoot = process.env.CY_API_ROOT;
|
||||
const previousPetEmail = process.env.PETEMAIL;
|
||||
process.env.NEXTAUTH_URL = trustedContactOrigin;
|
||||
process.env.CY_API_ROOT = trustedWelshContactOrigin;
|
||||
process.env.PETEMAIL = "pet@example.com";
|
||||
|
||||
try {
|
||||
await fn();
|
||||
@@ -447,6 +449,9 @@ const withContactOrigins = async (fn) => {
|
||||
|
||||
if (previousCyApiRoot === undefined) delete process.env.CY_API_ROOT;
|
||||
else process.env.CY_API_ROOT = previousCyApiRoot;
|
||||
|
||||
if (previousPetEmail === undefined) delete process.env.PETEMAIL;
|
||||
else process.env.PETEMAIL = previousPetEmail;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -775,7 +780,6 @@ const validContactPetRequest = (overrides = {}) => ({
|
||||
"content-type": "application/json; charset=utf-8"
|
||||
},
|
||||
body: {
|
||||
petEmail: "pet@example.com",
|
||||
contactEmail: "applicant@example.com",
|
||||
contactSubject: contactSubjects.plq.en,
|
||||
contactbody: "Please help with my enquiry.",
|
||||
@@ -931,18 +935,83 @@ test("Contact Us PET notification enforces its exact field set", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
for (const petEmail of [
|
||||
test("Contact Us PET notification rejects obsolete caller-supplied recipient", async () => {
|
||||
await assertContactPetRejected({
|
||||
req: validContactPetRequest({
|
||||
body: {
|
||||
...validContactPetRequest().body,
|
||||
petEmail: "pet@example.com"
|
||||
}
|
||||
}),
|
||||
status: 400,
|
||||
code: "INVALID_REQUEST_FIELDS"
|
||||
});
|
||||
});
|
||||
|
||||
for (const configuredRecipient of [undefined, "invalid"]) {
|
||||
test(`Contact Us PET notification fails safely for configured recipient ${String(configuredRecipient)}`, async () => {
|
||||
await withContactOrigins(async () => {
|
||||
if (configuredRecipient === undefined) delete process.env.PETEMAIL;
|
||||
else process.env.PETEMAIL = configuredRecipient;
|
||||
|
||||
const loaded = loadContactPetEndpoint();
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(validContactPetRequest(), res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 500);
|
||||
assert.strictEqual(
|
||||
res.state.jsonBody.error.code,
|
||||
"PET_RECIPIENT_NOT_CONFIGURED"
|
||||
);
|
||||
assert.strictEqual(loaded.notifyCalls.length, 0);
|
||||
assert.strictEqual(
|
||||
JSON.stringify(res.state.jsonBody).includes(
|
||||
String(configuredRecipient)
|
||||
),
|
||||
false
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("Contact Us PET notification rejects caller recipient before configured recipient validation", async () => {
|
||||
await withContactOrigins(async () => {
|
||||
delete process.env.PETEMAIL;
|
||||
const loaded = loadContactPetEndpoint();
|
||||
const res = createRes();
|
||||
|
||||
await loaded.mod.default(
|
||||
validContactPetRequest({
|
||||
body: {
|
||||
...validContactPetRequest().body,
|
||||
petEmail: "attacker@example.com"
|
||||
}
|
||||
}),
|
||||
res
|
||||
);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(
|
||||
res.state.jsonBody.error.code,
|
||||
"INVALID_REQUEST_FIELDS"
|
||||
);
|
||||
assert.strictEqual(loaded.notifyCalls.length, 0);
|
||||
});
|
||||
});
|
||||
|
||||
for (const contactEmail of [
|
||||
undefined,
|
||||
"invalid",
|
||||
`${"a".repeat(190)}@example.com`
|
||||
]) {
|
||||
test(`Contact Us PET notification rejects recipient ${String(petEmail).slice(0, 30)}`, async () => {
|
||||
test(`Contact Us PET notification rejects contact email ${String(contactEmail).slice(0, 30)}`, async () => {
|
||||
await assertContactPetRejected({
|
||||
req: validContactPetRequest({
|
||||
body: { ...validContactPetRequest().body, petEmail }
|
||||
body: { ...validContactPetRequest().body, contactEmail }
|
||||
}),
|
||||
status: 400,
|
||||
code: "INVALID_PET_EMAIL"
|
||||
code: "INVALID_CONTACT_EMAIL"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user