32 Commits

Author SHA1 Message Date
Sunil Mohan Adapa
2467d6a033
middleware: Implement middleware for common headers such as CSP
- This allows overriding these headers in individual pages easily instead of
relaxing global policy.

- Drop the obsolete CSP directive "block-all-mixed-content" and avoid a console
warning in Firefox.

Tests:

- Load a page and notice in the browser developer tools that the three headers
referrer-policy, content-security-policy, and x-content-type-options are set as
before.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2025-11-17 13:02:52 -05:00
Sunil Mohan Adapa
26d317bfd5
middleware: Handle page not found errors specially
- Show a different message for them.

Test:

- Try to visit page like /plinth/apps/sharing/foo/edit/ where a share named
'foo' does not exist. The common error handling middleware is triggered and an
alert message 'Page not found' with exception trace back is shown.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2024-12-29 17:16:48 +02:00
Sunil Mohan Adapa
38829a3cfa
middleware: Handle method not allowed errors and redirect
- These could happen when a user tries to reload a page that only allows POST
requests.

- Or when the generic exception handling logic in the middleware redirects the
user to a page that only allows POST.

Tests:

- Insert a exception in the diagnose() of the 'users' app. Without the patch,
running diagnostics on the users app leads to a blank page. With the patch, the
user is redirected to Diagnostics app page and the original error is shown as a
alert message.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2024-12-29 17:16:23 +02:00
Sunil Mohan Adapa
407fccba2f
ui: Handle and show most page load errors as alerts
- In addition to the OperationalError, also handle all generic exceptions during
page submit and page load. Redirect to the same page or parent using
breadcrumbs.

- Log exceptions handled by common error middleware so that they are also part
of the system logs.

- Update kiwix test as needed.

- Refactor some test code that is setting up the menu items.

Tests:

- When an error occurs during form POST, the same page is show but with an error
message.

- When an error occurs in an app page during GET, the browser is redirected to
the parent section.

- When an error occurs in apps page during GET, the browser is redirected to the
home page.

- When an error occurs in home page during GET, the error is not handled and
default 500 handle is triggered.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2024-12-29 17:16:06 +02:00
Sunil Mohan Adapa
9459ef4be4
middleware: Show translated error messages when operation completes
Tests:

- Change locale to Spanish. Raise an exception in bepasty.privileged.setup().
Try to install bepasty. Without the patch, error is not translated. With the
patch, it is.

- Successful installation of an app shows translated success message.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2024-10-21 12:47:10 -04:00
Sunil Mohan Adapa
28e417d7ef
middleware: Show HTML exception message as extra detail in messages
Tests:

- Insert an exception in privileged setup() and uninstall() method and try to
install/uninstall app and notice that error messages are shown in a simple
format and extra details are provided, collapsed by default.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2024-03-19 11:46:23 -04:00
Brian Ó Donnell
557a3b2588
middleware: Add new middleware to handle common errors like DB busy
- During database error such as 'database is locked', show a special message
asking users to try again instead of submitting a bug report.

[sunil: Minor formatting, rename the template file name]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
[jvalleroy: Fix missing import]
Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2023-09-22 11:19:47 -07:00
Sunil Mohan Adapa
4cb1477c0d
setup: Drop setup_helper and use the new Operation API
- Task of managing an operation's progress is now performed by the new Operation
class. Drop them from setup helper.

- Task of providing install() method is now moved to package module. Instead of
storing operation specific data in setup_helper like objects, store them in
thread specific storage that can retrieved anywhere during the operation without
holding references.

- Progress of an operation show as a progress bar is currently missing. This
will be regression until fixed later.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2022-08-15 10:36:32 -04:00
Sunil Mohan Adapa
528fd08245
middleware, views: Reduce use of setup_helper
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-12-04 16:38:17 -05:00
Sunil Mohan Adapa
cf36a9d385
*: Drop use of module level is_essential flag
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-12-04 16:37:59 -05:00
Sunil Mohan Adapa
b609abe7e5
*: Use the App's state management API
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-12-04 16:37:40 -05:00
Sunil Mohan Adapa
b0e3aaa356
middleware: Don't show setup view to non-admin users
This is an improvement over !2069, which solved #2094.

Tests:

- Install an app. Success result is shown.

- Install an app and kill the apt-get process in the middle. Error result is
shown.

- Click install on an app (email_server). Close the window without seeing the
result. Access the app page (like email_server/my_aliases) as a non-admin user.
No success/file message is shown. The page is shown properly. Access the page as
admin, success message is shown.

- Access an uninstalled/installed app page as anonymous user. User is redirected
to login page.

- Access an uninstalled/installed app page as non-admin user. Forbidden page is
shown.

- Access an uninstalled app page meant for non-admin users (such as
email_server/my_aliases) as non-admin user. Forbidden page is shown.

- Access an installed app page as admin. Success.

- Access an installed app page meant for non-admin users (such as
email_server/my_aliases) as admin. Success.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>

Reviewed-by: Fioddor Superconcentrado <fioddor@gmail.com>
2021-10-16 20:56:23 +02:00
Sunil Mohan Adapa
74214c18ae
*: Use Django gettext functions instead of ugettext
- ugettext functions will be removed in Django 4.0. Each use emits a warning
when running with Django 3.2. Since we have warnings enabled in developer mode,
we see quite a few messages because of this.

- ugettext is already a simple alias of gettext. So, no regressions are
expected.

Tests:

- Accessing an affected app in UI with Django 3.2 and Django 2.2 works fine.

- Using Django 3.2 there are no warnings related to removal of ugettext
functions.

- Ran regular unit tests.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-09-20 16:50:16 -04:00
Sunil Mohan Adapa
084921ac0d
first_setup: Use template variable to refresh page
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
2020-12-19 16:26:52 +02:00
Joseph Nuthalapati
377010b078
openvpn: Deny access to users not in group "vpn"
A new function called `user_group_view` has been added to
restrict access to only users in the groups "vpn" and "admin".

Some changes are made in AdminRequiredMiddleware to handle this.

Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-11-25 13:15:22 -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
Alice Kile
eb83e00011
fix formatting issues
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2019-12-07 13:08:35 -05:00
James Valleroy
2ea575806d
middleware: Skip 'installed' message for essential apps
Closes #1085.

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
2018-04-09 19:10:49 +05:30
Sunil Mohan Adapa
1da594c32d
Support Django 2.0
This is the final change required for Django 2.0 support. Instead of using
MIDDLEWARE_CLASSES use MIDDLEWARE setting. Support for new style middleware was
provided in version 0.3.0, so depend on that version. Django built-in middleware
already supports new style and plinth middleware will now support new style.

The actual semantics of the middleware don't need changes. See:
https://docs.djangoproject.com/en/2.0/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2018-03-12 17:27:26 -04:00
Sunil Mohan Adapa
dea4af17fb
Rename Plinth to FreedomBox in license headers
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2018-02-16 20:10:09 -05:00
Sunil Mohan Adapa
1247f2ef93
setup: Prioritize FirstSetupMiddle over some others
- Minor PEP8 fixes.
- Add doc strings.
- Make a few methods private.
- Remove unused global declarations.
- Remove unused imports.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
2017-10-11 18:13:14 +05:30
Joseph Nuthalpati
7ce56864e9
setup: Fix Plinth startup issues
- Selectively run setup for modules
- Middleware pending

Fixes #1024

Signed-off-by: Joseph Nuthalpati <njoseph@thoughtworks.com>
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2017-10-11 18:12:45 +05:30
Soumya97
6a53dd015e
context_processor: Cache whether user is admin (Closes: #781) 2017-04-18 16:01:48 +05:30
Rahul De
ad0b235dd7
users: Restrict groups and active user control to admins
- Only admins can now edit the groups of any user

- Only admins can mark any user as active or not

- Refactored all occurrences of admin checks to its own utility function
2017-02-15 21:07:54 +05:30
Sunil Mohan Adapa
dd5ab7612e
Minor refactoring and lint fixes
- Rename AdminMiddleware to AdminRequiredMiddleware to be consistent
  with stronghold/Django terminology

- Simplify .gitignore pattern

- Format single line docstrings as per PEP8.

- Add missing docstrings.

- Restrict lines to 79 characters.
2017-02-15 21:07:27 +05:30
lispyclouds
3b23f78bdc
Implement middleware for admin views
- Add AdminMiddleware to deny non admin users
- Add decorator to mark views as "non admin"
2017-02-15 20:57:52 +05:30
Sunil Mohan Adapa
b7a974e326
setup: Fix an infinite redirect in a rare case
Don't try to show setup view for login page.  This happens under a rare
circumstance that user does not usually face.  If 'users' module has not
been setup but we try to run first boot and last part of the firstboot
process is not yet completed and when user is not already logged in, an
infinite redirect happens.

Simply don't try to show setup view for login URL under any
circumstance.  This is similar to how firstboot middleware itself does
not meddle with login URL.
2017-01-19 18:46:06 -05:00
fonfon
da91981ff5
Prevent anonymous users from accessing any setup page
Anonymous users were able to access pages that used
the 'public' decorator of stronghold. If such a page
showed the installation routine of the setup module
they were able to access and use it, in other words:
Anonymous users were able to install software.
2016-12-28 19:35:04 -05:00
Sunil Mohan Adapa
18996b5c56
Import urlresolvers from django.urls
In Django 1.10, django.core.urlresolvers is available from
django.urls. Use it.
2016-08-11 18:03:00 -04:00
Sunil Mohan Adapa
cde615aee6
middleware: Don't interfere in 404 URLs in setup
Don't raise a 404 exception when a page is not found.  Instead, simply
bypass the setup middleware so that other middleware have a chance to
process further.

Slight modification so that resolve method can be mocked during
unittests.
2016-06-02 20:13:40 -04:00
Sunil Mohan Adapa
f34cb78867
middleware: Fix anonymous access to setup view
Django runs process_request() on all the middleware classes in order.
Then it runs process_view() on all of them again.  During the runs, if
any of the views return a HttpResponse.  Further processing is ignored.

Setup middleware was handling process_request() and returning
HttpResponse if the application as not setup.  However, stronghold
middleware handles process_view().  Since process_request happens first
and response is being returned there, stronghold middleware is never
called.

Make setup middleware use process_view() instead.
2016-06-02 20:13:35 -04:00
Sunil Mohan Adapa
8dcafe3e0e
middleware: New middleware to check need for setup
- Call the setup helper to check if a installation or update of a module
  is required.

- Show installation and progress using the setup view.
2016-02-13 13:50:16 +05:30