Crestapps core prompt security

From CrestApps/CrestApps.AgentSkills · View interactive version

Crestapps core prompt security is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the CrestApps.Core 2.0 prompt-security layer that hardens AI Profile chat. Covers IPromptSecurityService and weighted IPromptSecurityRule scoring, custom rules, PromptSecurityOptions guards and thresholds, the security preamble and input delimiters, IOutputSecurityFilter, audit logging, mu

How to use this skill

Skill file

CrestApps.Core Prompt Security

A local, deterministic, regex-based layer for AI Profile chat: input validation, a security preamble and input delimiters, output filtering, rate limiting and audit logging. It is defense in depth, never a replacement for tool authorization.

Guidelines

Configure Site Policy

using CrestApps.Core.AI.Security;

builder.Services.Configure<PromptSecurityOptions>(builder.Configuration.GetSection("CrestApps:AI:PromptSecurity"));

builder.Services.PostConfigure<PromptSecurityOptions>(options =>
{
    options.MaxPromptLength = 6000;
    options.BlockingThreshold = PromptRiskLevel.High;
    options.CustomBlockedPatterns.Add(@"\binternal-canary-123\b");
    options.AnonymousMessageRateLimitTiers =
    [
        new ChatRateLimitTier { Limit = 5, Window = TimeSpan.FromSeconds(30) },
        new ChatRateLimitTier { Limit = 30, Window = TimeSpan.FromMinutes(5) },
        new ChatRateLimitTier { Limit = 150, Window = TimeSpan.FromHours(1) },
    ];
});

| Option | Default |

|---|---|

| EnableInjectionDetection, EnableOutputFiltering, EnableSecurityPreamble, EnableInputDelimiters, EnableChatInteractionInputDelimiters, EnableAuditLogging | true |

| MaxPromptLength | 8000 |

| BlockingThreshold | High |

| LowRiskScoreThreshold / Medium... / High... / Critical... | 10 / 20 / 35 / 50 |

| AnonymousMessageRateLimitTiers | 5 per 30s, 30 per 5m, 150 per hour, 500 per day |

| AnonymousSessionStartRateLimitTiers | 5 per 30s, 10 per 5m, 150 per hour, 500 per day |

| MaxMessagesPerWindow / RateLimitWindow | 20 per 1 minute (authenticated callers; anonymous fallback when tiers are empty) |

| MaxAnonymousSessionsPerWindow / AnonymousSessionRateLimitWindow | 20 per 10 minutes (fallback when session-start tiers are empty) |

The single-window values are fallbacks: while a tier list is populated, changing only MaxAnonymousSessionsPerWindow has no effect. Clear the tiers or lower a tier instead. Tiers apply to anonymous traffic only; authenticated callers use MaxMessagesPerWindow and never hit session-start throttling.

In JSON a tier is { "Limit": 5, "Window": "00:00:30" }.

Rate-Limit Partitions

AIChatRateLimitingOptions decides which keys a throttle counts against. Each value is a ChatRateLimitPartition flags combination:

| Option | Default |

|---|---|

| AuthenticatedMessagePartitions | AuthenticatedUser and NetworkAddress |

| AnonymousMessagePartitions | Visitor, NetworkAddress, Session and Connection |

| AnonymousSessionStartPartitions | Visitor and NetworkAddress |

The network-address bucket is shared with anonymous throttling, so logging out does not reset a caller's per-IP allowance. Remove NetworkAddress from AuthenticatedMessagePartitions if authenticated users behind one NAT must not share a bucket.

builder.Services.AddCrestAppsCore(crestApps => crestApps
    .AddAISuite(ai => ai
        .AddOpenAI()
        .AddChatInteractions(chat => chat
            .ConfigureVisitorIdentity(options =>
            {
                options.RemoteAddressMode = AIVisitorRemoteAddressMode.Hashed;
                options.AllowCrossSiteEmbedding = true; // only for chats framed on other sites
            })
            .ConfigureChatRateLimiting(options =>
            {
                options.AuthenticatedMessagePartitions = ChatRateLimitPartition.AuthenticatedUser;
            }))));

AllowCrossSiteEmbedding writes the crestapps-ai-visitor cookie as SameSite=None; Secure; Partitioned (keep UsePartitionedCookie on). It requires HTTPS; behind a TLS-terminating proxy forward the scheme. The visitor cookie owns a visitor's session documents, so the upload and remove endpoints validate antiforgery for requests that carry it. An embedded chat therefore also needs the antiforgery cookie written the same way, and SuppressXFrameOptionsHeader, or the first load of the framed page is blocked by X-Frame-Options: SAMEORIGIN and uploads answer 400:

builder.Services.AddAntiforgery(options =>
{
    options.Cookie.SameSite = SameSiteMode.None;
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
    options.Cookie.Extensions.Add("Partitioned");
    options.SuppressXFrameOptionsHeader = true;
});

Remote Address Behind a Proxy

DefaultAIVisitorIdentityResolver uses the connection's remote address and never reads X-Forwarded-For, which any client can send. Behind a reverse proxy, let ASP.NET Core's forwarded headers middleware replace the address for trusted proxies only:

builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.KnownProxies.Add(IPAddress.Parse("10.0.0.4"));
});

app.UseForwardedHeaders();

Without it, every visitor shares the proxy's address. An IPv4-mapped address is keyed as IPv4, an IPv6 client is keyed on its /64 in every RemoteAddressMode (ChatRateLimitKeyResolver reduces a plain-text address to ip:{prefix}::/64; the stored address stays full), and a request with no address shares one bucket. A visitor cookie is reused only when it looks like an id (letters, digits, -, _, at most 64 characters). Replace IChatRateLimiter for a distributed store in multi-instance deployments. AIChatEndpointRateLimitingOptions lets you swap the ASP.NET Core endpoint policy for session-start routes.

Per-Profile Throttles

profile.WithSettings(new PromptSecurityProfileSettings
{
    MaxMessagesPerWindow = 10,
    RateLimitWindow = TimeSpan.FromMinutes(2),
    AnonymousMessageRateLimitTiers = null,  // inherit the site tiers
    AnonymousSessionStartRateLimitTiers = [], // opt out, use the single window
    MaxAnonymousSessionsPerWindow = 3,
});

Every field is nullable and null inherits the site value. 0 disables that throttle for the profile.

Add a Custom Rule

using System.Text.RegularExpressions;
using CrestApps.Core.AI.Security;

public sealed partial class CanaryPromptSecurityRule : IPromptSecurityRule
{
    public string RuleId => "canary-token";

    public ValueTask<PromptSecurityRuleResult> EvaluateAsync(
        PromptSecurityEvaluationContext context,
        CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrWhiteSpace(context.NormalizedInput) || !CanaryRegex().IsMatch(context.NormalizedInput))
        {
            return ValueTask.FromResult<PromptSecurityRuleResult>(null);
        }

        return ValueTask.FromResult(new PromptSecurityRuleResult
        {
            RuleId = RuleId,
            Categories = ["data-exfiltration"],
            Severity = PromptRiskLevel.Critical,
            Score = 50,
            Reason = "Detected the internal canary token.",
        });
    }

    [GeneratedRegex(@"\binternal-canary-123\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant, matchTimeoutMilliseconds: 100)]
    private static partial Regex CanaryRegex();
}
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPromptSecurityRule, CanaryPromptSecurityRule>());

Evaluate NormalizedInput (Unicode FormKC, invisible characters removed, whitespace collapsed) and fall back to FoldedInput (homoglyphs folded) only for lookalike attacks. Scores 8 to 12 contribute to an aggregate; 20 or more can block alone. Always set a regex timeout. Return null for no match.

Input and Output Enforcement

Guardrails and Redaction

Prompt security is pattern-based and global. For per-profile topic refusal add guardrails (AddGuardrails() on the AI suite, AIProfileGuardrailSettings on the profile; see crestapps-core-ai-profiles). With guardrails registered, AIChatHubCore runs, in order:

  1. Customer message: ValidatePromptSecurityAsync runs IPromptSecurityService first. If it does not block and CheckCustomer is on, AIGuardrailService.CheckAsync(..., AIGuardrailDirection.Customer, ...) runs. A refusal becomes a blocked PromptSecurityResult whose DetectionRule starts with AIGuardrailService.DetectionRulePrefix ("guardrail:") and the caller sees the profile's PlaceholderReply instead of the generic blocked message.
  2. Reply: the protected virtual ValidateOutputAsync runs IOutputSecurityFilter first, then the reply guardrail when CheckReplies is on. It returns the message to show instead of the reply, or null; a refused reply is not saved.

Guardrail checks are typed-chat only in the hub. Other channels call AIGuardrailService.CheckAsync themselves. When the check cannot run (no utility deployment, model error) the text passes with a warning, so guardrails stay a second line behind prompt security.

Prompt security never redacts stored conversations. To redact personal data, keep only metadata, or enforce retention, use conversation privacy (AddConversationPrivacy(), AIProfileDataPrivacySettings, AIConversationPrivacyService, AIPersonalDataRedactor; see crestapps-core-ai-profiles).

Uploads

Every chat session and chat interaction upload passes IUploadedFileScanner.ScanAsync(IFormFile, CancellationToken) before storage. The default NoOpUploadedFileScanner returns Clean; register your scanner before document processing so TryAdd keeps yours. FileScanResult.Infected(...) and FileScanResult.Error(...) reject the upload (fail closed).

Answers in the Browser

The chat scripts sanitize every answer (CoreAIImages.sanitize in chat-markers.js), because a model can be tricked by a document, crawled page or prompt injection into writing markup:

See crestapps-core-chat-interactions for the details and the allowedImageHosts option.

Tool Authorization

Choose the Right Extension Point

| Need | Use |

|---|---|

| Detect a prompt pattern | IPromptSecurityRule |

| Validate input directly | IPromptSecurityService |

| Output policy | IOutputSecurityFilter |

| Limits and thresholds | PromptSecurityOptions |

| Throttle keys | AIChatRateLimitingOptions |

| Distributed throttling | Replace IChatRateLimiter |

| Per-user tools in Chat Interactions | Replace IAIToolAccessEvaluator |

| Per-profile refused topics | AddGuardrails() and AIProfileGuardrailSettings |

| React to a refused topic | IAIGuardrailHandler |

| Change hub output checks | Override AIChatHubCore.ValidateOutputAsync |

| Redact or expire stored conversations | AddConversationPrivacy() |

Upgrading Within 2.0 Previews

Upgrading from 1.x

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.