javascript - 部署 Django 时的内容安全策略问题
问题描述
我想部署我的 django 应用程序,为此,在 settings.py 中我设置了 DEBUG=False 因为我这样做了,我的网站不再运行,并且在 Firefox 控制台中,我收到了大量关于内容安全策略的错误。在过去的两天里,我试图解决这个问题,但似乎无法掌握它。这是我要部署的第一个 django 项目,所以我可能遗漏了一些明显的东西。我正在使用一些内联 JavaScript 和 CSS,我正在从谷歌 CDN 加载我的 jquery 和 ajax。我在 base.html 页面的标题中设置了 CSP,如下所示:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; frame-src *;">
为了测试它,我让它非常不安全,但如果我的脚本想要加载,仍然没有。这是为什么?
控制台日志:
Loading failed for the <script> with source “https://code.jquery.com/jquery-3.5.0.js”. account:106:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js”. account:107:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js”. account:108:1
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). account:18:1
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). account:32:1
Content Security Policy: The page’s settings blocked the loading of a resource at https://use.fontawesome.com/releases/v5.13.0/css/all.css (“style-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://use.fontawesome.com/releases/v5.13.0/css/all.css (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). 4 account
Content Security Policy: The page’s settings blocked the loading of a resource at https://code.jquery.com/jquery-3.5.0.js (“script-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://code.jquery.com/jquery-3.5.0.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js (“script-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js (“script-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). element-collapser.js:88:56
TypeError: i is undefined
infinite.min.a9e9aa085994.js:7:1207
Loading failed for the <script> with source “http://www.privacypolicies.com/public/cookie-consent/3.1.0/cookie-consent.js”. account:114:1
Content Security Policy: The page’s settings blocked the loading of a resource at http://www.privacypolicies.com/public/cookie-consent/3.1.0/cookie-consent.js (“script-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at http://www.privacypolicies.com/public/cookie-consent/3.1.0/cookie-consent.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). account:115:1
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). account:126:1
Layout was forced before the page was fully loaded. If stylesheets are not yet loaded this may cause a flash of unstyled content. preload.js:101:24
Loading failed for the <script> with source “https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”. list-view:58:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js”. list-view:59:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js”. list-view:60:1
Content Security Policy: The page’s settings blocked the loading of a resource at https://use.fontawesome.com/releases/v5.13.0/css/all.css (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). list-view:62:1
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). preload.js:169:52
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). 2 utils.js:35:9
Loading failed for the <script> with source “https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js”. list-view:84:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js”. list-view:85:1
Loading failed for the <script> with source “https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js”. list-view:86:1
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). list-view:17:1
Content Security Policy: The page’s settings blocked the loading of a resource at https://use.fontawesome.com/releases/v5.13.0/css/all.css (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). list-view:77:1
Content Security Policy: The page’s settings blocked the loading of a resource at https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at https://cdn.jsdelivr.net/npm/jquery-validation@1.19.1/dist/jquery.validate.js (“default-src”).
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). list-view:88:1
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”). 2 utils.js:35:9
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“default-src”).
我的设置.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'mySecretKey'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = True
# DEBUG = int(os.environ.get('DEBUG', default=1))
ALLOWED_HOSTS = ['*']
LOGIN_REDIRECT_URL = 'account:dashboard'
LOGIN_URL = 'account:login'
LOGOUT_URL = 'account:logout'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user@gmail.com'
EMAIL_HOST_PASSWORD = "password"
# Application definition
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, '') # not images, because i take that from the outer folder
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
INSTALLED_APPS = [
'book',
'landing',
'account',
'club',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'sorl.thumbnail',
'crispy_forms',
'ajaximage',
'el_pagination',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'bookclub.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request', # For EL-pagination
],
},
},
]
WSGI_APPLICATION = 'bookclub.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'bookclubdb',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '', # default value: 5432
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
AJAXIMAGE_AUTH_TEST = lambda u: True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
解决方案
推荐阅读
- python - AttributeError:“collections.OrderedDict”对象没有属性“train”
- java - 有没有办法通过代码或命令行(JDK 9+)禁用原生 DPI 缩放?
- selenium - 如何使用 selenium Junit 验证页脚文本
- firebase - 错误:未找到请求的实体。在 FirebaseMessagingError.FirebaseError [作为构造函数]
- django-rest-framework - get_queryset 被执行而不是 get_object
- python - 如果两个区域相交,则合并它们
- vba - 使用 VBA 在whatsapp web 中发送图像
- python - python特征选择:chi2和chi2_contingency之间的矛盾
- html - 节点静态 Web 服务器页面中的奇怪标题(“b8”)
- selenium-chromedriver - 使用 selenium 将 URL 自定义 URL 传递给 chrome 驱动程序时出现“参数无效异常错误”