To prevent AI agent hijacking, is it possible to add some security features to for instance limit it to the installed domain and making the chatbot available in a site CSP context? (for instance being able to add a nonce based on the site generated nonce. I did solve this for now by using:
(() => {
const allowedHosts = new Set([
"url",
"www.url"
]);
if (!allowedHosts.has(window.location.hostname)) {
document.documentElement.style.display = "none";
return;
}
})();
Great topic. Thanks for bringing this up. Have you been a victim of AI agent hijacking? How did you incorporate your code (i.e., via the configurator’s JS code field)? Also, how did you test its efficacy?
In any case, I would assume the AI chatbot developers have already integrated measures to prevent their widget from being hijacked; however, that is definitely something for the developers to answer.
Upon further analysis, I learned the JS code shared above should be entered in the AI Chatbot configurator’s JS Code field.
The JS code won’t make the chatbot impossible to hijack, but it raises the barrier to casual copying. If used on an unauthorized domain, the page goes blank. It serves as an additional deterrent/security layer above the configurator’s “Visibility on Pages” feature, rather than as a cryptographically secure access-control mechanism.
To further assist, if you want to add a console warning to any website (bad actor) attempting to hijack the AI agent, use this JS code instead:
(() => {
const allowedHosts = new Set([
"mysite.com",
"www.mysite.com"
]);
const currentHost = window.location.hostname.toLowerCase();
if (!allowedHosts.has(currentHost)) {
console.warn(
"Security warning: Unauthorized domain detected. " +
"This Elfsight chatbot is authorized only for (mysite.com)."
);
document.documentElement.innerHTML = "";
}
})();
I added the JS code to our configurator. Works great. Thanks for sharing, @David_Zeegers!
I hope this further clarifies the topic.
Note: In the original code, replace url or mysite with your actual domain.
Thanks for looking into this and for testing the approach! I also discussed it with our devs and wanted to add a bit of context from their side.
This custom JS can be useful as an optional extra measure, but it’s better not to treat it as a security mechanism in itself. It doesn’t prevent the widget from being copied or reused; rather, it can make unauthorized use less convenient by hiding or breaking the page where the widget is embedded.
Because of that, we’d recommend using the built-in Visibility on Pages setting as the main way to control where the widget should appear. The custom script can still be used if someone specifically wants this kind of behavior, but it should be configured carefully so it doesn’t accidentally affect legitimate pages on their own site.
One small terminology note: in this case, “unauthorized use” may be a more accurate description than “hijacking”, since the widget itself isn’t being compromised.
Thanks again for raising the topic and digging into it