langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
48.5k stars 6.94k forks source link

What does the CORS env variables do? #8456

Closed taowang1993 closed 1 month ago

taowang1993 commented 1 month ago

Self Checks

Provide a description of requested docs changes

If set these variables to my own domain, does it prevent others from embedding my chatbot? The iframe already exposes the url, people can just copy the iframe code and embed my chatbot.

<iframe
 src="http://localhost/chatbot/zpbXMm2liB3QYoxb"
 style="width: 100%; height: 100%; min-height: 700px"
 frameborder="0"
 allow="microphone">
</iframe>

CONSOLE_CORS_ALLOW_ORIGINS=my-frontend-domain WEB_API_CORS_ALLOW_ORIGINS=my-frontend-domain

dosubot[bot] commented 1 month ago

The CORS environment variables in Dify are used to set cross-origin access policies. Specifically:

If you set these variables to your own domain, it will restrict access to only your specified domain, thereby preventing others from embedding your chatbot. For example:

CONSOLE_CORS_ALLOW_ORIGINS=my-frontend-domain
WEB_API_CORS_ALLOW_ORIGINS=my-frontend-domain

This configuration ensures that only requests from my-frontend-domain are allowed, preventing unauthorized embedding of your chatbot [1][2].

To continue talking to Dosu, mention @dosu.

taowang1993 commented 1 month ago

I already set them to my own domain. WEB_API_CORS_ALLOW_ORIGINS=https://riverhill.ai CONSOLE_CORS_ALLOW_ORIGINS=https://riverhill.ai But I can still embed the chatbot on other domains. I want to make sure the chatbot only works on my domain.

dosubot[bot] commented 1 month ago

To ensure that your chatbot only works on your domain, you need to set additional environment variables beyond WEB_API_CORS_ALLOW_ORIGINS and CONSOLE_CORS_ALLOW_ORIGINS. Here are the steps you should follow:

  1. Set the following environment variables to your domain:
export CONSOLE_API_URL="https://riverhill.ai/api/console"
export CONSOLE_WEB_URL="https://riverhill.ai/console"
export SERVICE_API_URL="https://riverhill.ai/api/service"
export APP_API_URL="https://riverhill.ai/api/app"
export APP_WEB_URL="https://riverhill.ai/app"
  1. Ensure that WEB_API_CORS_ALLOW_ORIGINS and CONSOLE_CORS_ALLOW_ORIGINS are set to your domain:
export WEB_API_CORS_ALLOW_ORIGINS="https://riverhill.ai"
export CONSOLE_CORS_ALLOW_ORIGINS="https://riverhill.ai"
  1. Add a check in your chatbot's code to verify the referrer domain. For example, if you are using Dify, you can use a utility function to check if the referrer is your domain:
export const isMyDomain = () => {
  return document.referrer.includes('riverhill.ai')
}

By setting these environment variables and adding a referrer check in your code, you can ensure that your chatbot only works on your specified domain [1][2].

To continue talking to Dosu, mention @dosu.