为什么我会收到一个“SSL错误:叫你不应该调用一个函数”和Django(Why do I get a

2019-09-30 10:31发布

我有一个Python 3.5 / Django 1.10的应用程序通过提供服务Apache / mod_wsgi SSL上。 它被连接到Postgres 9.5.2数据库(用psycopg2==2.6.2 )中,在一个服务器上运行AlwaysData

它工作正常,大部分的时间,但我有时我不理解错误。

(SSL error: called a function you should not call)

如果我把下面的数据库设置:错误似乎发生每次

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'host',
        'PORT': '',
        'OPTIONS': {
            'sslmode': 'require',
        },
    }
}

这似乎在查询数据库发生。

# django/db/backends/utils.py line 64
return self.cursor.execute(sql, params)

当REST API(使用Django静止框架制造)由Angular2应用程序称为出现问题。

我已经激活了如下设置:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

为什么会发生? 如何解决在我的Django项目这个问题。

注意: 这个问题看起来类似,但所以不是非常有帮助,我不直接管理的OpenSSL层。

编辑:这里是完整的回溯

File "proj/env/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "proj/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "proj/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "proj/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "proj/env/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "proj/env/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "proj/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  474.             response = self.handle_exception(exc)

File "proj/env/lib/python3.5/site-packages/rest_framework/views.py" in handle_exception
  434.             self.raise_uncaught_exception(exc)

File "proj/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  471.             response = handler(request, *args, **kwargs)

File "proj/apps/costs/apis.py" in get
  296.         data = self.get_spends_stats(cost_items, perimeter, start_date, end_date)

File "proj/apps/costs/apis.py" in get_spends_stats
  306.         for building in buildings:

File "proj/env/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  256.         self._fetch_all()

File "proj/env/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
  1087.             self._result_cache = list(self.iterator())

File "proj/env/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql()

File "proj/env/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "proj/env/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "proj/env/lib/python3.5/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "proj/env/lib/python3.5/site-packages/django/utils/six.py" in reraise
  685.             raise value.with_traceback(tb)

File "proj/env/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

Exception Type: OperationalError at /costs/api/benchmark/cost-center/3/38/2016-01/2017-12/
Exception Value: SSL error: called a function you should not call

Answer 1:

看起来像一个psycopg2错误(或更确切地说, 如毗卢指出 ,底层libpq的错误)。 这似乎违反了所需的呼叫顺序- 可能不会等待某些事件 。 由于这种不规则时,它可以是一个竞争状态。

它甚至提供了有关这是另一种错误的错误不完整的信息。 它应该使用ERR_print_errors()来获得它的格式完整的消息[pid]:error:[error code]:[library name]:[function name]:[reason string]:[file name]:[line]:[optional text message]



Answer 2:

有一个相关的bug#58956在阿帕奇+ OpenSSL的在SSL_shutdown握手,用完全相同的错误消息,已经结束的固定由OpenSSL的在2016年二月尝试升级到1.0.2g或1.1.0或更高版本。


编辑:如果你有一些版本1.0.2(也许更多的版本,但您感兴趣的包被链接到1.0.2),那么SSL的升级是值得考虑的。 版本1.1.0这里所写的只是出于完整性考虑任何人都可以轻松地后检查版本,是否与此有关的bug。 现在没有人有1.1可能在生产主机和一个自制的升级,这将是可能是一个坏主意。



Answer 3:

这个问题似乎通过更改数据库设置来解决

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'host',
        'PORT': '',
        'OPTIONS': {
            'sslmode': 'disable',
        },
    }
}

如果没有设置该选项使用prefer为默认(参见https://www.postgresql.org/docs/9.5/static/libpq-ssl.html )似乎有不可预知的行为。

我想,根本原因就是Apache和Postgres之间的OpenSSL的不匹配。 它具有可查。

目前的修复,使不安全的数据库连接,但这是另一回事。



文章来源: Why do I get a “SSL error: called a function you should not call” with Django