Skip to content

Sécurité: Empêcher un utilisateur d’accéder aux droits des prescripteurs habilités s’il effectue des démarches depuis une organisation non habilitée [GEN-2332]#5887

Merged
tonial merged 10 commits into
masterfrom
alaurent/authorized_prescribers
Apr 10, 2025
Merged

Sécurité: Empêcher un utilisateur d’accéder aux droits des prescripteurs habilités s’il effectue des démarches depuis une organisation non habilitée [GEN-2332]#5887
tonial merged 10 commits into
masterfrom
alaurent/authorized_prescribers

Conversation

@tonial

@tonial tonial commented Apr 4, 2025

Copy link
Copy Markdown
Contributor

🤔 Pourquoi ?

⚠️ Attention : il y a deux propositions dans cette PR : avec ou sans le dernier commit

  1. request.user_is_authorized_prescriber (propriété sur l'objet request)
  2. request.user.is_authorized_prescriber (propriété sur l'objet User)

J'attends qu'on ai choisi la solution retenue pour adapter les tests

Pour mieux prendre en compte les permissions des utilisateurs

J'ai mis toute l'équipe en relecteurs parce que c'est une property qu'on utilise à pas mal d'endroit et qui disparait.
À présent :

  • soit c'est l'utilisateur de la requête et on peut dire s'il est actuellement sur une organisation habilitée
  • soit c'est un utilisateur récupéré en base de donnée, et on ne peut que dire s'il est membre d'une organisation habilitée

J'ai trouvé plus simple de mettre la propriété sur le user pour éviter de passer l'objet request (notamment sur les @check_user) mais c'est ouvert à discussion : je pourrait mettre un request.user_is_authorized_prescriber à la place
On pourrait ajouter un post_generation sur la factory pour définir automatiquement is_authorized_prescriber plutôt que de le faire à la main à chaque fois, mais j'ai peut qu'on se retrouve à créer des bugs car on ne se rendrait pas compte qu'une fonction aura en argument un objet qui n'y a pas accès normalement.

🍰 Comment ?

Décrivez en quelques mots la solution retenue et mise en oeuvre, les difficultés ou problèmes rencontrés. Attirez l'attention sur les décisions d'architecture ou de conception importantes.

🚨 À vérifier

  • Mettre à jour le CHANGELOG_breaking_changes.md ?
  • Ajouter l'étiquette « Bug » ?

🏝️ Comment tester ?

Les instructions pour reproduire le problème, les profils de test, le parcours spécifique à utiliser, etc. Si vous disposez d'une recette jetable, mettre l'URL pour tester dans cette partie.

💻 Captures d'écran

@tonial tonial self-assigned this Apr 4, 2025
@tonial tonial added the modifié Modifié dans le changelog. label Apr 4, 2025

@xavfernandez xavfernandez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ça fait plaisir toutes ces requêtes en moins 👍

Comment thread itou/templates/apply/submit/application/resume.html
Comment thread itou/utils/perms/middleware.py Outdated
@tonial tonial force-pushed the alaurent/authorized_prescribers branch from a7206ee to 8755d53 Compare April 7, 2025 10:06
Comment on lines +157 to +159
# Only authorized prescribers may create a GEIQ diagnosis, so if the author is a prescriber
# he was authorized when he created it
return geiq_allowance_amount(self.author.is_prescriber, self.administrative_criteria.all())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personnellement je suis pas super fan de simplifier un comportement en se basant sur des suppositions, si un jour elles changent alors tout devient faux et il y a peu de chance qu'on revienne sur ce bout de code, surtout si rien ne casse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette property est actuellement fausse: si self.author.is_prescriber_with_authorized_org is False mais que self.author.is_prescriber is True alors ça veut dire qu'il était PH à l'époque du diag, et donc que geiq_allowance_amount() == 1400
Il ne faut donc pas regarder is_prescriber_with_authorized_org qui donne une vision aujourd'hui, et pas à l'époque.

La bonne approche est de stocker dans un champ le montant de l'aide, mais @xavfernandez a dit que ce n'était pas prioritaire, et qu'on pouvait faire comme ça en attendant.
Ça ne me dérange pas d'ajouter le montant sur l'objet, c'est assez rapide à faire dans une PR séparée.

Comment thread itou/templates/job_seekers_views/step_check_job_seeker_nir.html Outdated
Comment thread itou/templates/layout/_header_help_dropdown_content.html Outdated
Comment thread itou/utils/auth.py Outdated
Comment thread itou/utils/perms/middleware.py Outdated
Comment thread itou/utils/perms/utils.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pas directement lié aux changements de la PR, mais maintenant que ça utilise request on pourrais tout à fait faire des template filter et ainsi réduire un peu le boilerplate dans certaines vues.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonne idée !

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bon, je suis en train de regarder pour passer can_(view|edit)_personal_information en template filters, sauf que dans les template, on a des {{ job_application.job_seeker.get_full_name|mask_unless:can_view_personal_information }} ce qui fait qu'on doit mettre des {% with ... %} autour et c'est pas franchement mieux selon moi.
Je vais donc laisser de côté pour l'instant.

Comment thread itou/www/eligibility_views/forms.py Outdated
Comment thread tests/users/test_models.py
Comment thread tests/www/eligibility_views/test_forms.py

@vincentporte vincentporte left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bravo pour l'investigation Antoine !
Je comprends que si nous mettons la propriété sur User, nous devrions à chaque fois restester si user == request.user. Donc plutôt option 1.
Je comprends moins bien la question que tu soulèves sur les check_users, car la méthode reçoit déjà request.user.

@tonial

tonial commented Apr 8, 2025

Copy link
Copy Markdown
Contributor Author

Suite à la réunion on part sur request.from_authorized_prescriber

@tonial tonial force-pushed the alaurent/authorized_prescribers branch 4 times, most recently from 47b7cce to 1e86735 Compare April 8, 2025 14:24
Comment thread itou/approvals/models.py
job_application = JobApplicationFactory(job_seeker__with_mocked_address=True)
job_seeker = job_application.job_seeker
# Ensure sender cannot update job seeker infos
assert not job_seeker.can_edit_personal_information(job_application.sender)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi avoir supprimé ce test ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parce qu'on ne peut plus appeler ce code, et que c'était redondant avec la suite du test (le post ne fait rien)

@celine-m-s celine-m-s left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toutes ces requêtes en moins, ça fait bien plaisir !!! 🤩
J'ai tout relu mais je ne sais pas si tu as fini. N'hésite pas à redemander une relecture sinon.

@xavfernandez xavfernandez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'aurais bien vu de nouveaux checks du nouvel attribut dans les tests de TestItouCurrentOrganizationMiddleware

@tonial tonial force-pushed the alaurent/authorized_prescribers branch from 1e86735 to 3eb4dd2 Compare April 10, 2025 07:19
@tonial tonial requested a review from xavfernandez April 10, 2025 07:26

@xavfernandez xavfernandez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Les deux derniers commits sont à inverser car beaucoup de tests échouent logiquement après le renommage de la méthode en is_prescriber_with_authorized_org_memberships alors que is_prescriber_with_authorized_org est encore bien utilisé.

Comment thread tests/www/eligibility_views/test_forms.py Outdated
Comment thread tests/www/eligibility_views/test_forms.py Outdated
Comment thread tests/www/eligibility_views/test_forms.py Outdated
Comment thread itou/utils/auth.py
Comment thread itou/utils/perms/middleware.py Outdated
@tonial tonial force-pushed the alaurent/authorized_prescribers branch from 3eb4dd2 to 2a2ef7e Compare April 10, 2025 12:25
@tonial tonial enabled auto-merge April 10, 2025 12:30
@tonial tonial force-pushed the alaurent/authorized_prescribers branch from 2a2ef7e to 31e69b8 Compare April 10, 2025 12:47
tonial added 8 commits April 10, 2025 14:55
Don't pass the user when we only need to know if he's an
authorized_prescriber
We will soon need the request instead of the user to know if he's an
authorized prescriber
We will soon need the request instead of the user to know if he's an
authorized prescriber
We will soon need an attribute on request to know if the user is
currently in an authorized organization
This doesn't tell us if he's acting as an authorized prescriber, just
that he is the member of at least one authorized organization
@tonial tonial force-pushed the alaurent/authorized_prescribers branch from 31e69b8 to fd995fc Compare April 10, 2025 12:55
@tonial tonial added this pull request to the merge queue Apr 10, 2025
Merged via the queue into master with commit 0a275b7 Apr 10, 2025
@tonial tonial deleted the alaurent/authorized_prescribers branch April 10, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

modifié Modifié dans le changelog.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants