Whatever message this page gives is out now! Go check it out!
public struct function validate(required string message)
{
result: "success" | "successWith" | "failure" | "fatal",
message: "optional human-readable explanation",
repromptMessage: "optional modified message, used with successWith"
}
// ./guardrails/PromptInjectionGuardrail.cfc
component {
public
struct function validate(required string userMessage) {
var result = {result : "success", message : ""};
var lowerMessage = lcase(arguments.userMessage);
var patterns = [
"ignore previous instructions",
"ignore all previous",
"disregard all previous",
"forget previous instructions",
"new instructions:",
"system:",
"assistant:",
"you are now",
"act as if",
"pretend you are",
"roleplay as"
];
for (var pattern in patterns) {
if (findNoCase(pattern, lowerMessage)) {
result.result = "failure";
result.message = "Prompt injection detected: '" & pattern & "'";
return result;
}
}
if (len(arguments.userMessage) > 10000) {
result.result = "fatal";
result.message = "Input exceeds maximum allowed length";
return result;
}
return result;
}
}
component {
/**
* Input Guardrail: Credit Card Number Detection
* Detects credit card numbers in user input to prevent PII leakage to the
* LLM. Supports Visa, MasterCard, Amex, Discover, Diners Club, JCB formats.
* Returns successWith (redacted) or failure depending on configuration.
*/
public
struct function validate(required string userMessage) {
var result = {result : "success", message : "", repromptMessage : ""};
var msg = arguments.userMessage;
// ---- Credit-card patterns (with optional spaces / dashes) ----
var ccPatterns = [
// Visa: starts with 4, 13 or 16 digits
"4\d{3}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{1,4}",
// MasterCard: starts with 51-55 or 2221-2720
"(5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)[\s-]?\d{4}["
"\s-]?\d{4}[\s-]?\d{4}",
// Amex: starts with 34 or 37, 15 digits
"3[47]\d{2}[\s-]?\d{6}[\s-]?\d{5}",
// Discover: starts with 6011, 65, or 644-649
"(6011|65\d{2}|64[4-9]\d)[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}",
// Plain 16-digit sequence (catch-all)
"\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b"
];
for (var pattern in ccPatterns) {
var matcher = createObject("java", "java.util.regex.Pattern")
.compile(pattern)
.matcher(msg);
if (matcher.find()) {
// Redact the card number and return successWith
var sanitized = matcher.replaceAll("[CREDIT CARD REDACTED]");
result.result = "successWith";
result.message = "Credit card number detected in input and redacted.";
result.repromptMessage = sanitized;
return result;
}
}
return result;
}
}
// ./guardrails/HarmfulContentGuardrail.cfc
component {
public
struct function validate(required string aiMessage) {
var result = {result : "success", message : ""};
var lowerMessage = lcase(arguments.aiMessage);
var harmfulPatterns = [
"kill yourself", "commit suicide", "hurt yourself",
"illegal drugs", "how to hack", "create a bomb", "violent acts"
];
for (var pattern in harmfulPatterns) {
if (findNoCase(pattern, lowerMessage)) {
result.result = "failure";
result.message =
"AI response contains harmful content: '" & pattern & "'.";
return result;
}
}
var hateSpeechPatterns = [ "racial slur", "discriminatory language" ];
for (var pattern in hateSpeechPatterns) {
if (findNoCase(pattern, lowerMessage)) {
result.result = "fatal";
result.message = "AI response contains hate speech.";
return result;
}
}
return result;
}
}
chatModel = ChatModel({
PROVIDER : "openAi",
APIKEY : apiKey,
MODELNAME : "gpt-4o-mini"
});
aiService = AiService({
CHATMODEL: chatModel,
INPUTGUARDRAILS: [
expandPath("./guardrails/PromptInjectionGuardrail.cfc"),
expandPath("./guardrails/ContentFilterGuardrail.cfc")
],
OUTPUTGUARDRAILS: [
expandPath("./guardrails/HarmfulContentGuardrail.cfc")
]
});
response = aiService.chat("What is the best way to learn programming?"); The AI service automatically enforces all configured guardrails.
chatModel = ChatModel({
PROVIDER: "openAi",
APIKEY: apiKey,
MODELNAME: "gpt-4o-mini"
});
aiService = AiService({
CHATMODEL: chatModel,
INPUTGUARDRAILS: [
expandPath("./guardrails/LanguageGuardrail.cfc"),
expandPath("./guardrails/PromptInjectionGuardrail.cfc")
],
OUTPUTGUARDRAILS: [
expandPath("./guardrails/HarmfulContentGuardrail.cfc"),
expandPath("./guardrails/SystemLeakGuardrail.cfc")
]
});
// Controller layer
public string function askSupport(required string question) {
try {
return aiService.chat(question);
} catch (any e) {
// Optionally log e.message and return a user-friendly fallback
return "I’m unable to answer that question. Please rephrase or contact support.";
}
}
// ./guardrails/ContentFilterGuardrail.cfc
component {
public
struct function validate(required string userMessage) {
var result = {
result : "success",
message : "",
repromptMessage : ""
};
if (len(trim(arguments.userMessage)) == 0) {
result.result = "failure";
result.message = "Input cannot be empty.";
return result;
}
var bannedWords = [ "competitorX", "internalCodeName" ];
var sanitizedMessage = arguments.userMessage;
var containsProhibited = false;
for (var word in bannedWords) {
if (findNoCase(word, sanitizedMessage)) {
containsProhibited = true;
sanitizedMessage = replaceNoCase(
sanitizedMessage,
word,
repeatString("*", len(word)),
"all"
);
}
}
if (containsProhibited) {
result.result = "successWith";
result.message = "Input contained prohibited content and was sanitized.";
result.repromptMessage = sanitizedMessage;
}
return result;
}
}
// ./guardrails/ExternalModerationGuardrail.cfc
component {
public
struct function validate(required string userMessage) {
var result = {result : "success", message : ""};
cfhttp(
url = "https://moderation.example.com/api/v1/check",
method = "post",
result = "httpRes"
) {
cfhttpparam(type = "header", name = "Content-Type",
value = "application/json");
cfhttpparam(type = "body",
value = serializeJson({text = arguments.userMessage}));
}
if (httpRes.statusCode != "200") {
// Fail safely on moderation service error
result.result = "fatal";
result.message = "Unable to validate content at this time.";
return result;
}
var body = deserializeJson(httpRes.fileContent);
if (body.block == true) {
result.result = "failure";
result.message = "Content violates moderation policy: " & body.reason;
return result;
}
return result;
}
}
component {
variables.aiService = "";
public
void function onApplicationStart() {
var chatModel = ChatModel({
PROVIDER : "openAi",
APIKEY : application.aiApiKey,
MODELNAME : "gpt-4o-mini"
});
variables.aiService = AiService({
CHATMODEL: chatModel,
INPUTGUARDRAILS: [
expandPath("./guardrails/PromptInjectionGuardrail.cfc"),
expandPath("./guardrails/ContentFilterGuardrail.cfc")
],
OUTPUTGUARDRAILS: [
expandPath("./guardrails/HarmfulContentGuardrail.cfc")
]
});
}
}
public string function handleUserQuestion(required string question) {
try {
return application.aiService.chat(question);
} catch (any e) {
// Centralized error handling for all guardrail failures
logError("AI guardrail failure: #e.message#");
return "I’m unable to answer that request. Please try a different "
"question.";
}
}