21 Commits

Author SHA1 Message Date
Sunil Mohan Adapa
2f0ec1a1cd
web_framework: Disable caching templates files in development mode
- When a template page is updated, we are having to restart service in order for
the new changes to reflect. This is due to caching of template files starting
Django 4.1[1]. Disable this behavior in development mode to allow reload the
browser page to see changes reflected.

Links:

https://docs.djangoproject.com/en/5.0/releases/4.1/#templates

Tests:

- Change a template file and reload the page without restarting service. The
changes should reflect immediately.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2025-01-07 11:45:57 +02:00
Sunil Mohan Adapa
770974c8ce
sso: Switch to django-axes >= 5.0
- Add explicit dependency on django-ipware >=3. django-axes >= 6 adds
only and optional dependency on django-ipware. Adding explicit dependency make
the behavior safer.

- Depend on django-axes >= 5 where the authentication backend and other features
are available. The new code won't work with older versions. The new approach
uses and authentication backend to deny access to the login form on lockout and
a middleware to redirect user to locked out form when limit of attempts have
been reached.

- Drop old code used for compatibility with django-axes 3.x.

- Suppress verbose and debug messages as django-axes is too chatty.

- Re-implment the CAPTCHA form entirely. In the old style, we have a login form
with CAPTCHA field. That would not work with the new django-axes authentication
middle. On submission of the form, auth.authenticate() will be called. This
call invokes various authentication backends include django-axes authentication
backend. This backend's behavior is to reject all authentication attempts when
the IP is listed in locked table. The new approach is to provide a simple
CAPTCHA form with just the CAPTCHA field. If the form is successfully
validated (correct CAPTCHA is provided), then the lock on the IP address is
reset. The user is then free to perform 3 more attempts to login.

- Update firstboot form to send the request parameter when using
auth.authenticate() method. This needed by Django axes' authentication method
which will be triggered.

Tests:

- Run tests on Debian Bookworm and Debian testing.

- Axes verbose messages and debug messages are not printed on the console when
running FreedomBox in debug mode.

- Only three invalid attempts are allowed at the login page. After the final
incorrect attempt, user is redirected to CAPTCHA page. Visiting the login page
using the URL works but entering the correct credentials still takes the user to
CAPTCHA page.

- CAPTCHA form appears as expected. Clicking the CAPTCHA images downloads the
audio file corresponding to the image. Incorrect CAPTCHA shows an error. Correct
CAPTCHA takes the user to login form where they are able to login with correct
credentials. Entering incorrect credentials 3 times will take the user again to
CAPTCHA page.

- Creating user account during firstboot works.

- Blocked IP address the IP of the client such as 10.42.0.1 and not the local IP
address 127.0.0.1 according the django-axes log messages. While one client IP
address is blocked, another IP is able to login to the same user account that
was attempted by the blocked client.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2023-08-23 21:47:39 -04:00
Sunil Mohan Adapa
abf389aaa1
main: Drop initializing Django when listing dependencies
- Avoid any potential problems with sqlite3 not being present.

- Avoid any potential problems with secret key not being available. Avoid
accidentally creating the secret key hardware/container images.

Tests:

- --list-dependencies works with and without secret key and plinth sqlite3.

- --list-dependencies works with plinth and root users.

- --list-dependencies does not create the sqlite3 database or the secret key.

- Running app works in normal mode and creates the secret key.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-11-18 06:48:41 -05:00
Sunil Mohan Adapa
28c1d5eda3
web_framework, tests: Workaround captcha 0.5.6 vs. Django 3.2
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-09-18 08:36:46 -04:00
Sunil Mohan Adapa
75a46962f0
module_loader, web_framework: Update console log messages
- Drop message for initialization of each app. This was removed earlier but was
reintroduced during init() refactoring. There is not much use for these messages
and the noise they generate hide some important messages relating to domains
added.

- Print lists in a better way for humans.

- Add a log message after all the initialization is completed.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-09-27 09:54:09 -04:00
Sunil Mohan Adapa
e5177289dc
web_framework: Don't create Django secret key when listing depends
This allows --list-dependencies to run without having to write to disk.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-06-28 21:01:57 -04:00
Sunil Mohan Adapa
28fe8c8c3e
web_framework: Split initialization into two parts
A simple Django configuration does not need to create the database whereas DB
migration requires creating the database.

In some operations such as listing dependencies, we can skip running the second
part and so writing to database will no longer be necessary during such
operations.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-06-28 21:01:55 -04:00
Sunil Mohan Adapa
f2d43737fa
web_framework: Reduce verbosity of DB migration process
During provisioning of container/VM, we need to run --list-dependencies. When
run without --develop, they are unable to create database. When run with
--develop, the output from the database migration process is messing up the
output meant for stdin as it is outputting to stdout instead stderr. Reduce
verbosity even in debug mode to fix this.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2020-06-04 18:19:36 +03:00
Sunil Mohan Adapa
61545d1b8d
web_framework: Cleanup expired sessions every week
Currently, sessions are created as files in /var/lib/plinth/sessions. If a user
does not logout, the sessions remains there ever after expiry. Cleanup these
accumulating files by running a cleanup job every week.

Adding django.contrib.sessions to apps list necessary to ensure that
'clearsessions' management command is available. This creates an empty database
table for session storage but is harmless.

Tests performed:

- When run with the change for first time, migration is run for
django.contrib.sessions app.

- Change the scheduled interval to 30 seconds in the code. Login as a user. A
new session file is created in data/var/lib/plinth/sessions. Forward the system
clock by at least 2 weeks. The session expires. Within 30 seconds the file is
also removed.

- Login, then remove the django-secret.key. In 30 seconds we see a message that
the session data is corrupt. Advance the clock by at least 2 weeks. The session
file is removed and the message about session data is no longer printed.

- Repeat for system level plinth after `./setup.py install` and `sudo -u plinth
plinth`.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-02-24 18:04:26 -05:00
Sunil Mohan Adapa
fd345aca80
web_framework: Generate and retain a secret key
- Secret is important for various functions of Django. There is no impact on
existing installations due to the change. Improves the security of existing
functions in minor ways and will be useful in future usage of Django.

- Create the file in /var/lib/plinth/ with 0o600 permissions.

- Make git ignore the file in code folder.

- Don't copy the file during './setup.py install' operation.

Impact to users after upgrade:

- All existing sessions will get logged out. This is because SECRET_KEY is used
to generate user session hash that is used to logout users when their password
changes.

Tests performed:

- Run development version of service. File should get created in
data/var/lib/plinth/django-secret.key. Permissions should be 0o600.

- Run again, the file should not be overwritten. Printing
django.conf.settings.SECRET_KEY should match the one in the file.

- Run `setup.py install`. This should not install django-secret.key in
/var/lib/plinth.

- Run `sudo -u plinth plinth`. This should create the secret key file in
/var/lib/plinth/django-secret.key. Permissions on the file should be 0o600.
Ownership should be plinth:plinth.

- Remove the file in both cases, a fresh new file should get created with new key.

- Truncate the file to less than 128 chars, the existing file should get
overwritten with new key.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-02-24 18:04:20 -05:00
Sunil Mohan Adapa
837e8fc6e1
web_framework: Separate out Django settings into module
This allows for many cases of having to deal with Django objects such as models.

- Allows all modules including ones with models to be imported by Sphinx.

- Run most of the management commands using django-admin.

- Make it simpler to import all modules on REPL interactive Python shells.

Does not change any of the settings that we are passing to Django for
configuration.

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-02-22 15:51:41 -05:00
Sunil Mohan Adapa
9368504da5
*.py: Use SPDX license identifier
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2020-02-19 14:38:55 +02:00
Sunil Mohan Adapa
4956a2a63a
web_framework: Set the timezone to UTC
This is needed to ensure that all the time/date values stored in the database
are UTC. A timezone change in the settings should not make the database values
inconsistent.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-02-10 18:13:01 -05:00
Alice Kile
eb83e00011
fix formatting issues
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-12-07 13:08:35 -05:00
Sunil Mohan Adapa
b0d797a84a
Minor yapf and isort changes
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-07-21 09:06:04 -04:00
Sunil Mohan Adapa
0f807bcd48
sso: Use new features of axes, log axes messages
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-07-21 09:05:58 -04:00
Sunil Mohan Adapa
dc9ab52edc
axes: Minor fixes to configuration for IP blocking
- Use the X-Forwarded-For header only if specified in the configuration. This
  makes FreedomBox safe to use when not behind a reverse proxy server (although
  we are unlikely to do this).

- When fetching the IP address to reset after successful login, use the
  X-Forwarded-For header only if specified in the configuration.

- Minor flake8 refactorings.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-01-28 17:17:32 -05:00
James Valleroy
de2f1437ea
django: Remove deprecated AXES_BEHIND_REVERSE_PROXY
Closes #1244

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2019-01-23 16:24:16 -08:00
Sunil Mohan Adapa
1faee11d4d
django: Use Argon2 password hash
Passwords will be automatically upgraded for each user on login.

Usernames not yet upgraded are vulnerable to user enumeration attack due to
difference in password check timing.

No need to add build dependency on python3-argon2 because tests use a different
Django configuration which does not use argon2 hash.

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-01-13 14:46:15 -05:00
Sunil Mohan Adapa
7ee4d13dce Show Gujarati in the list of UI languages
- Explicitly setup the list of Django languages. Keep them sorted by language
  code.

- Fallback to language name when Django can't provide locale language name.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
2019-01-09 14:52:02 +05:30
Sunil Mohan Adapa
40ecce554f main: Separate out Django setup into a separate module
This has less to do with abstraction than to do with modularization.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
2019-01-09 14:52:02 +05:30