Skip to content

Commit aa26a39

Browse files
Ben ThomasCopilot
authored andcommitted
fix(hub): address audit frontend review
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent b87988a commit aa26a39

7 files changed

Lines changed: 63 additions & 26 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, expect, it } from "vitest"
2+
3+
import { isAdministratorRoute } from "."
4+
5+
describe("isAdministratorRoute()", () => {
6+
it.each([
7+
"/settings/administrators",
8+
"/settings/administrators/11111111-1111-4111-8111-111111111111",
9+
"/settings/admins",
10+
"/settings/admins/11111111-1111-4111-8111-111111111111",
11+
"/settings/audit",
12+
"/settings/audit/",
13+
"/Settings/Audit",
14+
])("classifies %s as an administrator route", (pathname) => {
15+
expect(isAdministratorRoute(new Request(`https://hub.example.test${pathname}`))).toBe(true)
16+
})
17+
18+
it("does not classify ordinary settings routes as administrator routes", () => {
19+
expect(isAdministratorRoute(new Request("https://hub.example.test/settings/sharing"))).toBe(
20+
false,
21+
)
22+
})
23+
})

‎hub/client/application/hub/is-administrator-route/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export function isAdministratorRoute(request: Request) {
2-
const { pathname } = new URL(request.url)
2+
const pathname = new URL(request.url).pathname.toLowerCase().replace(/\/+$/, "")
33
return (
44
pathname === "/settings/administrators" ||
55
pathname.startsWith("/settings/administrators/") ||

‎hub/client/application/hub/routes/security-audit-flow.test.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ function authenticatedSession() {
102102
return http.get(`${window.location.origin}/api/auth/session`, () => HttpResponse.json(session()))
103103
}
104104

105+
function expectOnlyRealAuditRequests(requests: string[]) {
106+
expect(
107+
requests.every(
108+
(request) =>
109+
request === "GET /api/auth/session" || request.startsWith("GET /api/security-audits?"),
110+
),
111+
).toBe(true)
112+
}
113+
105114
describe("real security audit frontend integration", () => {
106115
it("loads only the canonical Hub audit APIs and preserves opaque pagination", async () => {
107116
const before = "current+/cursor=="
@@ -133,7 +142,7 @@ describe("real security audit frontend integration", () => {
133142
expect(requests.filter((request) => request.includes("/api/security-audits"))).toEqual([
134143
`GET /api/security-audits?limit=50&before=${encodeURIComponent(before)}`,
135144
])
136-
expect(requests.some((request) => request.includes("/api/repositories"))).toBe(false)
145+
expectOnlyRealAuditRequests(requests)
137146
expect(screen.getByRole("link", { name: "Older actions" })).toHaveAttribute(
138147
"href",
139148
`/settings/audit?scope=hub&before=${encodeURIComponent(nextCursor)}`,
@@ -158,7 +167,7 @@ describe("real security audit frontend integration", () => {
158167

159168
expect(await screen.findByText("405")).toBeVisible()
160169
expect(screen.getByText("The Hub security audit is read-only.")).toBeVisible()
161-
expect(requests.some((request) => request.includes("/api/repositories"))).toBe(false)
170+
expectOnlyRealAuditRequests(requests)
162171
})
163172

164173
it.each([
@@ -219,7 +228,7 @@ describe("real security audit frontend integration", () => {
219228
])
220229

221230
expect(await screen.findByText(message)).toBeVisible()
222-
expect(requests.some((request) => request.includes("/api/repositories"))).toBe(false)
231+
expectOnlyRealAuditRequests(requests)
223232
})
224233

225234
it("rereads the session after an audit 403 and reports lost administrator access", async () => {

‎hub/client/application/hub/security-audit-contracts/index.test.ts‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const record = {
99
outcome: "success",
1010
actor: { membershipId: "22222222-2222-4222-8222-222222222222" },
1111
context: {
12-
provider: "github",
12+
provider: "corp.auth.v2",
1313
revision: 3,
1414
providerKeys: ["github", "entra"],
1515
},
1616
}
1717

1818
describe("securityAuditResponseSchema", () => {
19-
it("accepts provider-neutral UUID records and a nullable opaque cursor", () => {
19+
it("accepts provider-neutral UUID records, a dotted provider, and a nullable opaque cursor", () => {
2020
expect(
2121
securityAuditResponseSchema.parse({
2222
records: [record],
@@ -32,7 +32,7 @@ describe("securityAuditResponseSchema", () => {
3232
["a nested object", { metadata: { provider: "github" } }],
3333
["a non-integer number", { revision: 1.5 }],
3434
["a mixed array", { providerKeys: ["github", 2] }],
35-
["a secret-bearing field", { accessToken: "github_pat_1234567890abcdef" }],
35+
["a secret-bearing field", { accessToken: "public-provider-label" }],
3636
["credential-shaped text", { reason: "Bearer private-credential-value" }],
3737
])("rejects context containing %s", (_label, context) => {
3838
expect(
@@ -43,20 +43,21 @@ describe("securityAuditResponseSchema", () => {
4343
).toBe(false)
4444
})
4545

46-
it("rejects malformed record identity, time, actor, outcome, and cursor values", () => {
47-
expect(
48-
securityAuditResponseSchema.safeParse({
49-
records: [
50-
{
51-
...record,
52-
id: "12",
53-
occurredAt: "yesterday",
54-
outcome: "unknown",
55-
actor: { membershipId: 7 },
56-
},
57-
],
58-
nextCursor: "",
59-
}).success,
60-
).toBe(false)
61-
})
46+
it.each([
47+
["record identity", { id: "12" }, null],
48+
["record time", { occurredAt: "yesterday" }, null],
49+
["actor membership identity", { actor: { membershipId: 7 } }, null],
50+
["outcome", { outcome: "unknown" }, null],
51+
["cursor", {}, ""],
52+
] as Array<[string, Record<string, unknown>, string | null]>)(
53+
"rejects a malformed %s independently",
54+
(_label, recordPatch, nextCursor) => {
55+
expect(
56+
securityAuditResponseSchema.safeParse({
57+
records: [{ ...record, ...recordPatch }],
58+
nextCursor,
59+
}).success,
60+
).toBe(false)
61+
},
62+
)
6263
})

‎hub/client/application/hub/security-audit-contracts/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const securityAuditContextSchema = z
2323
})
2424
}
2525
if (
26+
key !== "provider" &&
2627
typeof value === "string" &&
2728
(/^(?:basic|bearer)\s/i.test(value) ||
2829
/^(?:github_pat_|gh[pousr]_)/i.test(value) ||

‎hub/client/application/hub/view-models/settings-audit/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function settingsAudit(
8282
time: event.createdAt,
8383
action: event.operation,
8484
actor: event.actor,
85-
context: [{ label: "Fixture details", value: event.context }],
85+
context: [{ label: "Details", value: event.context }],
8686
outcome: "Succeeded",
8787
})),
8888
}

‎hub/client/pages/security-audit-page/stories.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ export const Repository: Story = {
175175
id: "11111111-1111-4111-8111-111111111111",
176176
time: "2026-10-05T19:00:00Z",
177177
action: "share_link.create",
178-
actor: "Fixture user 1001",
179-
context: [{ label: "Fixture details", value: "Local repository audit fixture" }],
178+
actor: "octocat",
179+
context: [
180+
{ label: "Prototype", value: "Welcome" },
181+
{ label: "Build", value: "3" },
182+
],
180183
outcome: "Succeeded",
181184
},
182185
],

0 commit comments

Comments
 (0)