Revert "security: enforce authenticated CRM contact ownership"
This reverts commit e13700ab65.
This commit is contained in:
@@ -323,157 +323,64 @@ test("getlogin passes strict relayPolicy overrides to relayGet", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
const createPortalLoginFixture = (overrides = {}) => {
|
||||
const calls = {
|
||||
session: [],
|
||||
hash: [],
|
||||
relay: []
|
||||
};
|
||||
const session = Object.prototype.hasOwnProperty.call(overrides, "session")
|
||||
? overrides.session
|
||||
: { user: { email: "user@test.local" } };
|
||||
|
||||
test("getportallogin returns HASH_REQUIRED when hash missing", async () => {
|
||||
const mod = loadModule("pages/api/endpoint/getportallogin_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
getServerSession: async (...args) => {
|
||||
calls.session.push(args);
|
||||
return session;
|
||||
},
|
||||
authOptions: (req, res) => ({ req, res }),
|
||||
relayGet: async (options) => {
|
||||
calls.relay.push(options);
|
||||
if (overrides.relayGet) return overrides.relayGet(options);
|
||||
return respondSuccessMock(options.res, { value: [] });
|
||||
},
|
||||
hashAPIPath: (input) => {
|
||||
calls.hash.push(input);
|
||||
if (overrides.hashAPIPath) return overrides.hashAPIPath(input);
|
||||
return "&hash=expected";
|
||||
},
|
||||
escapeODataString: (value) => value.trim().replace(/'/g, "''"),
|
||||
azureHeadersPaged: (accessToken) => ({ accessToken })
|
||||
});
|
||||
|
||||
return { mod, calls };
|
||||
};
|
||||
|
||||
const invokePortalLogin = async (
|
||||
fixture,
|
||||
query = { emailAddress: "user@test.local", hash: "expected" },
|
||||
method = "GET"
|
||||
) => {
|
||||
const req = { method, query };
|
||||
const res = createRes();
|
||||
await fixture.mod.default(req, res);
|
||||
|
||||
return { req, res };
|
||||
};
|
||||
|
||||
test("getportallogin rejects unsupported methods before authentication", async () => {
|
||||
const fixture = createPortalLoginFixture();
|
||||
const { res } = await invokePortalLogin(fixture, undefined, "POST");
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 405);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "METHOD_NOT_ALLOWED");
|
||||
assert.strictEqual(fixture.calls.session.length, 0);
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin rejects missing session", async () => {
|
||||
const fixture = createPortalLoginFixture({ session: null });
|
||||
const { req, res } = await invokePortalLogin(fixture);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 401);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "UNAUTHENTICATED");
|
||||
assert.strictEqual(fixture.calls.session[0][0], req);
|
||||
assert.strictEqual(fixture.calls.session[0][1], res);
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin rejects session without email", async () => {
|
||||
const fixture = createPortalLoginFixture({
|
||||
session: { user: { id: "application-user" } }
|
||||
});
|
||||
const { res } = await invokePortalLogin(fixture);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 401);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "UNAUTHENTICATED");
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin rejects malformed requested email", async () => {
|
||||
const fixture = createPortalLoginFixture();
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "not-an-email",
|
||||
hash: "expected"
|
||||
});
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "INVALID_EMAIL_ADDRESS");
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin rejects another user's email before CRM access", async () => {
|
||||
const fixture = createPortalLoginFixture();
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "other@test.local",
|
||||
hash: "expected"
|
||||
});
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 403);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "FORBIDDEN");
|
||||
assert.strictEqual(fixture.calls.hash.length, 0);
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin valid HMAC for another email does not bypass ownership", async () => {
|
||||
const fixture = createPortalLoginFixture({
|
||||
relayGet: async () => ({}),
|
||||
hashAPIPath: (input) =>
|
||||
input.includes("other%40test.local") ||
|
||||
input.includes("other@test.local")
|
||||
? "&hash=valid-other-user-hash"
|
||||
: "&hash=unexpected"
|
||||
});
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "other@test.local",
|
||||
hash: "valid-other-user-hash"
|
||||
input && input.startsWith("/api/endpoint/getportallogin_api")
|
||||
? "&hash=expected"
|
||||
: "&hash=relay",
|
||||
azureHeadersPaged: () => ({})
|
||||
});
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 403);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "FORBIDDEN");
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin returns HASH_REQUIRED when hash missing", async () => {
|
||||
const fixture = createPortalLoginFixture();
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "user@test.local"
|
||||
});
|
||||
const req = { query: { emailAddress: "user@test.local" } };
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "HASH_REQUIRED");
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin returns INVALID_HASH when hash mismatch", async () => {
|
||||
const fixture = createPortalLoginFixture();
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "user@test.local",
|
||||
hash: "wrong"
|
||||
const mod = loadModule("pages/api/endpoint/getportallogin_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
relayGet: async () => ({}),
|
||||
hashAPIPath: (input) =>
|
||||
input && input.startsWith("/api/endpoint/getportallogin_api")
|
||||
? "&hash=expected"
|
||||
: "&hash=relay",
|
||||
azureHeadersPaged: () => ({})
|
||||
});
|
||||
|
||||
const req = { query: { emailAddress: "user@test.local", hash: "wrong" } };
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(res.state.jsonBody.error.code, "INVALID_HASH");
|
||||
assert.strictEqual(fixture.calls.relay.length, 0);
|
||||
});
|
||||
|
||||
test("getportallogin catch path returns PORTAL_LOGIN_FETCH_FAILED", async () => {
|
||||
const fixture = createPortalLoginFixture({
|
||||
const mod = loadModule("pages/api/endpoint/getportallogin_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
relayGet: async ({ res, errorResponse }) =>
|
||||
respondErrorMock(res, errorResponse)
|
||||
respondErrorMock(res, errorResponse),
|
||||
hashAPIPath: (input) =>
|
||||
input && input.startsWith("/api/endpoint/getportallogin_api")
|
||||
? "&hash=expected"
|
||||
: "&hash=relay",
|
||||
azureHeadersPaged: () => ({})
|
||||
});
|
||||
const { res } = await invokePortalLogin(fixture);
|
||||
|
||||
const req = {
|
||||
query: { emailAddress: "user@test.local", hash: "expected" }
|
||||
};
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(
|
||||
@@ -484,13 +391,26 @@ test("getportallogin catch path returns PORTAL_LOGIN_FETCH_FAILED", async () =>
|
||||
|
||||
test("getportallogin passes strict relayPolicy overrides to relayGet", async () => {
|
||||
let capturedRelayPolicy = null;
|
||||
const fixture = createPortalLoginFixture({
|
||||
|
||||
const mod = loadModule("pages/api/endpoint/getportallogin_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
relayGet: async ({ relayPolicy, res }) => {
|
||||
capturedRelayPolicy = relayPolicy;
|
||||
return respondSuccessMock(res, { value: [] });
|
||||
}
|
||||
},
|
||||
hashAPIPath: (input) =>
|
||||
input && input.startsWith("/api/endpoint/getportallogin_api")
|
||||
? "&hash=expected"
|
||||
: "&hash=relay",
|
||||
azureHeadersPaged: () => ({})
|
||||
});
|
||||
const { res } = await invokePortalLogin(fixture);
|
||||
|
||||
const req = {
|
||||
query: { emailAddress: "user@test.local", hash: "expected" }
|
||||
};
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(capturedRelayPolicy)), {
|
||||
@@ -503,65 +423,30 @@ test("getportallogin passes strict relayPolicy overrides to relayGet", async ()
|
||||
});
|
||||
|
||||
test("getportallogin accepts encoded email hash variant", async () => {
|
||||
const fixture = createPortalLoginFixture({
|
||||
session: { user: { email: "user+test@local" } },
|
||||
const mod = loadModule("pages/api/endpoint/getportallogin_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
respondSuccess: respondSuccessMock,
|
||||
relayGet: async ({ res }) =>
|
||||
respondSuccessMock(res, { value: [{ contactid: "c1" }] }),
|
||||
hashAPIPath: (input) =>
|
||||
input && input.includes("emailAddress=user%2Btest%40local")
|
||||
? "&hash=expected"
|
||||
: "&hash=other"
|
||||
});
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "user+test@local",
|
||||
hash: "expected"
|
||||
: "&hash=other",
|
||||
azureHeadersPaged: () => ({})
|
||||
});
|
||||
|
||||
const req = {
|
||||
query: { emailAddress: "user+test@local", hash: "expected" }
|
||||
};
|
||||
const res = createRes();
|
||||
await mod.default(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), {
|
||||
value: [{ contactid: "c1" }]
|
||||
});
|
||||
});
|
||||
|
||||
test("getportallogin uses normalized session email and preserves CRM response", async () => {
|
||||
let capturedQueryUrl;
|
||||
const crmResponse = {
|
||||
value: [
|
||||
{
|
||||
pinswg_preferredlanguage: 846040000,
|
||||
emailaddress1: "user.o'neill@test.local",
|
||||
contactid: "contact-1",
|
||||
yomifullname: "O'Neill, User",
|
||||
firstname: "User",
|
||||
lastname: "O'Neill"
|
||||
}
|
||||
]
|
||||
};
|
||||
const fixture = createPortalLoginFixture({
|
||||
session: { user: { email: " User.O'Neill@Test.Local " } },
|
||||
relayGet: async ({ queryUrl, res }) => {
|
||||
capturedQueryUrl = queryUrl;
|
||||
return respondSuccessMock(res, crmResponse);
|
||||
}
|
||||
});
|
||||
const { res } = await invokePortalLogin(fixture, {
|
||||
emailAddress: "user.o'neill@test.local",
|
||||
hash: "expected"
|
||||
});
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 200);
|
||||
assert.deepStrictEqual(
|
||||
JSON.parse(JSON.stringify(res.state.jsonBody)),
|
||||
crmResponse
|
||||
);
|
||||
assert.match(capturedQueryUrl, /emailaddress1 eq 'user\.o''neill@test\.local'/);
|
||||
assert.match(capturedQueryUrl, /statuscode eq 1/);
|
||||
assert.match(
|
||||
capturedQueryUrl,
|
||||
/\$select=pinswg_preferredlanguage,emailaddress1,contactid,yomifullname,firstname,lastname$/
|
||||
);
|
||||
});
|
||||
|
||||
test("getnewappeals catch path returns ADMIN_NEW_APPEALS_FETCH_FAILED", async () => {
|
||||
const mod = loadModule("pages/api/admin/getnewappeals_api.js", {
|
||||
respondError: respondErrorMock,
|
||||
|
||||
Reference in New Issue
Block a user