Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions ui/apps/console/e2e/saml.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Locator, type Page, expect, test } from "@playwright/test";
import { SignedXml } from "xml-crypto";
import { createPublicKey, verify } from "node:crypto";
import {
adminUpdateUser,
createNamespace,
Expand All @@ -20,7 +20,6 @@ import {
signOut,
} from "./helpers";
import {
type SignOnRequest,
adminContext,
answerSignOn,
buildSamlUser,
Expand Down Expand Up @@ -108,15 +107,37 @@ async function publishIdpMetadata(entityId: string, bindings?: Binding[]) {
return `http://ui:8080/${file}`;
}

function isSignedBy(request: SignOnRequest, certificate: string) {
const signature = request.document.getElementsByTagNameNS(
"http://www.w3.org/2000/09/xmldsig#",
"Signature",
)[0];
if (!signature) throw new Error("expected a signed SAML request");
const verifier = new SignedXml({ publicCert: certificate });
verifier.loadSignature(signature);
return verifier.checkSignature(request.xml);
const rsaSha256 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";

function isSignedBy(url: URL, certificate: string) {
const encoded = new Map(
url.search
.slice(1)
.split("&")
.map((pair) => {
const at = pair.indexOf("=");
return [pair.slice(0, at), pair.slice(at + 1)] as const;
}),
);
const signature = required(encoded.get("Signature"), `a Signature in ${url}`);
const algorithm = decodeURIComponent(
required(encoded.get("SigAlg"), `a SigAlg in ${url}`),
);
if (algorithm !== rsaSha256) {
throw new Error(
`expected the request signed with RSA-SHA256, got ${algorithm}`,
);
}
const signed = ["SAMLRequest", "RelayState", "SigAlg"]
.filter((name) => encoded.has(name))
.map((name) => `${name}=${encoded.get(name)}`)
.join("&");
return verify(
"sha256",
Buffer.from(signed),
createPublicKey(certificate),
Buffer.from(decodeURIComponent(signature), "base64"),
);
}

const buildEntityId = () => `${identityProvider}/${buildShortId()}`;
Expand Down Expand Up @@ -256,7 +277,8 @@ test.describe("Admin configuration", () => {
const token = await signInWithSso(page);

expect(requests).toHaveLength(1);
expect(isSignedBy(requests[0], certificate)).toBe(true);
expect(endpointOf(requests[0].url)).toBe(signOnURLs.redirect);
expect(isSignedBy(requests[0].url, certificate)).toBe(true);
expect((await readSessionUser(token)).email).toBe(user.email);
});

Expand Down
2 changes: 0 additions & 2 deletions ui/apps/console/e2e/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function buildSamlUser(): SamlUser {
export type SignOnRequest = {
url: URL;
document: Element;
xml: string;
relayState: string;
};

Expand Down Expand Up @@ -200,7 +199,6 @@ function buildSignOnRequest(
return {
url,
document: new DOMParser().parseFromString(xml, "text/xml").documentElement,
xml,
relayState: relayState ?? "",
};
}
Expand Down
Loading