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
15 changes: 15 additions & 0 deletions frontend/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jest.mock("./components/Chat/ChatWindow", () => {
attackResultId,
conversationId,
activeConversationId,
attackTarget,
onConversationCreated,
onSelectConversation,
labels,
Expand All @@ -129,6 +130,7 @@ jest.mock("./components/Chat/ChatWindow", () => {
attackResultId: string | null;
conversationId: string | null;
activeConversationId: string | null;
attackTarget?: { identifier_hash?: string | null } | null;
onConversationCreated: (attackResultId: string, conversationId: string) => void;
onSelectConversation: (convId: string) => void;
labels: Record<string, string>;
Expand All @@ -139,6 +141,7 @@ jest.mock("./components/Chat/ChatWindow", () => {
<span data-testid="conversation-id">{conversationId ?? "none"}</span>
<span data-testid="active-conversation-id">{activeConversationId ?? "none"}</span>
<span data-testid="has-target">{activeTarget ? "yes" : "no"}</span>
<span data-testid="attack-target-hash">{attackTarget?.identifier_hash ?? "none"}</span>
<span data-testid="labels-operator">{labels.operator ?? ""}</span>
<span data-testid="labels-json">{JSON.stringify(labels)}</span>
<button onClick={onNewAttack} data-testid="new-attack">
Expand Down Expand Up @@ -185,6 +188,7 @@ jest.mock("./components/Config/TargetConfig", () => {
onSetActiveTarget(makeTarget({
target_registry_name: "test_target",
target_type: "OpenAIChatTarget",
identifier_hash: "test-target-hash",
}))
}
data-testid="set-target"
Expand Down Expand Up @@ -386,6 +390,17 @@ describe("App", () => {
expect(screen.getByTestId("conversation-id")).toHaveTextContent("conv-123");
});

it("retains the active target identifier when creating an attack", () => {
renderApp();

fireEvent.click(screen.getByTestId("nav-config"));
fireEvent.click(screen.getByTestId("set-target"));
fireEvent.click(screen.getByTestId("nav-chat"));
fireEvent.click(screen.getByTestId("set-conversation"));

expect(screen.getByTestId("attack-target-hash")).toHaveTextContent("test-target-hash");
});

it("clears conversationId on new attack", () => {
renderApp();

Expand Down
8 changes: 7 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import { DEFAULT_GLOBAL_LABELS } from './components/Labels/labelDefaults'
import { filtersFromSearchParams, filtersToSearchParams } from './components/History/historyFilters'
import type { ViewName } from './components/Sidebar/Navigation'
import type { TargetInstance, TargetInfo } from './types'
import { targetEndpoint, targetModelName, targetType } from './utils/targetIdentity'
import {
targetEndpoint,
targetIdentifierHash,
targetModelName,
targetType,
} from './utils/targetIdentity'
import { attacksApi, versionApi } from './services/api'
import { toApiError } from './services/errors'
import { useTour } from './hooks/useTour'
Expand Down Expand Up @@ -284,6 +289,7 @@ function App() {
target_type: targetType(activeTarget),
endpoint: targetEndpoint(activeTarget),
model_name: targetModelName(activeTarget),
identifier_hash: targetIdentifierHash(activeTarget),
}
: null
skipNextLoadForAttackId.current = arId
Expand Down
72 changes: 72 additions & 0 deletions frontend/src/components/Chat/ChatWindow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ describe("ChatWindow Integration", () => {
target_type: "AzureOpenAIChatTarget",
endpoint: "https://azure.openai.com",
model_name: "gpt-4o",
identifier_hash: "different-target-hash",
};

render(
Expand All @@ -1667,6 +1668,7 @@ describe("ChatWindow Integration", () => {
target_type: mockTarget.identifier.class_name,
endpoint: mockTarget.identifier.endpoint,
model_name: mockTarget.identifier.model_name,
identifier_hash: mockTarget.identifier.hash,
};

render(
Expand All @@ -1683,6 +1685,76 @@ describe("ChatWindow Integration", () => {
expect(screen.queryByTestId("cross-target-banner")).not.toBeInTheDocument();
});

it("should keep a historical Round Robin attack writable when the identifier hash matches", () => {
const roundRobinTarget = makeTarget({
target_registry_name: "round-robin",
target_type: "RoundRobinTarget",
endpoint: null,
model_name: null,
identifier_hash: "round-robin-hash",
inner_targets: [
{ target_registry_name: "inner-a", model_name: "e2e-dummy-model" },
{ target_registry_name: "inner-b", model_name: "e2e-dummy-model" },
],
});
const historicalTarget: TargetInfo = {
target_type: "RoundRobinTarget",
endpoint: null,
model_name: null,
identifier_hash: "round-robin-hash",
};

render(
<TestWrapper>
<ChatWindow
{...defaultProps}
activeTarget={roundRobinTarget}
attackResultId="ar-round-robin"
conversationId="conv-round-robin"
attackTarget={historicalTarget}
/>
</TestWrapper>
);

expect(screen.queryByTestId("cross-target-banner")).not.toBeInTheDocument();
expect(screen.getByTestId("chat-input")).toBeEnabled();
});

it("should lock a historical Round Robin attack when the composite identifier hash differs", () => {
const roundRobinTarget = makeTarget({
target_registry_name: "round-robin",
target_type: "RoundRobinTarget",
endpoint: null,
model_name: null,
identifier_hash: "active-round-robin-hash",
inner_targets: [
{ target_registry_name: "inner-a", model_name: "e2e-dummy-model" },
{ target_registry_name: "inner-b", model_name: "e2e-dummy-model" },
],
});
const historicalTarget: TargetInfo = {
target_type: "RoundRobinTarget",
endpoint: null,
model_name: null,
identifier_hash: "different-round-robin-hash",
};

render(
<TestWrapper>
<ChatWindow
{...defaultProps}
activeTarget={roundRobinTarget}
attackResultId="ar-round-robin"
conversationId="conv-round-robin"
attackTarget={historicalTarget}
/>
</TestWrapper>
);

expect(screen.getByTestId("cross-target-banner")).toBeInTheDocument();
expect(screen.queryByTestId("chat-input")).not.toBeInTheDocument();
});

it("should auto-open conversation panel when relatedConversationCount > 0", async () => {
mockedAttacksApi.getRelatedConversations.mockResolvedValue({
conversations: [
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/components/Chat/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { attacksApi } from '../../services/api'
import { toApiError } from '../../services/errors'
import { buildMessagePieces, backendMessagesToFrontend } from '../../utils/messageMapper'
import type { Message, MessageAttachment, TargetInstance, TargetInfo } from '../../types'
import { targetEndpoint, targetModelName, targetType } from '../../utils/targetIdentity'
import { targetInfoMatchesTarget } from '../../utils/targetIdentity'
import type { ViewName } from '../Sidebar/Navigation'
import { useChatWindowStyles } from './ChatWindow.styles'

Expand Down Expand Up @@ -543,11 +543,10 @@ export default function ChatWindow({
// from the currently configured target, prevent sending new messages.
// The user can "Continue with your target" to branch into a new attack with their target.
const isCrossTargetLocked = Boolean(
attackResultId && attackTarget && activeTarget && (
attackTarget.target_type !== targetType(activeTarget) ||
(attackTarget.endpoint ?? '') !== (targetEndpoint(activeTarget) ?? '') ||
(attackTarget.model_name ?? '') !== (targetModelName(activeTarget) ?? '')
)
attackResultId &&
attackTarget &&
activeTarget &&
!targetInfoMatchesTarget(attackTarget, activeTarget)
)

// "Continue with your target" — clone the current conversation into a new attack
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export interface TargetInfo {
target_type: string
endpoint?: string | null
model_name?: string | null
identifier_hash: string
}

export interface AttackSummary {
Expand Down
56 changes: 56 additions & 0 deletions frontend/src/utils/targetIdentity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { makeTarget } from '../test-utils/targetFixtures'
import {
targetEndpoint,
targetIdentifierHash,
targetInfoMatchesTarget,
targetModelName,
targetType,
targetUnderlyingModelName,
Expand Down Expand Up @@ -34,6 +35,61 @@ describe('targetIdentity', () => {
})
})

describe('targetInfoMatchesTarget', () => {
it('matches a historical Round Robin target by its identifier hash', () => {
const target = makeTarget({
target_registry_name: 'round_robin_1',
target_type: 'RoundRobinTarget',
endpoint: null,
model_name: null,
identifier_hash: 'round-robin-hash',
inner_targets: [
{ target_registry_name: 'inner_a', model_name: 'e2e-dummy-model' },
{ target_registry_name: 'inner_b', model_name: 'e2e-dummy-model' },
],
})

expect(targetModelName(target)).toBe('e2e-dummy-model')
expect(
targetInfoMatchesTarget(
{
target_type: 'RoundRobinTarget',
endpoint: null,
model_name: null,
identifier_hash: 'round-robin-hash',
},
target,
),
).toBe(true)
})

it('rejects a different composite with the same root projection', () => {
const target = makeTarget({
target_registry_name: 'round_robin_1',
target_type: 'RoundRobinTarget',
endpoint: null,
model_name: null,
identifier_hash: 'active-round-robin-hash',
inner_targets: [
{ target_registry_name: 'inner_a', model_name: 'e2e-dummy-model' },
{ target_registry_name: 'inner_b', model_name: 'e2e-dummy-model' },
],
})

expect(
targetInfoMatchesTarget(
{
target_type: 'RoundRobinTarget',
endpoint: null,
model_name: null,
identifier_hash: 'different-round-robin-hash',
},
target,
),
).toBe(false)
})
})

describe('targetModelName / targetUnderlyingModelName', () => {
it('returns the target own model name when set', () => {
const target = makeTarget({
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/utils/targetIdentity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TargetInstance } from '../types'
import type { TargetInfo, TargetInstance } from '../types'

/**
* Helpers for reading a target's identity off its embedded `identifier`.
Expand Down Expand Up @@ -46,6 +46,11 @@ export function targetEndpoint(target: TargetInstance): string | null {
}

/** The ComponentIdentifier content hash used for duplicate detection. */
export function targetIdentifierHash(target: TargetInstance): string | null {
return target.identifier.hash ?? null
export function targetIdentifierHash(target: TargetInstance): string {
return target.identifier.hash
}

/** Whether persisted attack target information identifies the active target. */
export function targetInfoMatchesTarget(targetInfo: TargetInfo, target: TargetInstance): boolean {
return targetInfo.identifier_hash === targetIdentifierHash(target)
}
2 changes: 2 additions & 0 deletions pyrit/backend/models/attacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TargetInfo(BaseModel):
target_type: str = Field(..., description="Target class name (e.g., 'OpenAIChatTarget')")
endpoint: str | None = Field(None, description="Target endpoint URL")
model_name: str | None = Field(None, description="Model or deployment name")
identifier_hash: str = Field(..., description="Canonical target identifier hash")
Comment thread
romanlutz marked this conversation as resolved.


class ScoreView(Score):
Expand Down Expand Up @@ -250,6 +251,7 @@ def target(self) -> TargetInfo | None:
target_type=target_id.class_name,
endpoint=cast("str | None", target_id.params.get("endpoint") or None),
model_name=cast("str | None", target_id.params.get("model_name") or None),
identifier_hash=target_id.hash,
)

@computed_field # type: ignore[prop-decorator]
Expand Down
12 changes: 3 additions & 9 deletions pyrit/backend/services/attack_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,16 +698,10 @@ def _validate_target_match(
return

request_target_id = request_target_obj.get_identifier()
if (
stored_target_id.class_name != request_target_id.class_name
or (stored_target_id.params.get("endpoint") or "") != (request_target_id.params.get("endpoint") or "")
or (stored_target_id.params.get("model_name") or "") != (request_target_id.params.get("model_name") or "")
):
if stored_target_id.hash != request_target_id.hash:
raise ValueError(
f"Target mismatch: attack was created with "
f"{stored_target_id.class_name}/{stored_target_id.params.get('model_name')} "
f"but request uses "
f"{request_target_id.class_name}/{request_target_id.params.get('model_name')}. "
f"Target mismatch: attack was created with {stored_target_id.unique_name} "
f"but request uses {request_target_id.unique_name}. "
f"Create a new attack to use a different target."
)

Expand Down
Loading