Oh, I Found a Security Issue
Published October 26, 2026
This video features Gergo Simonyi at DjangoCon Europe 2026 in Athens, Greece.
TALK: Role-based access control in Django - How we forked Guardian by Gergo Simonyi
Django's built-in access control system is very good for basic operations and Guardian is a natural extension to the object level. However, our customers wanted more: a group hierarchy, just-in-time privileged access, delegating permissions to other users, custom permissions. This talk tells the story of how an authentication company built a role-based authorization system for Django.
More information: https://2026.djangocon.eu/speakers/TDJ7A8/
Follow DjangoCon Europe on social media:
*X - https://x.com/DjangoConEurope/
*LinkedIn - https://www.linkedin.com/company/djangocon-europe/
*Mastodon - https://fosstodon.org/@djangoconeurope
#django #djangocon #djangoconeurope #djangoconeurope2026
Automatically transcribed, so expect mistakes in names and technical terms.
Speaker 1: Next two talks, and we will wait for it a bit to quiet down and for everyone to find a seat. In the meantime, I hope you're all ready for lunch and had a great evening yesterday. We are taking timing not that serious apparently. That's okay. But in the meantime, I'll start with the introduction. Um With great pleasure, we have um Greg from Authentic here. Hi. And he is giving us a talk about role based access control in Django, which um for me Perfectly shows how Django evolved over time and with its its community and ecosystem. Because some years ago
Speaker 1: I did introduce the basic object-level permissions in Django Django Guardian builds upon that and now Greg explores it way deeper than anyone else before, I guess. Let's have a warm welcome and give him a round of applause.
Speaker 2: Hello. Come in, come in, come in. So this is role-based access control in Django or how we fork RDM. I am this person, Gergo Shimoni, but Greg is probably uh better for most of you. But also I'm Hungarian and in Hungary we have surnames first and given names last. So if you ever see a commit with this, that's me. Uh for some reason. I cannot hear, sorry. Congratulations with the elections. Oh the elections, yes. Uh political. But I will smile. So uh here's my company, authentic, uh and here
Speaker 2: and here. Uh They sponsor this, so I have to talk about them for one minute. We are an open course self-hosted identity provider. So basically your customers log in through us and we make everything very simple if you have massive systems with I don't know, dozens of integrations. And it's good. It's good for your home lab as well if you wanna uh do some homeless stuff. It's comparable to Key Cloak, if anyone has heard of that as well. We are also hiring specifically a back-end engineer with security background, and that's all I'm gonna say about Authentic. These are the slides. That's the code. I will not be showing code, but basically this is our fork of Guardian. You can see in the URL it's aka slash
Speaker 2: aka-Guardian. Uh yeah, and the slides if you want to go at your pace. Cool. Show of hands, please. How many of you have dealt with authorization in Django? Okay, many but not all. And with Guardian as well? Fewer. Oh, actually, uh so few. I I thought uh I thought that would be a bigger number. Interesting. Cool. That's fine, that's fine Uh role-based access. Has anyone ever done role-based access? Okay. Many and attribute-based access control. Also also many. Uh there are two more left. There are there are not not that many more. A directed graph is a mathematical concept. Anyone has heard about it?
Speaker 2: Nice. And the transitive closure of the directed graph. We will not be exploring that much, but uh one data structure of this talk is a transitive closure of a directed graph. And if you can guess it, you are I don't know. I'll I'll I'll get you a beer or something. Uh so access control. What is access control? Uh access control within this talk is authorization and there's an asterisk there because sometimes it's not but for the purposes of this talk whenever I say access control I mean authorization and vice versa just substitute in your head uh whichever you feel comfortable with. And it's role-based, so roles are gonna be important somehow. We'll see. Authorization in Django.
Speaker 2: So there were many hands up for this, so I will kinda speed through this. Hmm, cool. Uh yeah, basically this is from Django Docs if you have app foo and model bar. Then you are getting these four auto-generated permissions. And you can check for them with user. hesperm. And you can see these four are the cruds. So create three that we delete in a different order. I guess it's alphabetical. That's okay. Django Docs are their own thing. Uh and what does it do? What does it do? Question. Uh answer. It checks uh if the user is active. And is the super user?
Speaker 2: And if so, then it returns true. Because if a user is active on the super user, that user will have, should have access to everything. And then there is this block below, which is uh yeah, there's a try and accept, there's an if without an else, there's uh fallbacks with false. So we'll we'll dive a bit deeper in that in a second. But first I want to take a look at the inner function, the backend. hesperm. So this the outer function is user. hesperm, and and the inner function is backend. hesperm. And what is a backend? Uh well In Django, there are multiple concepts of backends. This here is an authentication backend. So that's slightly different from a database backend, but an authentication backend
Speaker 2: can also use uh a database backend if needed. Anyway, so backend has perm what does that do? It's very, very simple. It creates a set Of all the permissions that are related somehow to the user. So the first line of the set is all the user permissions that are directly related to the user model. Yeah, it's a many to many between user and permissions. If there is an entry in the join table, then it will come here. And the second is Given a user, uh take all of the groups of that user and then take all of the permissions of those groups. So this is kind of a One, two, many two, even even more. I don't know if there's a uh
Speaker 2: good word for that. So anyway, aggregate that If the per we're interested permission we're interested in is in that, then the user has the permission. So yeah, we you can basically You can basically have two paths for a user to have a certain permission, directly or indirectly through the groups. And the sharp-eyed among you, or my host, may have noticed that there is the object here, which is nowhere used in the function. And we shall see what that's about in a bit. But that's interesting, no? Yeah, so we built this uh mind mind uh model I guess of models, model
Speaker 2: of models. Of users, groups and permissions, and all of them have many to many connections between them. Yeah. And as I said, user to permission, if there's a user object with a corresponding user permission, then that user is assumed to have that permission. For example, let's say change underscore book is a nice example. If the user has that, then he can change books. He can change any book. And same for the other way around. Cool. So let's get back to the other hesperm. This is the user. hesperm, the outer one that we talked talked about before. And yeah, there's an if inside that we just talked about, the backend. hesperm. If that returns true, then
Speaker 2: the user has the permission. Nice. If that throws an exception, uh, or raises, I'm sorry, this is Python, uh, if that raises an exception of permission denied, that means it it just shouts at the system, no, don't let this guy in. And then we return false. And there's a third uh path here, uh which is kind of hidden hidden here. It's like the missing else statement for the if Uh and so basically what this means is that if that particular authentication backend doesn't know whether the user had permission, it can just return a false evalue and then the loop will go to the next backend. And the next and the next and the next. And if none of the backends uh
Speaker 2: can uh if none of the backends know about this user and this permission, then we just shut the user out just in case. So fail failing safe, I guess Uh yeah. And what's this? What is a backend? Well for authentication backends, Django can have any number of authentication backends, backend one, backend two, backend three, but Good luck, uh good uh good fortune guys, because Django by default only has one of these backends. It is called model backend, and we will see why it's called model backend in a minute Uh yeah, what's what's a backend made of? For authorization purposes, a backend is made of these six uh
Speaker 2: functions or member functions I guess. And and there's like uh the hesperm that we talked about before. Uh And kind of above that there are the getters. You can see that there's user permission getter, group permission getter, and all permissions is just the collection of those. And then the bottom two we will not be talking about, but they are also important, but not relevant to this talk. Uh cool. And there you can see the object again. And in Django 's uh I think it's called base backend. No, maybe whatever. Or or model backend, yeah. Uh these are not used anywhere. So now you know about authorization in Django. And now we will go to authorization in Guardian.
Speaker 2: And so what's Guardian? Guardian is, this is from the Guardian website. An implementation of object permissions for Django, providing an extra authentication backend. So we know what the extra authentication backend means. It means that You will now have instead of one backend, the default, two backends. Yeah? And what's object permissions? Well, we have uh basically two kinds of permissions at this point. From Django, we have the model permissions. If I have the change book Model permission, then I can change any book. And this is represented by a tuple.
Speaker 2: The change book. And the user that has that permission. And then with Guardian, we are introduced to object permissions, where I can specifically change this one book. uh and that provides a finer level of detail. Uh so this is also represented by a tuple, but it has three uh constituents. I don't know how to say this. three uh parts of the tuple. First two are the same and then the object, the magical object that has always been none for Django, because Django doesn't do anything with it. Now suddenly uh it has it has a book there. That's a good book, guys, if you haven't read
Speaker 2: Dune, just go read it. Uh Yeah, and in in in the Guardian uh docs you have this slide, or well, this part, I guess, where it says Go and put the object permission backend, the the guardian backend basically into your authentication backends and that's that's the way it will work. So Basically we'll have model backend and object provision backend. It's quite a mouthful, so uh I will be uh referring to it as object backend And so now that we have Django, now that we have Guardian, we can ask the question, can I change this specific object? And Django will go, hmm, I will go to the model backend. If it gives back yes, then yes.
Speaker 2: If it gives back an error, shouts an error basically, then Very much no. If it provides a falsity value, then go to the next. Yeah, this is basically what it does behind the behind the scenes. It will ask user has perm and then just uh the permission itself or or action, I guess. Uh yeah. And then it goes to the object backend. If it has to go to the object backend, it will go to the object backend and it will basically behind the scenes ask about this specific book. Can I change this specific book? And object backend again has these three possibilities. Yes is yes. Error is shouting no and falsy value is just falling through. And if neither of them gave an answer, then
Speaker 2: I'm I'm uh given a false value just in case. Cool. So now we understand this. Yes? Good. Now we understand why Django didn't do anything with this. Nice. And now uh yeah more slides, more slides, whatever. Uh Can we see what the guardians object backend. hesperm does? And yes, technically, but from your from where you're sitting, probably no. Because yeah, you can you can kind of see, you can kind of squint and see that the first half of these or the first part of these are comments, but I kind of wanted to show all three of these functions and there are actually more Hasperm functions within Guardian that serve a different purpose, but I'll not get into those.
Speaker 2: So anyway, these are much more complex than then Django 's. And we will be going up in complexity very soon. So we had this mental model, and now we have to somehow add to our mental model the object permissions. And we do it from two sides. There are gonna be user object permissions and group object uh group object permissions. So there are now four kind of sources of permissions I guess. You can be a user that's directly connected to a permission or to an object permission or to a group and through the group the permission or to a group and through the group a group object permission And what I'm not showing here is uh in these bottom tables, these arrow tables, in these bottom and
Speaker 2: models, in these bottom tables, uh There's there's gonna be a generic foreign key to the specific object that you are granting permission to. Uh yeah, it just didn't fit the slide, so I skipped it. Cool. This is reasonable, right? This is this is easy. This is manageable. And we can live happily ever after. Unless enterprise comes in. Because enterprise, man, I don't know if you've dealt with enterprise, but show of hands, please. How many of you had like big enterprise customers? Okay. For the rest of you, I I would Highly recommend it doing once. And then never again. It's like climbing Mount Fuji. You you know this saying it's like uh a wise man climbs Mount Fuji once, the fool climbs it twice.
Speaker 2: Uh yeah, but enterprise is is a different beast, man. And and yeah, what does enterprise want? Always control, always control. And so uh I'll not even explain these well maybe maybe kind of explain these, but but these are just some requests that my company got from enterprise customers. And unfortunately because It's our livelihood, we can't easily say no. And so therefore we were kind of forced into doing at least something about this Uh and so yeah, okay, I'll I'll we have a bit of time, so I'll say just a few words on this. So just in time privileged access is like When you're going to
Speaker 2: the resource, you don't have the permission, you request the permission right when you are trying to access the resource or changing the resource or deleting. And based on some conditions, you either get it or not. So this is like, I don't know, if you have notion and somebody uh hid a page from you, then you can request access to it. Sure, delegating permissions is basically as long as I have these permissions, this person that I'm delegating to also has these permissions. But when I lose them, I expect them to lose that lose it too. And custom permissions is basically we had four of those, the four four base permissions, I guess, that Django generates for any given model. They were the add, change, view, delete, I guess, create read update delete.
Speaker 2: And yeah, people wanted more. They wanted to copy books or they wanted to, I don't know, add an admin user specifically, not just a not just a user. Uh yeah, they wanted group hierarchy and group hierarchy is gonna be my favorite because group hierarchy basically means get a group and let them have a parent And if if you know where this is going, then then you know, and if you don't, then be prepared. Yeah, and and from that hierarchy. Enterprise said, yeah, we we kinda want to inherit the permissions. We want the groups that are the parent groups. to give the their child groups their permissions.
Speaker 2: And if they have another child group, like a grandparent group, should also give their grand grandchild group their permissions. So that's nice. And therefore, we got to work, right? We saw this. We added something called roll, which was very good for uh for the uh for the just in time uh access because with groups before you couldn't really remove people from groups but with roles this is a new thing uh or this was a new thing at the time you can just Create a role that stays there for I don't know a day or so. Sure, that's nice. But one of the downsides of
Speaker 2: Introducing the role is that now group object object permission has to go to group, and then that has to go to role, and then that has to go to permissions. So we are adding one layer of abstraction to this part of the puzzle Okay, and then these were introduced kind of why? Well these are these are not really uh so I think every uh arrow so far was a foreign key. These are not foreign keys, these are just uh In in the mind of the developer, they have to be included. So we had the copybook or create author instead of create user custom permissions. And then we added this. You see that up there? This is the group hierarchy. Basically, the group has now a foreign key to itself.
Speaker 2: Cool. And that is a problem because when a group has a parent, that parent will have a parent. That parent will have another parent. And if you're dealing with enterprise, you can go up quite a lot of steps. And sometimes you just get back to where you're started from. And that's a problem, guys, because that means you have infinite recursion in your queries and it's It's not too difficult to read out, but it's kind of difficult to weed out. And we we we like didn't want that there. Like that's that's not cool. Okay, anyway. What else? Oh yeah, now we also have two groups. Because Guardian
Speaker 2: doesn't know about the hierarchy. Guardian only knows about the Django group or whatever group we give it, but it doesn't know about the hierarchy, the group hierarchy. So if you want to inherit From the grandparent group to the child group, then we have to go from group object permission to Django group to roll to group and the Mac to roll to permission. And that is a large SQL query. And so at this time you are stopping right you you you're you're feeling the heat like this is unmaintainable easily uh and we probably should have stopped beforehand but you know it kind of kept working like Why why why stop now? But then then then something broke and yeah, we we just have to stop. Obviously not working. Let's go back to the drawing board.
Speaker 2: What can we do with this? And so so this is where kind of the transformational steps begin and I'm very sorry that I cannot share with share this in with you in detail uh because of lack of time. But if you go to the uh to the code uh Q QR code uh Yeah, the code URL that I provided at the beginning or and at the end I will also provide this. You can you will be able to see how we did this uh in practice. But for now, high level, we removed the custom permissions, at least removed any custom permissions that were Like too fancy, I guess. We had to keep some, unfortunately. But like so those are those are gone. That's nice. And now Admins can
Speaker 2: actually create their own custom permissions. So they are not in the code anymore, they are in the database And that's good because we don't have to think about them, but that's also very bad because now we have to check every time the permission table is uh altered whether or not an admin accidentally just uh removed or edited some something that they shouldn't have. So yeah, it it could be done. That's nice. Next we somehow, somehow deleted this Django group, and this was a big effort. Uh and from now on we only have our own groups, which is nice. Which is amazing because if you have two models that are the same name in your code base, you can see how that could lead to confusion, right?
Speaker 2: You open a shell, you type group. object. all, and then get nothing or get too many things or God forbid you press uh after that dot delete and then you're deleting from the wrong uh model. Yeah, that's a problem. But now, yeah, we have to add this because if we remove Django group, somehow we still need to keep the group object permission. Alive, I guess. And now we can actually kind of rename it role object permission. And at this point, we are so so deep into the journey and it's so painful. That that we've at this point have probably trialed like twelve different authorization frameworks or authorization like uh
Speaker 2: I don't know structures And so immediately when we went to this, we were like, okay, if we somehow delete this user object permission, uh then we will be left with A thing that is called role-based access. So we're coming to the uh I don't know the key point of the of the talk. Once you remove this, and I'll make it a bit clearer, you have you have this this thing which is it's it's actually maybe manageable. And I'll I'll I'll do the same thing, but I'll uh I'll build it up one by one. So we have the group, right? That has to be like the core of the functionality
Speaker 2: because it has the hierarchy. And the hierarchy is deadly if you if you don't care about it. And so we add a cache to this in the database that is seamless so far and knock on wood where some would give me Let's hope let's hope it remains seamless because it's a mess. You can look at the code. It will have user. Nice. And they will have role. Amazing. And that role will have role object permission, and that role object permission will link to the permission. Cool. And now for any user uh we can again go through whether the user has the permission that we're asking for. So I'm asking uh user has uh dot has permission
Speaker 2: uh change book it will go on one side to the role directly users can have roles and then to the object per role object permission and then put permission And that's manageable SQL, right? Or it can go to group uh and it has a cache uh that gives you all the descendant groups in one go and that's amazing because then we can just go to group role roll object permissions permission and that's nice that's amazing that's cool I I love that And you will see that I left some space on the right for a role model permission. Now this used to be Django 's uh thing, right? Django did the model permissions, Guardian did the object permissions. But at this point
Speaker 2: we are like okay let's just make it complete let's kick all of the authentication backends out of authorization let's just do this that's amazing that's cool So how did we actually do this? Easily. We forked Guardian. You can see the code if you want. It was an effort over I think three months, but it was it was nice. I removed so many Guardian functions. I I don't think I don't think Guardian had that many functions to remove So we have this. That's nice. And now, for the grand reveal, the SQL that you have all been waiting for, which is gonna be
Speaker 2: The SQL that does a single query that does everything that uh all at once uh queries User to role to object permission and model permission to permission and on the other side to the groups and the same from there. And it's this. It's nice. I I stole uh whoever did this uh yesterday, uh stole from him. So thank you for that. That's nice. And but guys, it's it's fine. It's fine. It's fine. I know. It's it's not that good. But it's actually not that bad. It's actually not that Because Here's the thing. You can put every line of this SQL that you saw
Speaker 2: onto this diagram and it will be fine. Like if you have groups, you have you have this much SQL for that. Uh and then you have the group hierarchy and you have this much. And you have the cache and you have this much again. You have okay, this is not the user, this is the user and group uh connection. Then you have group to roll User to roll and these two at the bottom. So it's kind of fine. And it benchmarks amazingly. Uh yeah, these are these are all the relevant lines in the SQL query. Yeah, this is what it looks like. Nice. And you can if if you if you squint the right way, you can kind of see it. Like I can see that here Here we have uh user to group and here we have group to group.
Speaker 2: Here we have all the stuff at the bottom and and like here we have just users themselves with the role So it's kind of fine. Uh yeah, and as I said, it benchmarked well. We did like a small benchmark uh with 10,000 groups, 10,000 like hierarchy nodes, which is like randomly selected uh groups form uh a different hierarchy. Yeah, we did this a couple of times and always we had Very, very fast calculation. So that's nice. That's good enough. And I think I'm out of time. So thank you for your attention See the slides? See the code.
Speaker 2: We just went through one hundred and sixty-eight slides, by the way. So thank you for your attention.
Speaker 1: Thank you for the great talk. We will do something shortly different because we have a question from online and we will bring that in. The question is, how is Django Guardian working if the backend is added at the end of the list? Isn't the Django model backend already returning true because it just ignores the object?
Speaker 2: Sometimes uh so when you have the model permission, uh I don't know where to uh there to the camera. Hi. When you have the model permission uh in the database for that user, then yes. the Django uh backend, the Django Django's default backend, the model backend, will return true. When uh you don't Then it will just return a falsy value, and that falsy value signals to the uh to the function, to the has uh perm function that it it should go to the next backend I hope it answers the question.
Speaker 1: I guess so. Um we have time for one short question. Short.
Speaker 2: That's okay. Ten minutes, Max.
Speaker 3: Super super talk, thank you. Can you just quickly clarify about the role object permission? Because you did it looked to me as if you must have not been using model backend at all. Is that correct?
Speaker 2: Sorry. So role role object permission or role model permission?
Speaker 3: Yeah, sorry, role sorry. Role model position.
Speaker 2: Role model permission. So the role model permission is also in the forked guardian's backend now. So it is it is in the backend. But do you drop model backend? Sorry?
Speaker 3: Do you drop model backend? Do you take that out from the authentication backends
Speaker 2: Well, no, but we overwrite the authorization functions with just returning empty. So basically yes.
Speaker 1: Okay, thank you.
April 15 - 17, 2026
Athens, Greece
Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026
Published October 26, 2026