Commit Graph

95 Commits

Author SHA1 Message Date
Usama Sadiq
b6828cecaa fix: enable pylint warnings (#36195)
* fix: enable pylint warnings
2025-01-30 17:15:33 +05:00
Emad Rad
c27d55a253 chore: typos fixed 2024-05-25 18:05:28 +03:30
Muhammad Soban Javed
e40a01c7cc feat!: upgrade Django version to 4.2 (LTS)
This reverts commit 23659d5ba8.
2024-02-02 17:08:54 +05:00
Usama Sadiq
23659d5ba8 Revert "feat!: upgrade Django version to 4.2 (LTS) (#34162)" (#34165)
This reverts commit 1fc2e8a771.
2024-02-01 19:29:56 +05:00
Muhammad Soban Javed
1fc2e8a771 feat!: upgrade Django version to 4.2 (LTS) (#34162)
* feat!: upgrade Django version to 4.2 (LTS)
---------

Co-authored-by: iamsobanjaved <iamsobanjaved@users.noreply.github.com>
2024-02-01 16:10:34 +05:00
Muhammad Soban Javed
790e364097 Revert "chore!: upgrade Django version to 4.2 (LTS)" 2024-01-31 17:14:43 +05:00
Awais Qureshi
79f1c66553 Merge branch 'master' into iamsobanjaved/django-42-lts 2024-01-30 12:53:33 +05:00
Syed Sajjad Hussain Shah
cb2a34e51f feat: logout other sessions on email change (#33846)
* feat: logout other sessions on email change

* fix: updated the approach for session invalidation

* fix: update and add tests

* fix: update tests with descriptive comments

* feat: add integration tests

* fix: store email in session update

* fix: add setting for tests

* fix: fix tests

* feat: Upgrade Python dependency edx-drf-extensions (#34135)

Commit generated by workflow `openedx/edx-platform/.github/workflows/upgrade-one-python-dependency.yml@refs/heads/master`

Co-authored-by: syedsajjadkazmii <syedsajjadkazmii@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: syedsajjadkazmii <syedsajjadkazmii@users.noreply.github.com>
2024-01-29 14:43:51 +05:00
Muhammad Soban Javed
67d53dce26 chore!: upgrade Django version to 4.2 (LTS)
This reverts commit 6f32d0e6f1.
2024-01-25 18:17:43 +05:00
Muhammad Soban Javed
6f32d0e6f1 Revert "chore!: upgrade Django version to 4.2 (LTS)" (#34095) 2024-01-23 19:07:53 +05:00
Muhammad Soban Javed
fc8e40fb10 chore!: upgrade Django version to 4.2 (LTS) (#33969)
* chore!: upgrade Django version to 4.2 (LTS)

* feat: Upgrade Python dependency Django

chore!: upgrade Django version to 4.2

Commit generated by workflow `openedx/edx-platform/.github/workflows/upgrade-one-python-dependency.yml@refs/heads/master`

* chore: Update common.py

* chore: Update common.py

---------

Co-authored-by: Muhammad Soban Javed <iamsobanjaved@gmai.com>
Co-authored-by: iamsobanjaved <iamsobanjaved@users.noreply.github.com>
Co-authored-by: Awais Qureshi <awais.qureshi@arbisoft.com>
2024-01-23 16:53:34 +05:00
Feanil Patel
7202c22e1d Merge pull request #33373 from openedx/feanil/update_password_length_default
feat: Update the minimum password length.
2023-10-17 10:08:15 -04:00
Feanil Patel
64e91d4080 test: Update to an even longer password. 2023-10-12 10:31:13 -04:00
Awais Qureshi
254903531a feat!: Adding fall back method for sha1 in case default algo is sha256 (#33345)
* feat!: `sha1` has been deprecated in django32 and removed in django42.

* test: fix quality failure

* fixup! update custom attribute tests (#33436)

I was wondering about all the cases, so I
updated the test to reflect this. I also
made some other minor adjustments.

---------

Co-authored-by: Muhammad Soban Javed <iamsobanjaved@gmai.com>
Co-authored-by: Robert Raposa <rraposa@edx.org>
Co-authored-by: Muhammad Soban Javed <58461728+iamsobanjaved@users.noreply.github.com>
2023-10-11 19:54:01 +05:00
Feanil Patel
1e2ea85372 test: Update more tests that had short passwords. 2023-10-10 16:36:26 -04:00
Awais Qureshi
64eba521f8 Revert "[django42] sha1 is removed in django42 version. " (#33213)
* Revert "[django42] sha1 is removed in django42 version.  (#33129)"
2023-10-05 15:56:55 +05:00
Awais Qureshi
26bf329eb7 [django42] sha1 is removed in django42 version. (#33129)
* fix: fixing django42 issue. sha1 is removed in 42 version.
2023-09-11 15:30:42 +05:00
Usama Sadiq
7710e60328 fix: fix middleware get_response parameter deprecation warning (#33067) 2023-08-22 15:52:30 +05:00
Sarina Canelake
4a2f231302 fix: fix github url strings (org edx -> openedx) 2022-09-15 14:52:28 -04:00
Eugene Dyudyunov
b4fecd620b refactor: rename toggle_warnings to toggle_warning (#30458)
Rename toggle_warnings to toggle_warning for consistency with setting_warning.
2022-05-24 11:47:31 -04:00
Tim McCormack
80f60ffb36 refactor: Move log_sensitive to edx-django-utils (#30349)
It was copied there in 4.7.0 (openedx/edx-django-utils#209) so it can be used in more IDAs.

Includes dropping dependency on PyNacl, which was only in use by that module.
2022-05-06 12:42:19 +00:00
Tim McCormack
a1b09c0b8d fix: More resilience when calling encrypt_for_log with missing key (#29878)
It's likely that someone will at some point enable encrypted logging but
forget to deploy the config change that sets the key; if this happens, we
should gracefully return a warning rather than raise an exception.

Along the same lines, make sure that safe-sessions won't raise an exception
if the setting is missing, and document the suggested use of getattr.
2022-02-07 16:00:56 +00:00
Tim McCormack
95ad20afde test: Small cleanups in SafeSessions code (#29868)
- test: Remove reference to `REDIRECT_TO_LOGIN_ON_SAFE_SESSION_AUTH_FAILURE`,
  since it was removed in commit bd7653aefcd77a/PR #29132.
- docs: Clarify what "work correctly" means for header-logging
2022-02-04 14:43:41 +00:00
Tim McCormack
7c7792f92a fix: Delete JWTs and other cookies when SafeSessions deletes session cookie (#29857)
This is more correct and may reduce the likelihood of perpetuating a bad
mixed-auth state.

In general, we should probably be modifying session and JWT cookies in
sync at all times, never individually. This specific code probably won't
make anything worse, but a clean reset might improve user experience in
the rare cases where someone somehow gets their browser into a weird
state.

- Switch from `response.set_cookie` with past expiry to just using the
  `response.delete_cookie` method.
- Docstring improvements.

ref: ARCHBOM-2030 (internal)
2022-02-03 15:00:23 +00:00
Tim McCormack
9827a077aa feat: Enable ENFORCE_SAFE_SESSIONS by default; improve docs
This toggle has been shown to work, so enable by default. Will need to be
documented in release notes for deployers.
2022-01-25 16:03:54 +00:00
Tim McCormack
7fc20e69f4 feat: Allow safe-session exemption even for exceptions
Change `mark_user_change_as_expected` to no longer take the response object
and instead convey the expected-change information via RequestCache.
This requires edx-django-utils 4.4.2, which fixes the bug where
RequestCache was cleared in the exception phase.

Also, no longer mark `ENFORCE_SAFE_SESSIONS` toggle as
temporary. We'll want it as an opt-out.

I was tempted to take this opportunity to move any existing
`mark_user_change_as_expected` calls to be closer to where the actual
change request.user occurs, reducing risk of both false positives and false
negatives, but it would be better to do that one at a time in case a move
breaks something. (Ideally it would be called right after any
`django.contrib.auth` `login` or `logout` call; previously, we were
constrained by having to make the call after a response object had been
created.) These changes can be made later if it becomes necessary.
2022-01-25 16:03:54 +00:00
Robert Raposa
5d79972260 fix: safe session bug when request has no user (#29731)
* fix: safe session bug when request has no user

Fixes a bug during safe session monitoring when
request has no user.

ARCHBOM-1940

* fixup! add comment and loosen if condition
2022-01-07 19:19:59 -05:00
Robert Raposa
8fa1d4d0b6 fix: conditionally set safe session monitoring (#29724)
Fixes a bug where a custom attribute was being set
even for cases where we did not have the appropriate
data, and an exception was being raised.

ARCHBOM-1940
2022-01-06 16:49:58 -05:00
Robert Raposa
973fb3c4c6 fix: enhance safe session setting documentation (#29723)
Adds note to documentation around the requirement
for setting SAFE_SESSIONS_DEBUG_PUBLIC_KEY to log
encrypted headers.

ARCHBOM-1940
2022-01-06 13:44:53 -05:00
Robert Raposa
d0259ff38e fix: enhance safe session monitoring (#29681)
* add custom attribute for list of user ids on mismatch.
* log request header for all mismatched users for all
requests for N seconds after the mismatch is found, if
LOG_REQUEST_USER_CHANGE_HEADERS is enabled. See toggle
docs for more details.

ARCHBOM-1940
2022-01-06 12:15:31 -05:00
Tim McCormack
9f9e0acc55 feat: Destroy session and send back an error if user identity changes (#28456)
A new feature toggle, default off, causes the session to be deleted when
the user identity on the response does not match the session or request.

There are a small number of requests that cause the user present on the
session at the time of the request to be a different user by the time of
the response. As far as I can tell, these are all cases where a user's
browser somehow ends up with a mix of cookies from multiple legitimate
login sessions on different accounts on the same device.

Because there no longer seems to be any case where this mismatch occurs
and where the response should be allowed through, this commit introduces
a feature toggle `ENFORCE_SAFE_SESSIONS` which will destroy the active
session and overwrite the response.

The plan is to make this behavior available in the next named release and
permanent in the one after.

Also:

- Use less fragile method of checking mocked set_attribute calls in tests
2021-12-09 19:23:22 +00:00
Jawayria
7663592aa6 chore: Applied lint-amnesty on openedx/core/djangoapps 2021-12-09 13:37:27 +05:00
Tim McCormack
02b9e059a2 feat: Remove monitoring for now-concluded verify-all work (#29495)
The removed attributes were needed in order to inform the move of the
`_verify_user` function call up out of the try/except block. That work has
concluded (https://github.com/edx/edx-platform/pull/29324) so the
monitoring can be removed.

Also:

- Bring a comment on some other monitoring up to date
- Make long-needed corrections to an existing docstring
- Remove malformed-cookie logging, since we haven't been using it
2021-12-02 18:45:48 +00:00
Tim McCormack
89699ed6a4 feat: Remove VERIFY_USER_CHANGE_UNCONDITIONAL toggle (now always enabled) (#29324)
We didn't see any errors after enabling this feature toggle, so remove it
in favor of the "True" setting.

Compare to PR #29306, which created the toggle.

ref: ARCHBOM-1952
2021-12-01 17:02:47 +00:00
Tim McCormack
0bef57591d feat: Add feature toggle to allow broader safe-sessions user checking (#29306)
Contingent on new feature toggle `VERIFY_USER_CHANGE_UNCONDITIONAL`, check
for request/response user mismatches on all requests, not just those
setting a session cookie on the response.

This is intended to *restore* an older behavior. I believe that almost all
requests used to set a new session cookie, and for some reason no longer
do, so this is really just an attempt to return to that previous behavior
no matter whether a new session cookie will be set. (Previously, the
cookie-to-be-deleted check would still have been in effect, so this is
actually a slight change from the earlier behavior -- the logout response
will now be included, and then quickly ignored due to a later check.)

The off-by-default switch moves several lines of code out of a try block,
but also out from under an if guard that checks for certain cookie
conditions. The movement out of the try block should be irrelevant, since
neither of the relocated lines should be raising a SafeCookieError.
However, there is some chance that they could raise other exceptions when
called from their new location (and new situations), hence the use of a
feature toggle -- we'll want to make it easy to switch the new behavior off
quickly if we start seeing an increase in errors.

Once the change is well-exercised, we can remove the toggle and the old
call locations.

I'm not entirely sure about the change to the `verify_error` utility
function in the unit tests, but it seems like even unauthenticated requests
in Django end up with a user and session on the request object, so this is
probably a close-enough way to mock that out.

I duplicated a couple of tests to test with feature toggle on/off.

ref: ARCHBOM-1952
2021-11-15 15:28:59 +00:00
Tim McCormack
8a764cca10 refactor: Safer, more readable user-mismatch detection code in safe-sessions (#29226)
- Add early exit for readability. Less indentation here may make the control flow easier to read.
- Wrap debug info generation in error-suppressing try-except block.

Co-authored-by: Robert Raposa <rraposa@edx.org>
2021-11-08 16:13:39 +00:00
Tim McCormack
fdd27a1f71 feat: Log session ID from various sources after safe-session mismatch (#29185)
Add logging in case a safe-session user mismatch is related to wrong
session being retrieved from cache. This additional logging should
reveal any such mismatch (without revealing the actual session ID in
logs).

Send to metrics as custom attributes as well.

Also:

- Compute "session_id_changed" based on all three session IDs (and
  send as custom attribute)
- Put all _verify_user logs into one (multiline) log line
- Accordingly, change logging assertion to only require a substring,
  at-least-once match rather than a full-and-only match.

ref: ARCHBOM-1939
2021-11-04 19:34:44 +00:00
Tim McCormack
18c273f150 feat: Add debug info to safe-sessions: Multiple cookies, response cookies (#29168)
Also:

- Normalize response of a helper function to always be a boolean
- Make unit test accepting of unrelated custom attr calls

ref: ARCHBOM-1939, ARCHBOM-1941
2021-10-29 17:11:23 +00:00
Manjinder Singh
0ba74be102 docs: improve docs (#29158) 2021-10-29 10:58:26 -04:00
Robert Raposa
91ac846e71 fix: safe session code cleanup (#29139)
1. Minor code change to get user_id more consistently.
2. Remove outdated comments.
2021-10-28 10:06:50 -04:00
M. Zulqarnain
834dabb399 feat: New codemods on OpenedX 2 (#28777) 2021-10-28 16:45:53 +05:00
Manjinder Singh
bd7653aefc feat: removing toggle (#29132)
The toggle was meant to be temporary and its been off for week and
nothing seems to have gone wrong.
2021-10-27 13:31:25 -04:00
Manjinder Singh
ef135fba5a feat: only redirect to login for top-level page navigation requests (#29054)
Commit modifies safe session middleware to return an 401 in case of authentication failure and lack of 'text/html' in Accept header.

Previously, the middleware would always redirect to login in case of auth failure, but this was deemed inappropriate for any requests that are not top-level page navigation requests(we check this by seeming if 'text/html' is precent in Accept header)

Co-authored-by: Robert Raposa <rraposa@edx.org>
2021-10-21 08:45:52 -04:00
Rebecca Graber
835666a0bf fix: do not log error for sessions where user is changed to None (#29036) 2021-10-15 13:55:26 -04:00
Rebecca Graber
2200b5382e feat: only log requests that fail user verification in SafeSessionMiddleware (#29028)
* feat: only log requests that fail user verification in SafeSessionMiddleware
ARCHBOM-1923
2021-10-15 10:52:13 -04:00
Tim McCormack
fe3d855986 feat: Don't warn about expected user changes in safe-sessions (#28983)
This is intended to silence a rare false positive that seems to happen
when someone logs in on a browser that already has an active session
for another user. We believe there should be no further positives once
this case is handled.

- login and logout views annotate the response to indicate the session
  user should be changing between the request and response phases
- safe-sessions middleware skips the verify-user check when this
  annotation is present

Also:

- Adds a test around existing behavior for unexpected user-changes
- Remove logging control based on `is_from_log_out`. This reverts most
  of af9e26f/PR #11479 for two reasons:
  - The safe-sessions `_verify_user` code has since changed to check for
    `request.user.id == None`
  - A commit later in the PR changes the login and logout pages to
    signal that the user/session change is expected
2021-10-13 15:53:16 +00:00
Awais Qureshi
1f12ab186c chore: Django3 has removed python_2_unicode_compatible.
Execute the codemodes.
2021-07-29 18:22:22 +05:00
Tim McCormack
503e3d1d37 test: Fix safe_sessions comments and add pinning test (#28249)
Add pinning test for SafeCookieData values, and update SafeSessions
middleware comments to match code.

Main comment changes:

- Fix description of cookie structure:
    - Specify hash algorithm (SHA256, not "H")
    - Don't try to describe internals of TimestampSigner; description was
      incorrect in several ways: Did not include string delimiters under
      base64 (there's JSON in there); did not include the actual MAC
      portion. Just describe general effect and shape of output.
    - Add missing trailing pipe delimiter in signed data hash input
- Use phrase "intermediate key" rather than the less familiar term "usage
  key"
2021-07-22 21:09:32 +00:00
Usama Sadiq
46993bff00 refactor: pyupgrade second iteration (#27461) 2021-05-11 11:24:07 +05:00
Feanil Patel
455033458c feat!: Replace logging WaffleSwitch with a django settinge.
This was initially introduced as a temporary flag to be able to get more
information.  But if we get this kind of issue again, we'll need
something like this logging to determine the source of the session
collision.  Rather than removing the code and adding it back in later,
convert this temporary switch into an opt-in setting that can be used
again in the future.

BREAKING_CHANGE: 'safe_session.log_request_user_changes' switch no
longer exists and is replaced with the 'LOG_REQUEST_USER_CHANGES' django
setting which defaults to 'False'
2021-04-02 14:20:07 -04:00