Skip to content

Commit

Permalink
[mod] check secret_key value when the webapp starts
Browse files Browse the repository at this point in the history
Before this commit the secret_key is checked as soon as the searx module is imported.

So the documentation, utils/standalone_searx.py have to set SEARX_DEBUG=1

With this commit:
* searx check the secret_key when the webapp is loaded
* after searx loads that the engines.

For reference see searx#2386
  • Loading branch information
dalf committed Dec 21, 2020
1 parent 2555996 commit 3fa1895
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 0 additions & 4 deletions searx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,3 @@
settings['server']['secret_key'] = environ['SEARX_SECRET']
if 'SEARX_BIND_ADDRESS' in environ:
settings['server']['bind_address'] = environ['SEARX_BIND_ADDRESS']

if not searx_debug and settings['server']['secret_key'] == 'ultrasecretkey':
logger.error('server.secret_key is not changed. Please use something else instead of ultrasecretkey.')
exit(1)
33 changes: 28 additions & 5 deletions searx/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@
# see run() at the end of this file : searx_debug activates the reload feature.
werkzeug_reloader = flask_run_development or (searx_debug and __name__ == "__main__")

# initialize the engines except on the first run of the werkzeug server.
if not werkzeug_reloader\
or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"):
initialize()

babel = Babel(app)

rtl_locales = ['ar', 'arc', 'bcc', 'bqi', 'ckb', 'dv', 'fa', 'fa_IR', 'glk', 'he',
Expand Down Expand Up @@ -1135,9 +1130,37 @@ def __call__(self, environ, start_response):
return self.app(environ, start_response)


class InitializeApplication:
'''Check settings['server']['secret_key'] when the application is started
Allow to import searx.webapp without issue.
'''

def __init__(self, app):
self.app = app

# check secret_key
if not searx_debug and settings['server']['secret_key'] == 'ultrasecretkey':
logger.error('server.secret_key is not changed. Please use something else instead of ultrasecretkey.')
exit(1)

# initialize the engines except on the first run of the werkzeug server.
if not werkzeug_reloader\
or (werkzeug_reloader and os.environ.get("WERKZEUG_RUN_MAIN") == "true"):
initialize()

def __call__(self, environ, start_response):
return self.app(environ, start_response)


# uwsgi application
application = app

# patch app to handle non root url-s behind proxy & wsgi
app.wsgi_app = ReverseProxyPathFix(ProxyFix(application.wsgi_app))

# initiliaze the webapp when it is started not loaded
app.wsgi_app = InitializeApplication(app.wsgi_app)


if __name__ == "__main__":
run()
2 changes: 1 addition & 1 deletion utils/standalone_searx.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.. code:: bash
$ SEARX_DEBUG=1 python3 utils/standalone_searx.py rain
$ python3 utils/standalone_searx.py rain
Example to run it from python:
Expand Down

0 comments on commit 3fa1895

Please sign in to comment.