Chatbot security

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;

}

})();

Adding rel="noopener" to outgoing links (which I assume would all open in a new tab/window) would also enhance security and is easy to implement.

Hi there, @David_Zeegers :waving_hand:

I am glad to say that you can set the specific pages, where you’d like to display the widget in the Visibility section on the Settings tab:


Give it a try and let me know if it helped :slightly_smiling_face:

Hi @David_Zeegers,

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.

Thank you!

[UPDATE]

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.

Hi @Petar_Dietrich :waving_hand:

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 :slightly_smiling_face:

Hi Max,

Thanks for your valuable input. Yes, we’re using both the “Visibility on Pages” feature and the above JS code as an optional security measure.

As the topic creator, perhaps @David_Zeegers would like to chime in as well?

Cheers!