-
Notifications
You must be signed in to change notification settings - Fork 0
feat(frontend): add banned-IPs admin UI #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bihius
wants to merge
2
commits into
main
Choose a base branch
from
feat/banned-ips-admin-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| import { Modal } from "@/components/shared/Modal"; | ||
| import { Alert } from "@/components/ui/alert"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { useAuth } from "@/hooks/use-auth"; | ||
| import { ApiError } from "@/lib/api-client"; | ||
|
|
||
| import { unbanIp } from "./api"; | ||
| import type { BannedIp } from "./types"; | ||
|
|
||
| type UnbanIpDialogProps = { | ||
| bannedIp: BannedIp; | ||
| onSuccess: () => void; | ||
| onClose: () => void; | ||
| }; | ||
|
|
||
| export function UnbanIpDialog({ bannedIp, onSuccess, onClose }: UnbanIpDialogProps) { | ||
| const { accessToken } = useAuth(); | ||
| const [submitting, setSubmitting] = useState(false); | ||
| const [serverError, setServerError] = useState<string | null>(null); | ||
|
|
||
| async function handleUnban() { | ||
| if (!accessToken) return; | ||
| setSubmitting(true); | ||
| setServerError(null); | ||
| try { | ||
| await unbanIp(accessToken, bannedIp.ip); | ||
| onSuccess(); | ||
| } catch (err) { | ||
| setServerError( | ||
| err instanceof ApiError ? err.detail : "An unexpected error occurred", | ||
| ); | ||
| setSubmitting(false); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Modal | ||
| title="Unban IP address" | ||
| onClose={onClose} | ||
| footer={ | ||
| <> | ||
| <Button type="button" onClick={onClose} variant="outline"> | ||
| Cancel | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| disabled={submitting} | ||
| onClick={() => void handleUnban()} | ||
| variant="destructive" | ||
| > | ||
| {submitting ? "Unbanning…" : "Unban"} | ||
| </Button> | ||
| </> | ||
| } | ||
| > | ||
| {serverError && ( | ||
| <Alert variant="destructive" aria-live="assertive"> | ||
| {serverError} | ||
| </Alert> | ||
| )} | ||
| <p className="text-sm text-foreground"> | ||
| Are you sure you want to unban{" "} | ||
| <span className="font-mono font-semibold text-foreground">{bannedIp.ip}</span>{" "} | ||
| across <span className="font-semibold text-foreground">all virtual hosts</span>? | ||
| This clears the address from every active ban table, not just{" "} | ||
| <span className="font-semibold text-foreground">{bannedIp.domain}</span>. | ||
| </p> | ||
| </Modal> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { listBannedIps, unbanIp } from "./api"; | ||
|
|
||
| function jsonResponse(body: unknown) { | ||
| return new Response(JSON.stringify(body), { | ||
| status: 200, | ||
| headers: { "content-type": "application/json" }, | ||
| }); | ||
| } | ||
|
|
||
| describe("banned-ips API", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it("fetches the banned-ips list", async () => { | ||
| vi.stubEnv("VITE_API_BASE_URL", ""); | ||
| const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce( | ||
| jsonResponse({ | ||
| items: [ | ||
| { | ||
| ip: "203.0.113.10", | ||
| vhost_id: 1, | ||
| domain: "app.example.com", | ||
| gpc0: 12, | ||
| ban_threshold: 10, | ||
| banned: true, | ||
| expires_in_seconds: 120, | ||
| }, | ||
| ], | ||
| total: 1, | ||
| }), | ||
| ); | ||
|
|
||
| const response = await listBannedIps("token"); | ||
|
|
||
| expect(response.items[0]?.ip).toBe("203.0.113.10"); | ||
| expect(fetchMock.mock.calls[0]?.[0]).toBe("/api/v1/security/banned-ips"); | ||
| }); | ||
|
|
||
| it("sends a DELETE request with the IP URI-encoded in the path", async () => { | ||
| vi.stubEnv("VITE_API_BASE_URL", ""); | ||
| const fetchMock = vi | ||
| .spyOn(globalThis, "fetch") | ||
| .mockResolvedValueOnce(jsonResponse({ ip: "203.0.113.10", cleared: 1 })); | ||
|
|
||
| const response = await unbanIp("token", "203.0.113.10"); | ||
|
|
||
| expect(response).toEqual({ ip: "203.0.113.10", cleared: 1 }); | ||
| expect(fetchMock.mock.calls[0]?.[0]).toBe("/api/v1/security/banned-ips/203.0.113.10"); | ||
| expect(fetchMock.mock.calls[0]?.[1]?.method).toBe("DELETE"); | ||
| }); | ||
|
|
||
| it("URI-encodes IPv6 literals in the delete path", async () => { | ||
| vi.stubEnv("VITE_API_BASE_URL", ""); | ||
| const fetchMock = vi | ||
| .spyOn(globalThis, "fetch") | ||
| .mockResolvedValueOnce(jsonResponse({ ip: "2001:db8::1", cleared: 1 })); | ||
|
|
||
| await unbanIp("token", "2001:db8::1"); | ||
|
|
||
| expect(fetchMock.mock.calls[0]?.[0]).toBe( | ||
| "/api/v1/security/banned-ips/2001%3Adb8%3A%3A1", | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { apiRequest } from "@/lib/api-client"; | ||
|
|
||
| import type { BannedIpListResponse, UnbanResponse } from "./types"; | ||
|
|
||
| export function listBannedIps(token: string, signal?: AbortSignal) { | ||
| return apiRequest<BannedIpListResponse>("/security/banned-ips", { token, signal }); | ||
| } | ||
|
|
||
| export function unbanIp(token: string, ip: string) { | ||
| return apiRequest<UnbanResponse>(`/security/banned-ips/${encodeURIComponent(ip)}`, { | ||
| method: "DELETE", | ||
| token, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export type BannedIp = { | ||
| ip: string; | ||
| vhost_id: number; | ||
| domain: string; | ||
| gpc0: number; | ||
| ban_threshold: number; | ||
| banned: boolean; | ||
| expires_in_seconds: number; | ||
| }; | ||
|
|
||
| export type BannedIpListResponse = { | ||
| items: BannedIp[]; | ||
| total: number; | ||
| }; | ||
|
|
||
| export type UnbanResponse = { | ||
| ip: string; | ||
| cleared: number; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Encoding coverage only asserts a simple IPv4 path segment.
unbanIpcorrectly usesencodeURIComponent, which matters for IPv6 (:→%3A), but there is no regression test for that case.Suggestion: Add one case with an IPv6 literal (e.g.
2001:db8::1) expecting the encoded path. Low priority.