Class SecurityException

All Implemented Interfaces:
Serializable

public class SecurityException extends TnsAIException
Base exception for security-related violations.

This exception is thrown when security policies are violated, such as prompt injection detection, content moderation failures, or unauthorized access attempts.

Security Violation Types

TypeDescriptionRetryable
PROMPT_INJECTIONPrompt injection detectedNo
CONTENT_VIOLATIONUnsafe content detectedNo
VALIDATION_FAILUREInput validation failedYes
RATE_LIMITEDRate limit exceededYes
UNAUTHORIZEDAction not authorizedNo
SANDBOX_VIOLATIONSandbox policy violatedNo
APPROVAL_REQUIREDHuman approval neededYes

Usage Example


 // Throw security exception
 if (detector.detect(prompt).isInjectionDetected()) {
     throw new SecurityException(
         ViolationType.PROMPT_INJECTION,
         "Potential prompt injection detected in user input"
     );
 }

 // Handle security exception
 try {
     agent.chat(userInput);
 } catch (SecurityException e) {
     if (e.getViolationType() == ViolationType.RATE_LIMITED) {
         // Wait and retry
         Thread.sleep(e.getSuggestedRetryDelayMs());
         agent.chat(userInput);
     } else {
         // Log and reject
         auditLog.logViolation(e);
         return "Request rejected due to security policy";
     }
 }
 
See Also:
  • Constructor Details

    • SecurityException

      public SecurityException(SecurityException.ViolationType violationType, String message)
      Creates a security exception with type and message.
      Parameters:
      violationType - type of security violation
      message - error message
    • SecurityException

      public SecurityException(SecurityException.ViolationType violationType, String message, List<String> details)
      Creates a security exception with details.
      Parameters:
      violationType - type of security violation
      message - error message
      details - additional details about the violation
    • SecurityException

      public SecurityException(SecurityException.ViolationType violationType, String message, String agentId, String actionName)
      Creates a security exception with context.
      Parameters:
      violationType - type of security violation
      message - error message
      agentId - ID of the agent involved
      actionName - name of the action attempted
    • SecurityException

      public SecurityException(SecurityException.ViolationType violationType, String message, Throwable cause)
      Creates a security exception with cause.
      Parameters:
      violationType - type of security violation
      message - error message
      cause - underlying cause
  • Method Details

    • getViolationType

      public SecurityException.ViolationType getViolationType()
      Gets the type of security violation.
      Returns:
      violation type
    • getDetails

      public List<String> getDetails()
      Gets additional details about the violation.
      Returns:
      list of details (never null)
    • getAgentId

      public String getAgentId()
      Gets the agent ID involved in the violation.
      Returns:
      agent ID or null if not available
    • getActionName

      public String getActionName()
      Gets the action name involved in the violation.
      Returns:
      action name or null if not available
    • unauthorizedCause

      public static Optional<SecurityException> unauthorizedCause(Throwable failure)
      Locate a terminal authorization denial through executor/timeout wrappers.
    • getSuggestedRetryDelayMs

      public long getSuggestedRetryDelayMs()
      Description copied from class: TnsAIException
      Gets suggested retry delay in milliseconds. Override in subclasses for specific retry strategies.
      Overrides:
      getSuggestedRetryDelayMs in class TnsAIException
    • toString

      public String toString()
      Overrides:
      toString in class Throwable