Latest / Talk Python To Me / #485: Secure coding for Python with SheHacksPurple
Transcript
- 0:00What do developers need to know about AppSec and building secure software?
- 0:03We have Tanya Janca, aka SheHacksPurple, on the show to tell us all about it.
- 0:09We talk about what developers should expect from threat modeling sessions,
- 0:13as well as concrete tips for securing your apps and services.
- 0:17This is Talk Python To Me, recorded November 15th, 2024.
- 0:21Are you ready for your host, Tanya?
- 0:24You're listening to Michael Kennedy on Talk Python To Me.
- 0:28Live from Portland, Oregon, and this segment was made with Python.
- 0:31Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:37This is your host, Michael Kennedy.
- 0:40Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 0:45both accounts over at fosstodon.org, and keep up with the show and listen to over nine years
- 0:51of episodes at talkpython.fm.
- 0:53If you want to be part of our live episodes, you can find the live streams over on YouTube.
- 0:57Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified about upcoming shows.
- 1:04This episode is sponsored by Posit Connect from the makers of Shiny.
- 1:08Publish, share, and deploy all of your data projects that you're creating using Python.
- 1:13Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, Reports, Dashboards, and APIs.
- 1:19Posit Connect supports all of them.
- 1:22Try Posit Connect for free by going to talkpython.fm/Posit.
- 1:27P-O-S-I-T.
- 1:28And this episode is brought to you by Bluehost.
- 1:30Do you need a website fast?
- 1:32Get Bluehost.
- 1:33Their AI builds your WordPress site in minutes, and their built-in tools optimize your growth.
- 1:38Don't wait.
- 1:39Visit talkpython.fm/bluehost to get started.
- 1:43Tanya, welcome to Talk Python.
- 1:46I mean, it's awesome to have you here.
- 1:47Oh my gosh, I'm so excited to be here.
- 1:49Thank you for having me, Michael.
- 1:50Yeah, you're very welcome.
- 1:52And I'm super excited.
- 1:55You know, in a good and bad way, excited to have you here because I'm very excited to talk to you.
- 2:00But some of the stuff that we're going to talk about might be a little unnerving for people out there listening.
- 2:04They may pause the show and then run off to make some changes to what they're doing and then come back.
- 2:10And then that part might be a little exciting in a different way.
- 2:13Yes, it will.
- 2:16Have you ever had that experience where you're giving a presentation or something and somebody goes gasp in the audience and maybe runs away or something?
- 2:24I was doing a capture the flag contest once and I was showing people how to do an SQL injection.
- 2:30And then we just like logged in without a password.
- 2:32And this woman was like, oh my gosh.
- 2:35And she literally just she stood up.
- 2:38She's like, I have to go.
- 2:39And she told me the next day she fixed three of them at work.
- 2:43And she just stayed all night.
- 2:45Oh, amazing.
- 2:46You made a huge impression.
- 2:48It was pretty cool.
- 2:50Yeah, that's pretty awesome.
- 2:52I've never had that around security, but I was doing an in-person class once for database things, which we're going to touch on databases for sure.
- 2:59And this had to do with transactions.
- 3:00And I said, if you do things this way, it doesn't actually use a transaction unless, oh, yes, it does.
- 3:07And I showed that it did.
- 3:08And somebody said, I'll be back later.
- 3:10And they just took off.
- 3:11Oh my gosh, this is not good.
- 3:13We have to fix this now.
- 3:15So, yeah, hopefully no one actually has to do that here.
- 3:18But I'm sure there's a ton for people to learn.
- 3:21Speaking of learning, let's start with a little bit about you.
- 3:25You have a domain, shehackspurple.ca.
- 3:29That tells us several things about you, I believe.
- 3:31Purple is interesting.
- 3:33Hacks is interesting.
- 3:36And Canada.
- 3:36Tell us about yourself.
- 3:38So I was a software developer for a really long time.
- 3:42And then I switched to security.
- 3:45And when I was a software developer, I used to also play music in bars and music festivals as sort of my hobby.
- 3:51But I released albums and did all of that.
- 3:54So that's why I'm quite the public speaker, because I've been on stage my whole life.
- 3:58And so I switched to security.
- 4:01And I became a pen tester, which is Red Team.
- 4:04And that's where you attack things.
- 4:06And it's a fence of security.
- 4:08And I don't mean we swear at people.
- 4:09But then...
- 4:12It's the other way around, I believe.
- 4:14This is the direction the swearing probably goes.
- 4:16Right?
- 4:16But then as I was doing pen testing, I kept sitting with the devs.
- 4:22And I would pair a program with them.
- 4:23And I would help them fix things.
- 4:25And I'm like, well, if we threat model this...
- 4:26And I kept doing AppSec, essentially.
- 4:28And people are like, you keep doing blue team.
- 4:31You keep doing defenses.
- 4:32It's like you can't make up your mind.
- 4:34And eventually one of them is like, you're a purple team.
- 4:37And so I was at a conference in Europe.
- 4:39And on stage during the conference, this woman, she kept playing with her phone on a panel, ignoring the audience.
- 4:47And I was super shocked.
- 4:48And finally, she's like, I'm really sorry.
- 4:50But her company had created WannaCry by accident.
- 4:54Yeah.
- 4:55And it had just broken out.
- 4:56And she's like, so there's this virus.
- 4:59And it just hit the NHS and took down the hospitals and this and that.
- 5:04And oh, my gosh.
- 5:06And so everyone ran out of the room freaking out.
- 5:08And they all went to check Twitter.
- 5:10Because that's where everyone was talking.
- 5:11And so people are like, you have to make a Twitter account.
- 5:14And so I was like, oh, what will I call myself?
- 5:16And I was like, she acts computers.
- 5:18And then it was too long.
- 5:20Because that was my email address.
- 5:21She acts computers at gmail.com.
- 5:23That used to be my email.
- 5:24But then basically, that was too long.
- 5:28So I was like, I don't know.
- 5:29And someone's like, well, you know, you have that purple team thing going.
- 5:32So I was like, well, it's not like anyone will ever follow me.
- 5:35So I changed it into She Hacks Purple.
- 5:37And then it turns out that I really, so then I put some purple in my hair to tease my friend
- 5:43Kevin.
- 5:43And it went all from there.
- 5:46Like, yeah.
- 5:49I own a lot of purple now.
- 5:50That's amazing.
- 5:50Yeah.
- 5:51It's now part of the vibe, right?
- 5:53That's awesome.
- 5:54That's a really cool story.
- 5:55So you said you've been a programmer for a while and then got into app security and pen
- 6:01testing.
- 6:01How did you get into all that?
- 6:02It was totally an accident.
- 6:05So like I said, I was in a band.
- 6:07And so we had this pen tester in our office and he was in a band.
- 6:10And so we obviously became friends.
- 6:13And one day I came to his desk and I was like, hey, my band wrote this song called Mandatory
- 6:17Dance Party.
- 6:18And we want to make this mobile app where if you're near someone else that has the app,
- 6:21it goes beep, beep, beep.
- 6:23Mandatory Dance Party.
- 6:24And then you both have to dance or else.
- 6:26And then whoever jiggles their phone the most wins.
- 6:29I'm like, did you want to make this app with me?
- 6:30He's like, there's nothing I want to do more.
- 6:33And so our friendship began.
- 6:35And then for a year and a half, he just nagged me to become a pen tester.
- 6:39He's like, you'd be so good.
- 6:40You'd be really good at it.
- 6:42You've been doing you've been a dev for like 17 years.
- 6:45It's time for something new.
- 6:46And I was like, no, I am the king of software.
- 6:49This is the best.
- 6:50I make something out of nothing all day.
- 6:53It'll never be cooler than this.
- 6:55But then it turned out it was pretty cool.
- 6:57And you still get to write code sometimes.
- 7:00And then eventually I figured out I was not meant to be a pen tester.
- 7:03I was meant to do app sec.
- 7:04So I still get to hang out with devs all day.
- 7:06I'm not alone freezing cold in a data center at night.
- 7:10And I get to it's like more like a social butterfly type of job where a pen tester is more like
- 7:16the lone wolf type of job.
- 7:17Yeah.
- 7:17Yeah.
- 7:18You're working on the disassembly, looking for, you know, fuzzing something, looking for a crash.
- 7:23Yeah.
- 7:24So you still get to break stuff in app sec sometimes, but like, that's not all you do.
- 7:29You do a lot of conversations, a lot of brainstorming, and that's better for my extroverted personality.
- 7:35Yeah.
- 7:37That's awesome.
- 7:37You have a newsletter.
- 7:38People can visit your website and sign up and I'll put links to all those sorts of things
- 7:42in the show notes for you.
- 7:45So yeah, a couple of things I wanted to talk about.
- 7:49First of all, I've seen you given presentations about threat modeling and tell us what is threat
- 7:55modeling and what are some of the takeaways?
- 7:57Obviously our audience here is largely Python developers, data scientists, and a ginormous assorted
- 8:05other that sort of orbits around those spaces.
- 8:08So they'd probably be pretty interested on the developer data scientist side of threat modeling.
- 8:13What is it and what's it like for devs?
- 8:16So threat modeling is sort of like evil brainstorming.
- 8:22So you get a security person like me in the room, you get a dev or one of the technical leads
- 8:28for your system, and then you get the product owner.
- 8:30So the person that understands the business of this app and like why it exists in the world.
- 8:35And at least those three people.
- 8:37If more people come, it's better, but that's fine.
- 8:40And then you talk about what could go wrong and what are you going to do about it?
- 8:45I have this friend named Adam Shostak who's written a bunch of books about it.
- 8:48And so I used to ask a ton of questions and then I met him.
- 8:52And now I ask four things.
- 8:54What are we doing?
- 8:55And so then I usually draw it out on a whiteboard.
- 8:58So like, oh, there's an API and it talks to this and then this happens and then people steal bikes.
- 9:03Okay.
- 9:04And then it's like-
- 9:06Do we have a database?
- 9:06Is it in the cloud?
- 9:07Is it in a data center that we own?
- 9:09Whatever, yeah.
- 9:10Yeah.
- 9:10And then I ask questions along the way and then I'm like, so what could go wrong here?
- 9:15And then it's like, you know, these two things are talking.
- 9:17Do we authenticate first or do we just talk to any old API?
- 9:21Right?
- 9:22And we go through and we come up with some things that could go wrong and we make a list.
- 9:26And then I'm like, okay, so what are we going to do about these things?
- 9:30And you talk about basically, is this a serious risk?
- 9:34Is it scary?
- 9:35Or is it like, you know what, if that happens, it's not really a big deal and the likelihood is really, really rare.
- 9:40So maybe we'll accept that risk.
- 9:42But a lot of them, it's like, you know, if we added a certificate here or we included authentication there, like we can make some small changes.
- 9:51And if you're during the design phase of the system development lifecycle, it costs you nothing, right?
- 9:56Or usually it costs you nothing or very little.
- 10:00And so then you basically improve your design and then you write it all out.
- 10:05And then hopefully someone approves these changes.
- 10:09And then at the end, a thing that Adam asks is, did we do a good job?
- 10:14And that is the magical question.
- 10:16Because the very first time I did one of these in the government, I did not realize that that's what I was doing to my director.
- 10:25I was like, well, what about this?
- 10:27And what about that?
- 10:27And this could go wrong.
- 10:28And because I was trying to kill the project because I thought it was a terrible part.
- 10:32And we did end up canceling it.
- 10:33But I brought up like all these huge existential risks to this ridiculous project they were thinking of doing.
- 10:39And he just kept saying, it'll be fine.
- 10:42And I'll manage that risk.
- 10:43And I was like, you realize manage that risk means nothing, right?
- 10:47You're going to do nothing.
- 10:48So I brought up eight really big risks.
- 10:50And you want to do nothing about any of them.
- 10:52I'm like, are we doing it?
- 10:53Like, do you think we've done a good job here?
- 10:54Like, you feel proud today?
- 10:55Because I don't.
- 10:56And then we started again.
- 10:58And then eventually over the months, we canceled the project because it was quite silly.
- 11:03Just to be clear, I usually love it when software development projects go forward.
- 11:08This was a very special situation where it was not in the taxpayer's best interest.
- 11:13Yes.
- 11:15Anyway, I'm like, there's more there.
- 11:16But I'm like, NDAs.
- 11:17Yeah, of course.
- 11:19But usually we come up with like a couple of changes that grossly reduce the risk of the system.
- 11:26And it's quite fun.
- 11:29Like, once you start.
- 11:32So the first time I went to a threat model, I came with my dev background of, I will fix any problem you bring to me.
- 11:38I just need to do code.
- 11:40And I can fix whatever you need.
- 11:42But it's different when it's like, I'm going to try to break things.
- 11:45You have to learn kind of this new skill set of like, how can I make it do stuff it should not do?
- 11:50And it took me a while.
- 11:52And now I'm brutal.
- 11:53Like, I go to the movies.
- 11:55I'm like, we could have gotten free.
- 11:58It's even outside of technology now, huh?
- 12:00Oh, yeah.
- 12:02Oh, it's everywhere now.
- 12:03My significant other who does not work in tech will be like, I'm just trying to threat model this for the kids.
- 12:10If we go to the pool, they'll have wet bathing suits.
- 12:13Yeah.
- 12:15Yeah.
- 12:15Very nice.
- 12:16You know, I think there's two different ways to look at this, right?
- 12:20Obviously, from a developer perspective, you've got to look at, well, what packages or libraries am I using?
- 12:27And how are we validating input and all those kinds of things?
- 12:31And I feel like that's one level, but maybe the threat modeling is a little bit broader.
- 12:35Like, are we storing stuff unencrypted anywhere?
- 12:38Yeah.
- 12:39Who could get that?
- 12:40And it's the thing that makes me nervous.
- 12:43I said I would be somewhat nervous about this at the beginning.
- 12:45The stuff that makes me nervous about all of these things is the asymmetry of it.
- 12:49As a developer or data scientist or somebody in charge of this information and the systems, you have to be right all the time, right?
- 12:57And if you're ever not right, little Bobby Tables is on you like nothing.
- 13:00It's like a goalie.
- 13:02It's like a goalie, right?
- 13:04Like, you let into goals and everyone's like, you're the worst.
- 13:06And it's like, I defended against hundreds of shots.
- 13:09Do I get nothing?
- 13:11Exactly.
- 13:12Well, developers shouldn't be on their own in this, though.
- 13:16That's the whole thing.
- 13:17So, like, the job of the AppSec person, in my opinion, is to support the developers in making more secure apps, right?
- 13:25So, booking a threat modeling time with them or, you know, providing a static analysis tool or a secret scanner or whatever to scan their code so that they can make better code.
- 13:36So, they have help giving them training, giving them, like, a list of super clear requirements at the beginning of the project rather than at the end telling them it's wrong.
- 13:48That is a huge one.
- 13:50Like, if someone's going to build an API, I would prefer the API be behind an API gateway if it's publicly accessible.
- 13:57And I want to do things like turn on authentication and authorization and rate limiting and all sorts of fancy, nice, awesome stuff that they offer.
- 14:05And so, I tell them at the very beginning of the project, I don't wait until the end and I'm like, well, this design's all wrong.
- 14:13Like, if, you know what I mean?
- 14:14Yeah.
- 14:14But that's what we did back in the day.
- 14:16Like, when I had my first vulnerability assessment done, I, you know, I'm supposed to go to prod in two days.
- 14:24And they ran the world's crappiest dynamic scanner on my computer, on my app.
- 14:28And they're like, you have cross-site scripting.
- 14:31And this was so long ago.
- 14:32I searched for cross-site scripting on the internet and there was only three web pages.
- 14:36So, I'm old, everyone.
- 14:38And one was this thing called OWASP.
- 14:43And I was like, what the heck is that?
- 14:45And it took me quite a while to figure out how to fix it, right?
- 14:50Like, we've come a long way, but yeah, the developer shouldn't be alone in this anymore.
- 14:56They should have help.
- 14:57I'm not saying that every company does have that.
- 15:00I'm saying every company should.
- 15:01If you have a bunch of devs, you should have eventually a security person that supports them.
- 15:06Right.
- 15:06Maybe sits with them for a few hours a week or a month or something.
- 15:11And sort of, as you said, like a butterfly sort of moves around the team, hanging out with other people.
- 15:15Like, oh, you work on the API.
- 15:16Okay, let me hang out with you a bit this morning.
- 15:19Ideally, that would be great.
- 15:20Also, I mean, if you have a big enough company doing something like a security champions program.
- 15:26So, like each dev team, there's one representative and you just give them way more training and way more time.
- 15:33And you check in with them regularly.
- 15:35Like, if I'm working with only like 50 or 60 devs, like you can get to know a lot of them.
- 15:41But I remember working at this one place and there were 2000 devs and me.
- 15:45And I was like, okay, so how do I do this?
- 15:52And just very quickly, there was just like that one person per team that I would talk to all the time.
- 15:57And I remember, ironically, I went to Montreal on this vacation trip and my car got stuck in the mud because there'd been this big rainstorm.
- 16:06And I was like tweeting about it and how I was so sad.
- 16:08And one of my devs came to me and dragged me out of the mud.
- 16:12And I was like, he is a champion.
- 16:15I know.
- 16:15Oh, that's amazing.
- 16:16And there's benefits to large teams aren't in there.
- 16:19I know.
- 16:21I like, it never occurred to me that like I would get an answer.
- 16:24He's like, hey, I live in Montreal.
- 16:26Do you need help?
- 16:27And I was like, I do.
- 16:28And he's like, you help me all the time.
- 16:30I'll be right there.
- 16:30I was just like, oh, that's very cool.
- 16:33Yeah.
- 16:33This portion of Talk Python To Me is brought to you by Posit, the makers of Shiny, formerly RStudio, and especially Shiny for Python.
- 16:43Let me ask you a question.
- 16:45Are you building awesome things?
- 16:46Of course you are.
- 16:47You're a developer or a data scientist.
- 16:49That's what we do.
- 16:50And you should check out Posit Connect.
- 16:52Posit Connect is a way for you to publish, share, and deploy all the data products that you're building using Python.
- 16:59People ask me the same question all the time.
- 17:02Michael, I have some cool data science project or notebook that I built.
- 17:05How do I share it with my users, stakeholders, teammates?
- 17:08Do I need to learn FastAPI or Flask or maybe Vue or React.js?
- 17:13Hold on now.
- 17:15Those are cool technologies, and I'm sure you'd benefit from them.
- 17:17But maybe stay focused on the data project.
- 17:19Let Posit Connect handle that side of things.
- 17:22With Posit Connect, you can rapidly and securely deploy the things you build in Python.
- 17:27Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, ports, dashboards, and APIs.
- 17:34Posit Connect supports all of them.
- 17:35And Posit Connect comes with all the bells and whistles to satisfy IT and other enterprise requirements.
- 17:41Make deployment the easiest step in your workflow with Posit Connect.
- 17:45For a limited time, you can try Posit Connect for free for three months by going to talkpython.fm/posit.
- 17:52That's talkpython.fm/P-O-S-I-T.
- 17:55The link is in your podcast player show notes.
- 17:57Thank you to the team at Posit for supporting Talk Python.
- 18:02So you managed it almost, I don't want to endorse Scrum necessarily, but like a Scrum of Scrum equivalent.
- 18:08So sort of you found somebody or different people from who could sort of represent different segments or apps and stuff and got together with those groups.
- 18:16Yeah.
- 18:16Yeah.
- 18:17So I didn't know what it was called when I started doing AppSec because I didn't have any training, right?
- 18:24I was just like, all of our apps are a total mess.
- 18:27I just switched to the security team.
- 18:29I need to fix this.
- 18:31And so I was trying to talk to all of them, but very quickly, there was like a person that sort of self-identified as the person who was totally willing to tolerate Tanya and all of her security nerdiness.
- 18:42And I would hold these little lunch and learns where I was like, oh my gosh, my first one.
- 18:47I remember I, because I'd been on the dev team and then I switched to the security team and I was like, I'm going to break into a bank at lunch.
- 18:53Who wants to watch?
- 18:54I brought donuts.
- 18:56And everyone's like, you're not going to.
- 18:57And I'm like, it's a, it's a pretend bank, but I'm going to do it.
- 19:01And they're like, but donuts, right?
- 19:03I'm like, yeah.
- 19:03You just got to choose the right motivation.
- 19:08I feel like pizza, any sort of carb goes really like it goes over well.
- 19:15But yeah, I just slowly formed more and more relationships.
- 19:19And then that is how I got a lot of security done because I'm not their boss.
- 19:24I can't make them prioritize security.
- 19:27I need to persuade them that it's important.
- 19:30And one of those people from my very first program, he became an application security engineer and he just started his first security champions program this year.
- 19:39And I spoke for them two months ago and it was amazing.
- 19:42Exciting.
- 19:42Yeah, that's awesome.
- 19:44Honestly, I think coming from the software dev side probably gets you a lot of credibility with the software teams.
- 19:51Yeah, because I can just like read code and stuff and the rest of the security team usually can't.
- 19:58And also, so I've gotten in trouble for that, though.
- 20:02I've gotten in trouble with the security team at a couple of places I've worked where they're like, you're always on their side.
- 20:07And I was like, yeah, but they're right and we're wrong this time.
- 20:11Because sometimes the security team's being so completely unflexible.
- 20:16And I'm like, listen, this is like a minute risk if you really think about it.
- 20:20Where like if you look at the context of this and this, it doesn't generate legitimate business risks.
- 20:25So, no, I'm not going to upgrade off of this vulnerable library because it's not actually exploitable.
- 20:30And there's this and this and this precaution.
- 20:33And like we're actually fine.
- 20:34And it's going to cost like months and months of time.
- 20:37And so I'd rather use my social currency on something that really matters.
- 20:40And I've just had people get really pissed at me on the security team.
- 20:44Like this plus between username and query.
- 20:47So, yeah, it's just not good.
- 20:53It's just not good.
- 20:54There is, you know, you see all the time, whether it's npm or pip or whatever package management tool you're using,
- 21:03you might see, oh, or GitHub.
- 21:06This library you're using has this vulnerability.
- 21:08And it sounds scary.
- 21:10But if the vulnerability is in a portion of it that you literally never use and you never expose the internet, you know, it might be bigger fish to fry.
- 21:18Who knows?
- 21:18Maybe not.
- 21:19But a lot of it's hard to decide.
- 21:21Insecurity, we call...
- 21:23As you're saying where to spend your time.
- 21:24Yeah, so we call that reachability in the InfoSec field.
- 21:28And so my advice, and not everyone agrees, is basically if something...
- 21:34So let's say you have the math library.
- 21:35And the math library has like a thousand different math functions because we love math.
- 21:40And one of them has a great big bug in it.
- 21:42But we're not calling that one.
- 21:44Right.
- 21:44And it's probably like a denial of service.
- 21:46If you give it this weird number, it'll overflow and then like loop forever.
- 21:50And we're never calling it.
- 21:51Yeah, maybe it's something like really bad.
- 21:55It's like remote code execution.
- 21:57Like this sucks.
- 21:58Okay.
- 21:58But if it's not reachable from within your code, it's probably not actually a risk.
- 22:03So let's say it's like the worst one.
- 22:05It's remote code execution.
- 22:06I'm like, listen, at some point, could you upgrade off of this?
- 22:09And I need you to keep scanning it with your software composition analysis tool every time you check your code in to make sure you are not calling that dangerous function.
- 22:18So if you switch it around and you are no going to prod, but as long as it's not reachable, then we're not causing legit business risk.
- 22:27But I do want it in the backlog because at some point I'd like you to upgrade off.
- 22:31But like, let's say it's a medium.
- 22:32It's like there's so many bugs that I want people to fix.
- 22:37And some of them aren't even bugs.
- 22:39Like some of them are, I want you to use content security policy header.
- 22:43Is that a bug that you're not using it?
- 22:45In my heart it is, but technically no.
- 22:48But it's an additional layer of security that basically stops cross-site scripting in its tracks.
- 22:53Right.
- 22:54And so I'd rather spend my social currency getting like the entire organization to adopt CSP than to fit like upgrade off of tons of different dependencies that aren't actually hurting anyone.
- 23:06Yeah, I agree.
- 23:07I think that makes a lot of sense.
- 23:08This is why security people yell at me.
- 23:12Yeah.
- 23:12Once you've solved all the other security problems, you can come back to the unreachable ones, right?
- 23:17Yeah, for sure.
- 23:18Yeah.
- 23:19I want to dive into your book and you've got a bunch of good recommendations.
- 23:22But before I do, I just have a quick question.
- 23:24What do you think, you know, the White House last year or beginning of this year released a thing saying we call for memory safe languages.
- 23:31And I know you started in C and C++.
- 23:34I also started C++.
- 23:36I was kicked a Fortran kicking and screaming for a little while and went back to it and then moved on.
- 23:40But, you know, we got a lot more options these days, right?
- 23:44And I know folks at the PSF were actually working with the people at the White House to encourage them to consider Python as one of the options.
- 23:53But what do you think about this and its implications?
- 23:55Oh, a lot of software is written in C.
- 23:58A lot.
- 23:59Like, maybe half.
- 24:01Like, it's nuts how much is written of our whole world is written in C.
- 24:06And they're like, oh, future software.
- 24:09So, if we're writing brand new software, yeah, I wouldn't write it in C unless I absolutely had to.
- 24:16I would try to use Rust.
- 24:18But do I think that everyone's going to suddenly, like, rewrite everything into Rust?
- 24:23No, I don't believe that.
- 24:25And it's because of a lot of reasons.
- 24:27One, like, I'm told it's difficult to develop in Rust because basically there's, like, no libraries for it compared to C.
- 24:34C is so rich.
- 24:35There's so many options in C, C++, right?
- 24:38Because it's been around forever.
- 24:40There's a zillion code samples that you can copy from and then paste into your code, which you should not do unless you understand it fully.
- 24:48Yeah.
- 24:49So many comments there.
- 24:51So, memory safety.
- 24:53Like, if you are going to write a new app, I want it to be memory safe.
- 24:57Yes, absolutely.
- 24:58Do I expect everyone to rewrite all the old code?
- 25:01No, no one's going to do that.
- 25:02No one can afford to do that.
- 25:03No.
- 25:04But I'd love to see, like, a framework over top of C and C++ that provided memory safety.
- 25:10That would be amazing.
- 25:12I'd pay for that, right?
- 25:14Like, a library that just, like, I collect your garbage and you don't have to think about it anymore.
- 25:19Right?
- 25:20Like, that would be beautiful.
- 25:22That would solve, that would do a lot of backwards compatibility if we started turning that on in my brain.
- 25:27Yeah, you talked about the remote code execution issues.
- 25:31A lot of that has to do with exceeding a buffer we've allocated, using a freed buffer before the pointer was gone.
- 25:38Like, a lot of it has to do with this memory ownership and stuff that you're talking about.
- 25:42Yeah, and sometimes mismanagement of objects as well.
- 25:45And so, basically, you know, you make a memory.
- 25:48Can I explain this?
- 25:49Or is this, like, way below?
- 25:51Okay.
- 25:51So, we can overflow an integer, a string, a buffer.
- 25:56But basically, like, you declare a variable of some form.
- 26:00And let's say you're like, oh, my string's 20.
- 26:02Cool.
- 26:03If you put 25 in there, where do you think that extra 5 goes?
- 26:06Right?
- 26:07Next bit of execution, probably.
- 26:09Yeah, it goes somewhere else.
- 26:10Or a heap or something, yeah.
- 26:11Yeah, it goes somewhere else in the stack or the heap.
- 26:13And guess what?
- 26:15If you happen to do enough of it, you'll find the stack pointer.
- 26:18And guess what the stack pointer does?
- 26:20It tells you where the next instruction is.
- 26:22And what if I tell it where the next instruction is and it's in my overflow?
- 26:27And what if I've added my own shell code with instructions to do bad stuff, like open a web prompt?
- 26:34I would like a shell, please.
- 26:35A shell would be nice.
- 26:37Thank you.
- 26:38And then you can execute code on their server remotely, which is the RCE worst in the world thing we do not want to have happen.
- 26:46And this is because of memory safety.
- 26:49Because it's not automatically checking the bounds for us.
- 26:53And because we ourselves have not done perfect input validation, which is a hard thing to get right.
- 26:59I was teaching it today.
- 27:00And literally, we spent one hour and 15 minutes just on input validation.
- 27:06And they had a trillion questions.
- 27:08And a lot of them were like, yeah, but we don't need to do input validation if it's just internal, right?
- 27:14And I'm like, are you handling the employee paychecks?
- 27:17Do you want me to see your employee paychecks?
- 27:20Then you probably need to validate.
- 27:22So I'm a slippery fish.
- 27:24Yeah.
- 27:26Are you reading from the database that somebody else could have gotten into and leveled up?
- 27:31Yeah.
- 27:31There is a lot there, Michael.
- 27:32There's so much.
- 27:33It's not good.
- 27:35It's a negotiation, though, right?
- 27:37And it's about persuasion.
- 27:38And it's about what their threat model looks like.
- 27:40Because if you're handling hundreds of millions of dollars a day, your threat model is very different than I used to work at a place.
- 27:47And their entire job was to show videos to nurses and doctors that they had to watch each month so they could continue their certification.
- 27:54And it was like, did they see the video or did they not see it?
- 27:57Threat model low.
- 27:58Yeah.
- 27:59You don't want people to mess it up and pollute it or whatever or take it down.
- 28:04But at the same time, it's not going to make the front page of the news.
- 28:08Exactly.
- 28:09Exactly.
- 28:10We now know.
- 28:11Everyone we know that Nurse 7725 has not been up to date.
- 28:16I mean, it's not great, but it's not the same as social security numbers and all that.
- 28:22Exactly.
- 28:23Yeah.
- 28:24All right.
- 28:24Let's talk about your book.
- 28:25I think your book is really good.
- 28:27Yes.
- 28:28Now, to be clear, specifically the Alice and Bob Lawrence Cure Coding, because I haven't read the other book.
- 28:33But if it's in the same style, it seems to me, I'm sure it's also good.
- 28:36Tell us about your books.
- 28:37Thank you.
- 28:39So my new book's called Alice and Bob Lawrence Cure Coding.
- 28:43And I'm dyslexic.
- 28:45And I'm about to get diagnosed for ADHD, too.
- 28:48Because why not?
- 28:50They go so well together.
- 28:52Yay.
- 28:52And so when I read a textbook, I find it really hard.
- 28:57So I read a zillion books.
- 29:00Like, I love books.
- 29:01Sitting still is hard for me.
- 29:03Reading a textbook, I find really, really hard.
- 29:06I want that knowledge to be in my brain very badly.
- 29:08But sitting my butt still for eight hours is really difficult.
- 29:12And I found traditional textbooks really difficult.
- 29:15So I started blogging because someone double dog dared me when I worked at Microsoft.
- 29:21And what else could I do, right?
- 29:22There's no way out.
- 29:25They've done it.
- 29:25I know.
- 29:26There's nothing I could do.
- 29:28Brock, you faded me into a corner.
- 29:31But I just kept blogging and blogging.
- 29:33And people kept telling me I should write a book.
- 29:35And publishers started reaching out to me.
- 29:37I'm like, yeah, but my blog's very casual language.
- 29:39I use a lot of examples with Alice and Bob.
- 29:42Alice and Bob were used by mathematicians to explain cryptography to normal people.
- 29:48So Alice wants to tell Bob a secret.
- 29:50How does Bob know it was Alice?
- 29:52And so I just kept using them because we all use them.
- 29:55And so basically, Wiley approached me.
- 29:59And they're like, yeah, you can write the weirdest textbook in the whole world.
- 30:02I'm like, because Alice is going to date people.
- 30:04He's like, okay.
- 30:06So my first book was for AppSec engineers and people that want to work in application security
- 30:15because there was no book of how to do that.
- 30:17So I wrote a book for past me.
- 30:19And so then when I thought about Alice and Bob having a sequel, I was like, I want to write
- 30:24a book for really past me for when I was a software developer.
- 30:28And so I was like, what should I cover?
- 30:31And so I covered the 10 top programming languages and the eight most popular frameworks within
- 30:38reason.
- 30:39So like some frame, like it was hard to pick the frameworks because I was like, oh, I was
- 30:45thinking about that when I was reading.
- 30:46I'm like, oh yeah, these are, it's not so easy.
- 30:49It was hard.
- 30:49And so I asked a lot of my followers.
- 30:51And so you might disagree with me about the frameworks I chose, but I really liked .NET.
- 30:57So it was obviously going to be in there.
- 30:58Flask, obviously going to be in there.
- 31:00But I was like, should I put pandas in here?
- 31:03Or should I put, I put in jQuery and my advice is don't use jQuery.
- 31:09Yeah.
- 31:10But document ready was so good.
- 31:12Come on.
- 31:12And so, so it was hard to choose.
- 31:18And then I wanted to cover like all the different agnostic programming advice, because to be quite
- 31:25blunt, there's a lot of stuff like input validation that applies to every single language in the
- 31:30world and every framework.
- 31:31It just doesn't matter.
- 31:32And I don't care if some of them say they do some input validation for you.
- 31:36It's not enough.
- 31:36Trust me.
- 31:38And so I wanted, so like the first third of the book is just completely agnostic.
- 31:43And I've been giving secure coding training basically since before the first book.
- 31:47And I just keep refining it and refining it and improving and improving it.
- 31:52And I was like, well, I have a lot to say on this subject now.
- 31:56And so then I asked all my followers, like, what do you want to see in the book?
- 32:01And they added a bunch of things.
- 32:02Like, they're like, oh, I want to see this topic.
- 32:04I want to see that topic.
- 32:05So it got even bigger.
- 32:06But then the end of the book, the last third is the system development lifecycle, all the
- 32:13security steps, but from a developer's point of view.
- 32:16Because when I was a dev, it was like, why am I being subjected to this?
- 32:20Like, what's a threat model?
- 32:22I remember being in a meeting and this woman was like, you want to do a penetration test on
- 32:27me?
- 32:28And then she turned bright red and was like, I don't know if I should be in this meeting.
- 32:33I was like, no, no, no, no, no, no.
- 32:36She's like, I was talking.
- 32:39I'm not a doctor.
- 32:40This is going to be fun.
- 32:41I know.
- 32:41I know.
- 32:42I was like, your web app, your web app.
- 32:44She's like, you just used a lot of words that were uncomfortable.
- 32:47I'm like, I'm so sorry.
- 32:50And so it's like, what to expect when a penetration test happens or like in a threat model, like
- 32:55bring your awesome ideas of how you would hack your app.
- 32:59And like, this is maybe how much will be expected from you.
- 33:03And why we like, what all these tools are and what they do and how you might want to use
- 33:08them.
- 33:09Because I feel like sometimes we just, I've heard a lot of security teams say to me, well,
- 33:14they should know.
- 33:15I'm like, do you think if they knew they would have done that thing?
- 33:17No.
- 33:18Did you, did you tell them it explicitly?
- 33:20Well, I felt it was implied.
- 33:22Dude, that's not good enough.
- 33:24This portion of Talk Python To Me is brought to you by Bluehost.
- 33:29Got ideas, but no idea how to build a website?
- 33:32Get Bluehost.
- 33:33With their AI design tool, you can quickly generate a high quality, fast loading WordPress
- 33:39site instantly.
- 33:40Once you've nailed the look, just hit enter and your site goes live.
- 33:43It's really that simple.
- 33:44And it doesn't matter whether you're a hobbyist, entrepreneur, or just starting your side hustle.
- 33:49Bluehost has you covered with built-in marketing and e-commerce tools to help you grow and scale
- 33:55your website for the long haul.
- 33:56Since you're listening to my show, you probably know Python, but sometimes it's better to focus
- 34:00on what you're creating rather than a custom built website and add another month till you
- 34:05launch your idea.
- 34:06When you upgrade to Bluehost cloud, you get 100% uptime and 24 seven support to ensure your
- 34:12site stays online through heavy traffic.
- 34:15Bluehost really makes building your dream website easier than ever.
- 34:19So what's stopping you?
- 34:20You've already got the vision.
- 34:21Make it real.
- 34:22Visit talkpython.fm/bluehost right now and get started today.
- 34:27And thank you to Bluehost for supporting the show.
- 34:30Yeah, I totally agree with you.
- 34:31And there's a really, let me do a quick search.
- 34:33There's a really interesting fact, at least from the Python space.
- 34:37If you look at the latest survey from the PSF and the JetBrains, how long have you been programming?
- 34:44There's a little one somewhere.
- 34:45Yeah.
- 34:45How long have you been programming professionally?
- 34:4733% less than a year.
- 34:49And if you look at less than two years, that's 50, that's half of the people doing software just
- 34:57got started.
- 34:58They probably don't even get the little Bobby table jokes, you know?
- 35:01Yeah, I know.
- 35:02They need to read XKCD.
- 35:03Yes, I know.
- 35:05But, you know, seriously, they, how are they supposed to know?
- 35:08They're struggling to just figure out how does it compile?
- 35:10Where do I get a virtual environment?
- 35:12Why won't I import that thing?
- 35:14They're, they're just, they're not at the place where they're, they're polishing it and
- 35:18they're, they're protecting.
- 35:19They haven't had the experience of, oh, I put it on the internet.
- 35:21I was hacked in eight seconds.
- 35:22You know?
- 35:23It hurts, man.
- 35:25I remember, so the guy that became my mentor, he gave a talk for my dev team because I ran
- 35:32the community of practice where I work.
- 35:34Shocker, me being an extrovert wanting to run a community of practice.
- 35:38And so I invited him to come and talk and he took one of our apps and he was at the login
- 35:44screen and he's like, I'm going to break into your app without a password and it's going
- 35:48to take over a minute just because I'm talking.
- 35:50And then he just did an SQL injection and he just got in and I was like, what?
- 35:55No, no, no, no, no.
- 35:58And then he was like telling us.
- 36:00And then of course, like all the SQL codes going in my head and I'm like, oh my gosh,
- 36:05that is very bad.
- 36:06What if my name, what if my name was quote dash, no quote, semicolon, drop tables, semicolon,
- 36:14dash, dash.
- 36:14That's an, that's an interesting name, isn't it?
- 36:16Right.
- 36:17We all, we all have special names, but yeah.
- 36:20So, I for, I forgot when you asked about my book, part of why it is weird is I try to
- 36:27make it casual language.
- 36:29So it's really easy to understand.
- 36:30Yeah.
- 36:31And I try to get, honestly, I didn't, I didn't put that together, but I had that experience
- 36:35reading it.
- 36:35So I think you nailed it.
- 36:37Thank you.
- 36:37And I try to use like different ways of explaining the same thing, like with a story and then
- 36:43like the technical explanation.
- 36:44And then maybe, there's like a funny story from Alice and Bob, cause Alice will not
- 36:51put up with unethical dates with pen testers.
- 36:54And Bob really worships this really cool guy.
- 36:57and like seeing it, how it applies to people's real lives, I felt like hit home with
- 37:04a lot of people.
- 37:05and so, yeah, I just, I want, I feel like security can be really hard and I was like,
- 37:11how can I make it a lot easier for people?
- 37:14So that was my goal with the book.
- 37:15Both of them.
- 37:16Yeah.
- 37:16Yeah.
- 37:16Well, I think it's, I think it's really approachable.
- 37:18So I want to pull up a few quotes out of it that I thought we could sort of riff on that
- 37:22I think would be fun here.
- 37:24So you start out the book by talking about humans and how humans are implicitly trusting
- 37:32of each other in general, right?
- 37:34In general.
- 37:34But you know, that's why we have societies and groups rather than every time we see a
- 37:39person, we either run away or attack, you know, like that's just not how it works to
- 37:42be a person.
- 37:43Right.
- 37:44And that trust is not necessarily appropriately transferred to computer systems and communication
- 37:50systems and all.
- 37:51Right.
- 37:52So you gave some examples of implicit trust and you also gave a warning or an important
- 37:58news.
- 37:59Maybe tell us about this bit.
- 38:01So basically when we started designing things, like we didn't even have passwords at first.
- 38:06Like I remember in college, my sister telling her friend, my sister's so crazy.
- 38:11She has a password on her computer.
- 38:13Like who wants to log into your stupid computer?
- 38:16And she thought I was-
- 38:17What are you working at a bank?
- 38:18Come on.
- 38:19Right.
- 38:20And so now we all have passwords on our computers.
- 38:23And, but we design our systems the way that our society operates with implicit trust.
- 38:30Like just imagine like someone comes to your door with a package and they ring the doorbell,
- 38:35you open it.
- 38:36But in the animal kingdom, which I have watched a lot of nature documentaries because I have small
- 38:41children at home, panthers, if they see another panther, it's going down.
- 38:46They're going to fight or make a new baby panther or both.
- 38:50That is what happens in the animal kingdom.
- 38:53and so like they have no trust.
- 38:55Like some of them like try to kill each other after they try to make baby panthers.
- 38:59Like they're all over the place because they have no implicit trust.
- 39:02So you see them alone a lot.
- 39:04And so when we started designing networks, one of the things we would do is we would, first
- 39:11of all, a lot of networks in this world today are still flat, which means one firewall around
- 39:16the outside and that's it.
- 39:17So if you can get to anything in the network, you can get to everything in the network.
- 39:22And that is an implied trust.
- 39:23So then we came up with zoning, like the data, the databases are all in one zone and there's
- 39:28a firewall around that.
- 39:30And then we have like a public access zone.
- 39:32And then we have a deep militarized zone because we think we're badasses, et cetera.
- 39:35Right.
- 39:36And, but what happened is if you have an SQL injection, you've gotten behind the firewall and now you
- 39:42can get to every single database in the entire organization.
- 39:45You have hit the gold mind.
- 39:47Right.
- 39:48So that is bad.
- 39:49and then we came up with zero trust.
- 39:51The idea of everything is closed by default.
- 39:54And unless there's a business requirement, you don't open it.
- 39:59Right.
- 40:00So, let's say you have a database and an API and it has a front end and then you have
- 40:06a service account for those.
- 40:07So the service account only can talk to those three things.
- 40:11Oh, and it can talk to the secret management tool to get your secrets because you store your
- 40:15secrets in a correct place.
- 40:17You don't put those in source code and just check them in the GitHub.
- 40:19Please do not do that.
- 40:23I only do that when I'm trying to prove a point.
- 40:29But, but then ideally the API checks that who's calling it is its front end and not someone
- 40:35else.
- 40:36Right.
- 40:36And then it authenticates and authorizes to the database.
- 40:39And we have, and nothing else.
- 40:41No one else can call that API.
- 40:43Why would you be calling it unless you're a malicious actor or you're a tester?
- 40:47Right.
- 40:48So once the testing phase is over, we're in production, no one else should be talking
- 40:51to it.
- 40:52So you only accept connections from there.
- 40:54And if, if we do zero trust perfectly, it is amazing.
- 40:58But quite often, we have partial implementations because it's quite a lot of work to implement.
- 41:05And if you get it wrong, it can be painful.
- 41:08Yeah.
- 41:10Yeah.
- 41:11What do you think about, things like thinks to canaries and those types of things?
- 41:16Maybe tell me what that is real quick.
- 41:19Yeah.
- 41:20So, there's this guy named Harum Mia who goes on risky business all the time, which is
- 41:25a podcast.
- 41:26I like.
- 41:26It's a good podcast.
- 41:28Yeah.
- 41:28Patrick Gray and Alex.
- 41:30What's the guy's first name?
- 41:31It's not Alex.
- 41:32Is it?
- 41:32I thought the guy, like the, is it Nick, the guy that he chats with?
- 41:37I haven't listened to it in like a year.
- 41:39Yeah.
- 41:39I'm sorry.
- 41:39Last name below, but first name, forget it.
- 41:42Anyway, they, they do a good show.
- 41:43Yeah.
- 41:43So yeah, go on.
- 41:44They're really good.
- 41:45It is a fun show and they're so like catty.
- 41:48I love it.
- 41:49Like they make fun of everything.
- 41:50Nothing is sacred.
- 41:51It's so fun.
- 41:52Absolutely.
- 41:52But basically, things canary, the company that Harum Mina works for,
- 41:58which why I've heard so much about it.
- 42:00Cause he's always on the show.
- 42:01They make, basically like these things that go on your network.
- 42:05So for instance, it could be like a fake word file.
- 42:09it could be a, some sort of fake file somewhere on your network.
- 42:13And then you see if it gets stolen and shows up somewhere cause it calls home.
- 42:17And so people go, so imagine like you have a data breach and then you can see because it
- 42:25phones home or you search the internet all the time to look for that.
- 42:27And you see, Oh crap, that's there.
- 42:29I actually was on stack overflow today.
- 42:31as a viewer, cause I'm banned for life.
- 42:34That's a long story.
- 42:35I tried to answer all the SunGrap questions in one day and they didn't like it.
- 42:39I feel like they should give you an award, not a ban, but okay.
- 42:44I know it's a, I agree.
- 42:47Right.
- 42:47Some of my answers, they didn't like, like, don't suppress that result.
- 42:51You have cross-site scripting.
- 42:52Here's how you fix your code.
- 42:53They're like, you didn't answer his question.
- 42:54Down vote, down vote.
- 42:57But anyway, the internet can be harsh.
- 42:59What were you saying about this trust and well-meaning?
- 43:01I don't know.
- 43:01Yeah.
- 43:02But so, so basically like this guy was saying his website keeps getting scraped by all his
- 43:08competitors rather than them typing out the information himself.
- 43:11So he puts fake musical artists into his database and then he goes to other companies and he searches
- 43:18for those fake musical artists and then he sends them ceases and desists.
- 43:24And so like the idea of a canary is that like the canary in the coal mine, but basically
- 43:30someone steals it.
- 43:31And then if it can, I believe that some of them can call home, but basically if they use
- 43:36it somewhere, you can see it's yours.
- 43:38And then you're like, oh, we are in trouble.
- 43:41Yeah.
- 43:42It's an early, early alarm.
- 43:44You know, the canary in the coal mine sort of deal, right?
- 43:47I think that it's a cool idea, but I think that if the canary calls out to you, you're already
- 43:52pretty screwed, right?
- 43:54Yeah.
- 43:54It's pretty bad.
- 43:55I think that it's cool, but it wouldn't be the first security measure I would do.
- 43:59It would be like, I have an advanced program that's like good and I want it to be super
- 44:04great.
- 44:05Yeah.
- 44:06Yeah.
- 44:06Sounds good.
- 44:07All right.
- 44:07So you say, if you only learn one single thing from your book or this podcast episode,
- 44:11let's put it, let's adapt it.
- 44:12I hope it is this.
- 44:14Design every system with as little implied trust as possible.
- 44:17It's true.
- 44:18And I highlighted that in purple for you.
- 44:19How's that?
- 44:20It's true.
- 44:23We want to, so in my first book, I'm like, trust no one, not even your mom, because my
- 44:27mom accidentally sent me a virus one day and I opened it because it was from my mom.
- 44:31Oh no.
- 44:34And so you can't even trust your mom, even if your mom's a brilliant mathematician chemist,
- 44:39because she can still get a virus on her computer.
- 44:42Yeah.
- 44:43Because it turned out grandma sent it to her.
- 44:45Yeah.
- 44:46And she trusted grandma.
- 44:48Grandma's not even sophisticated enough to send a virus.
- 44:51Is she?
- 44:52I know.
- 44:52Well, it turned out she was.
- 44:54But anyway, so don't trust anyone.
- 44:57So when you get input to your app, to your API, wherever it comes from.
- 45:03So that can mean getting stuff out of your database.
- 45:05So unless it's a static table that you know for sure is trusted, you should be checking the
- 45:12stuff from the database.
- 45:13So let's say someone's like filling out a form and then you save it to the database.
- 45:16So you would want to validate those values.
- 45:18You save it to the database.
- 45:19So then let's say an API goes and get some of that data to go do stuff with it.
- 45:24I would validate those values again before I use that.
- 45:27And then they put it on a webpage and it has JavaScript angle brackets in it.
- 45:33Yes.
- 45:33Then I would output and code it before I put it out there.
- 45:36And I would have content security policy header and a bunch of other things.
- 45:39But I digress.
- 45:40But if we could not trust anything that we get and always validate that it is what we
- 45:47are expecting.
- 45:48And if it's not, we reject it.
- 45:49So we don't try to fix it.
- 45:51Is it what we're expecting?
- 45:52And so this can mean like, so let's say it's a date of birth.
- 45:56So is it first of all, is it a date?
- 45:58Is it in the past?
- 46:00Is it more than 150 years in the past?
- 46:03Because that's less likely.
- 46:04Is it in the format you're expecting?
- 46:08Those are some of the things that we could check.
- 46:10And if it's not any of, if any one of those things are wrong, reject and just say, hey,
- 46:15actually we're expecting this.
- 46:16But let's say you need a person's name.
- 46:19So I work with someone named Luke O'Malley.
- 46:21Well, he has a single quote in his last name, which is a special character if we are going
- 46:26to use an SQL database.
- 46:27So what am I expecting?
- 46:30I'm expecting letters, lower and uppercase.
- 46:32And I'm expecting a hyphen and or a single quote.
- 46:36All of those are on my yes approved list, my allow list.
- 46:40So I check it against my allow list, not a block list.
- 46:43Because a block list of bad characters, guess what's going to happen?
- 46:47Tanya's goes around it.
- 46:48Yep.
- 46:48Thanks.
- 46:49I'm in.
- 46:49Usually Unicode escape sequence forward or some random thing.
- 46:54Yes.
- 46:54There's a zillion ways around it.
- 46:56And like, I remember when I learned that, how sad I was for all my past apps.
- 47:02And so you use an approved list of good stuff and you accept the single character and you
- 47:09accept the hyphen and then you sanitize or escape them.
- 47:12So sanitize means changing it for a different value.
- 47:15So you might want to change the hyphen to a pipe and you might, or probably not a pipe,
- 47:20maybe that's still a special character.
- 47:21But like, let's say the carrot.
- 47:23And then you change the single quote to the tilde symbol.
- 47:27And then those are not a problem.
- 47:30And then you pass that on.
- 47:31So you validate that's what you're expecting.
- 47:33And then you have either escape, sanitize it or escape them.
- 47:37So just put like a backslash in front of the special characters.
- 47:40Or maybe HTML or URL encode them to that, you know, percent some numbers, which is the
- 47:46quote or I don't know, whatever, whatever it resolves to.
- 47:48It depends on what you want to do with it.
- 47:50So if you're going to take that and then put it into a parameter and send it to a
- 47:54parameterized query, maybe, maybe you want to escape it.
- 47:58It's going to escape it for you when it gets there.
- 48:01There's a lot of options, but single quotes are kind of the danger zone.
- 48:04So I want to be careful.
- 48:05They're definitely tricky.
- 48:06All right.
- 48:07Next section.
- 48:08This is the Python section.
- 48:11So you have, as you said, a bunch of different technologies and they're quite up to date.
- 48:17You've got Node.js.
- 48:18You've got .NET Core, not the old crusty Windows only .NET.
- 48:23You've got Python, Python 3 stuff.
- 48:25So I think that's great.
- 48:26So let's talk about some of the what to do.
- 48:30Maybe just pick a few off this list that jump out at you that you want to talk about.
- 48:34Okay.
- 48:35So some of them are really obvious.
- 48:37Like, please use Python 3.
- 48:38It's time to say goodbye to Python 2.
- 48:41I know that we can still love it in our hearts, but new apps need to be Python 3.
- 48:45And updating our environment often.
- 48:47This goes for every framework.
- 48:49One thing is, is if you find a real security bug, reporting it to the security folks from Python, that is a valuable thing to do.
- 48:58Whatever programming language or framework you're in, if you feel you found a real legit bug, you should support it or you should report it.
- 49:06Because as a result, when they fix it, they're fixing it for thousands and thousands and thousands of devs.
- 49:11And you are a wonderful human.
- 49:13So that is that.
- 49:15Pay it forward.
- 49:15Yeah.
- 49:16And I want to give a shout out to the PSF and Python folks.
- 49:20They've made a lot of efforts in putting more time and energy into the security at Python.
- 49:26Both Mike Fiedler got hired as the security person behind PyPI, the package index.
- 49:32And Seth Larson got hired for more broadly Python security.
- 49:37Hopefully I've characterized that right.
- 49:39But we now have two full-time people working on it.
- 49:41Whereas before it was kind of core devs and other people contributing their spare time.
- 49:45Hopefully they could grab it, you know?
- 49:47So that's good.
- 49:48And you point out that there's a security at python.org email address for legitimate reports and not hassling busy people.
- 49:57Yes.
- 49:57Don't hassle them.
- 49:58Test and make sure it's repeatable.
- 50:01Like these people are very busy.
- 50:03There are so many things.
- 50:06So one of the things was, so like let's say you're taking, because we're talking about user input.
- 50:10And we're taking, so the bottom of that page, we're taking user input as a string from your code.
- 50:16And we can use the template class from the string module rather than other functions for string manipulation.
- 50:23So if we do that, it's safer.
- 50:26There's less string overloads.
- 50:28Like if we avoid using, for instance, fstring and string format for handling user input, because that can be manipulated by the user.
- 50:36So it sounds weird, but like using the template class from the string module can't be manipulated as easily.
- 50:43Interesting.
- 50:44Yeah, I know.
- 50:46I had to do a lot of research for this.
- 50:47I bet.
- 50:48Yeah.
- 50:49And they just added a new type to the Python type system called a literal string, which works for SQL for things that are not meant to accept user input.
- 50:59So if you had a literal string, that was a query and you combined it with stuff that came from a regular string, like a user input.
- 51:06And then you would check it with a Python type checker.
- 51:11And then you would check it with a mypy type of checker.
- 51:14That is very cool.
- 51:16Yeah, yeah, yeah.
- 51:18That's pretty neat.
- 51:18There's not too many tools to support it, but the static type checkers do.
- 51:21Another one I think that's worth calling out here is you talked about, be sure you pin your dependencies.
- 51:26Yes.
- 51:28I'm like looking through all of the notes.
- 51:33You have them.
- 51:36Pinning.
- 51:37So I got into like a lot of arguments with my technical editors about this one.
- 51:42So you want to pin your dependencies, like as you're going through all the different environments and not allow it to update out when you get to prod, because otherwise you're all testing different versions.
- 51:54Right.
- 51:54So you want to make sure you're using the same one across the board that you're testing, because otherwise your tests aren't accurate.
- 51:59Right.
- 52:00Yeah.
- 52:01It might be the same, or it could be an important library got an update between when you checked it in and when it got built to a Docker container or something.
- 52:08And it's different.
- 52:09But the other thing is, is that you don't want it to be permanently like that forever.
- 52:15And so like when you're, so it sounds weird.
- 52:19So you pin it there, but then I can't remember where it is.
- 52:23I think the, I think the big distinction is, are you building an application or are you building a library?
- 52:29People are building applications with, because your application should pick its versions.
- 52:32But if you pick them concretely for the library, you're forcing potentially old vulnerable versions onto people.
- 52:39Right.
- 52:39There's this tension of, of what role am I playing, which I think is tricky.
- 52:43Yes.
- 52:44So ideally, like if you're like about to go to prod with something, you don't want it changing in different environments.
- 52:51That could be really bad.
- 52:52But yeah, like you said, pinning the version.
- 52:55Well, I'm just like reading it on the screen.
- 52:59We, so I remember like I was installed, I was doing like a proof of concept with this company.
- 53:04And in order to install it, it wanted me to downgrade a bunch of npm dependencies.
- 53:10And then it showed me that there were huge vulnerabilities in those dependencies that they'd asked me to use.
- 53:16And I was like, well, like deal breaker buddy.
- 53:18Right.
- 53:18And so when you are creating like a product for someone, if they can see that and how it's compiled, like that's a deal breaker for a lot of customers.
- 53:28Customers are very savvy in regarding security right now.
- 53:32And I love it.
- 53:33Yeah, it's really good.
- 53:34And even putting security aside, you can end up, if you have two libraries, they both use a sub library, a dependency, and they have different pin versions or ones less than or greater than or less than or equal to the other is greater than equal to.
- 53:47And there's no intersection of those numbers.
- 53:49You just, you're like, well, I can't run this.
- 53:51I guess we just can't use these because it'll say, I can't give you both greater than two and less than two at the same time.
- 53:57Right.
- 53:57It's a hassle.
- 53:58But the security bid is also important, obviously.
- 54:01I agree.
- 54:02Let's see.
- 54:02Yeah.
- 54:02So, bandit.
- 54:04Bandit's an interesting, interesting tool.
- 54:07Bandit is a free static analysis tool that specifically is made for Python.
- 54:14and so some of the mat, so for instance, if you're using Ruby, there's Breakman and it's made just for Ruby and that's it.
- 54:21So it's awesome because it only cares about your language because it's free and open source.
- 54:27There's not a giant security team behind it, supporting it.
- 54:31But if you have never used a static analysis tool, this is an excellent place to start.
- 54:36It's recommended over and over and over again.
- 54:39When I, when I teach secure coding in this, I have like this list of free Python resources.
- 54:45People love Python.
- 54:46People make a lot of tools for it because they love it.
- 54:49And Bandit is like really popular.
- 54:52Yeah.
- 54:52That's awesome.
- 54:52And it has a cool logo.
- 54:54It is very cute.
- 54:56It is very cute.
- 54:57I like how they call it.
- 54:59They call it a security linter.
- 55:02I would say it's a bit more like stack analysis.
- 55:04cause like it, it definitely is trying to find like sources and syncs, like doing flow analysis.
- 55:12I don't know if it does symbolic execution.
- 55:14Like I haven't seen like under the hood.
- 55:16so like when you buy like a SAS tool, there's like first generation and second generation and like different ways that they work.
- 55:24And I went, I'd love to know more under the hood of how it works.
- 55:28Yeah.
- 55:29Yeah.
- 55:29It looks really, it looks really cool.
- 55:31You know, it'll find common things like pickle issues or parsing YAML that might be bad or, you know, all the different things that you're still allowed to do, but people decided probably are not the best choices, but we'll leave it there.
- 55:44So we don't break things that are doing it, but don't do it.
- 55:46And kind of like the pointer safe string copies and C++, don't copy like you used to like, well,
- 55:54how do I do it now?
- 55:55And I don't even know anymore.
- 55:56You know, I feel too that like, so it's called security linter rather than stack analysis.
- 56:02I feel like it also focuses on code quality and not just security.
- 56:06And that's helpful too, because if you have higher quality code, it's just going to be more secure.
- 56:11It's going to be easier to maintain, easier to debug.
- 56:14So if there is a security problem, you can address it faster.
- 56:17You have less technical debt if you have higher quality code.
- 56:20So that's a win as well.
- 56:22Absolutely.
- 56:23All right.
- 56:23Let's talk about SQL relational databases, these kinds of things a little bit.
- 56:29I think there's, you know, the majority of people out there have an app that talks to a database.
- 56:34That database is relational.
- 56:36Usually it's running on its own server.
- 56:38Yeah.
- 56:38And when things go bad, it's usually the data that came out of the server that makes the news.
- 56:43Not always, but usually.
- 56:44Yeah.
- 56:45But it can be the server.
- 56:46What's an advisor?
- 56:48So, so an SQL server is a server, right?
- 56:52So it is a server.
- 56:53You need to patch it.
- 56:54You need to keep it up to date.
- 56:55You need to harden it.
- 56:57It comes with a hardening guide.
- 56:59You should do all the steps in the hardening guide.
- 57:01You should make sure that every single person you work with cannot access it, right?
- 57:05Like it seems really obvious, but you would be surprised.
- 57:11So like basic server hygiene applies to them.
- 57:17On top of that, then the SQL software, the SQL server software itself, it can be hardened as well.
- 57:25And that might sound odd, but like it has updates that it needs.
- 57:29So you, you want to make sure that you want to make sure that you have locked it down the way that you think that you should.
- 57:38Yeah.
- 57:38Then absolutely.
- 57:39And it can run in different users as well, right?
- 57:42It's easy to, it's easiest if it just runs as root, but wouldn't it be better if it, in case somebody gets to it and breaks in through it, right?
- 57:49You don't want them to use it as a lateral movement.
- 57:52You know, first they got into the server and then they went over to, you know, who knows where.
- 57:56We also want to make sure ideally that people are not accessing it with database owner unless they're the database owner, right?
- 58:04Like ideally DBO users are used rarely.
- 58:07Like let's say you're using MS SQL server.
- 58:11Yeah.
- 58:11That has database owner privileges.
- 58:14I get it.
- 58:15Cause you're a database administrator, but if your app or your API is accessing the database, we want to use a least privilege approach.
- 58:22So if you're just doing Slack statements, just do a read only user.
- 58:25If you're doing cred, create, read, update, delete, then you should use a read, write user.
- 58:31But DBO is not almost ever actually needed if we think about it.
- 58:35And so if you don't have that, then less can happen.
- 58:38That is bad.
- 58:38Right.
- 58:39Does it need access to every table or just these 10 tables?
- 58:42Exactly.
- 58:43Another thing is like classifying the data that's in each table as sensitive or not sensitive and maybe work for the government.
- 58:54So maybe it's like classified or secret or top secret or whatever your organization uses.
- 59:00But if you could classify those things and then label them, a lot of databases now have labels for sensitivity, which is awesome.
- 59:09But if not just add an extra field, if not just like add a field called sensitivity, and then it's just like public or unclassified or super secret.
- 59:18Don't show people.
- 59:20And when you do that, it makes life so much better if there is a security incident, because I know if I need to freak out a little or if I need to freak out a lot.
- 59:30And if it's not labeled, I have to assume the absolute worst when I respond until I know it's a lower threat.
- 59:37And that really sucks.
- 59:40Yeah.
- 59:41Yeah, for sure.
- 59:42Another thing that you called out in this section is make sure that logging is turned on.
- 59:47And you need to do this before something goes bad, because it's the logs that tell you what happened.
- 59:53Yes.
- 59:54I think logging, not just for databases, but across the board, logging is super important.
- 59:59Oh, yeah.
- 1:00:00I have a whole giant section in the book about what to log, what not to log, when to log, like exactly what I would love a log to look like, how to protect your logs.
- 1:00:10I had a customer and they're like 28 of our customers, their credit cards got nicked and Visa called us and they want us to prove that it wasn't us.
- 1:00:21And I'm like, great, let's go get the logs.
- 1:00:23They're like, we kind of deleted those.
- 1:00:26You know, they're hard to back up.
- 1:00:29I said, what?
- 1:00:30And they're like, well, we just switched to a new server four months ago.
- 1:00:34And so we just deleted the old server.
- 1:00:36So like, and we just did that.
- 1:00:38So like for four months, we have no records.
- 1:00:41And I said, oh my gosh.
- 1:00:43And so luckily, it turned out there was a sandwich shop in the same building and they caught an employee was skimming the cards.
- 1:00:51And so Visa's like, we figured it out.
- 1:00:53And we're like, great.
- 1:00:54I'm like, we're all getting fired.
- 1:00:55Because they just knew.
- 1:00:58But if Visa yanks your ability to use it and your main way of making money is charging money over the Internet, like you've just reduced your ability to do the main purpose of capitalism.
- 1:01:11Yeah, life is, you do not want to tick off Visa or MasterCard.
- 1:01:14Don't do it.
- 1:01:16All that stuff is definitely, it's definitely unnerving.
- 1:01:19Let's see some more advice.
- 1:01:21You got using ORM if you can, because they basically are immune, but they have a lot of automatic guards against.
- 1:01:29You're not writing direct string.
- 1:01:31So it's harder to concatenate that stuff.
- 1:01:33Yeah.
- 1:01:33They will also do a lot of the code for you, depending upon the one that you use.
- 1:01:38Like, I've used the entity relationship framework with .NET.
- 1:01:41It's like, I'm going to write all your gets and sets and do this and that for you.
- 1:01:44I'm like, oh, sweet.
- 1:01:45Thanks, buddy.
- 1:01:45And even your migrations and stuff.
- 1:01:47Yeah.
- 1:01:47It's pretty nice.
- 1:01:48Yeah.
- 1:01:49I'm a fan.
- 1:01:50I'm a believer.
- 1:01:51I know some people say, oh, it's a little bit slower or whatever.
- 1:01:53Like, yeah.
- 1:01:54But I like getting stuff done and sleeping at night.
- 1:01:56Yeah.
- 1:01:57So another one I think is really worth pointing out.
- 1:02:00Most important for databases, but also just generally.
- 1:02:03Good idea.
- 1:02:03Have an extensive and well-thought-out backup plan and try to back up something at least once.
- 1:02:08Or try to restore something at least once.
- 1:02:11Yes.
- 1:02:11I worked somewhere and we had a computer problem and they lost everyone's work in our entire 2,000-person department for the whole week.
- 1:02:22So for like the three days.
- 1:02:24It was the Wednesday.
- 1:02:24And they had lost all of our work.
- 1:02:27Everyone's work was not saved.
- 1:02:28All of it was gone.
- 1:02:29And we go to the backup guys and we're like, okay, do your thing.
- 1:02:33And they're like, oh, well, it'll take like at least a month.
- 1:02:36And we've never really tried it before.
- 1:02:37And we don't think it'll really work.
- 1:02:39And so you guys should just still redo it.
- 1:02:41And my boss was like, oh, I guess we have to redo it.
- 1:02:44And I was like, well, can I hire two new software developers?
- 1:02:48And he's like, why?
- 1:02:49I'm like, well, because those guys are obviously fired.
- 1:02:51Right?
- 1:02:52And then I can just like hire two new devs.
- 1:02:54It'll be great.
- 1:02:55And he's like, Tanya, go back to your room.
- 1:02:56But I'm like, we don't need them.
- 1:03:00Their job, they just proved that their job is completely worthless.
- 1:03:05So let's just get rid of them.
- 1:03:06And he was like, stop talking.
- 1:03:08Go away.
- 1:03:09I understand you're frustrated.
- 1:03:11That's not constructive, Tanya.
- 1:03:14If I fire them, then they're going to have to fire me because I hire them.
- 1:03:16No, I'm just kidding.
- 1:03:16You know, one thing that comes to mind, though, is, you know, back in the day,
- 1:03:22we had Suburgeon and CVS and SourceSafe and all these things.
- 1:03:26And if something went wrong with that, it was just gone.
- 1:03:28It was just gone.
- 1:03:29And now we have Git.
- 1:03:30And if something goes wrong, there might be 100 copies of it.
- 1:03:33It's less bad from a software person's perspective.
- 1:03:39Three times I have worked somewhere where they lost their code repository.
- 1:03:42Yeah.
- 1:03:43One of the times I started and one of the employees was junior and he had just deleted it by accident.
- 1:03:52And I managed to go to each person's computer and recover a ton of the code and put a lot of it back together.
- 1:04:00Another time someone just deleted it and they, I feel, were malicious.
- 1:04:06And then another time, basically, we didn't want to wait for shared services.
- 1:04:13So the Canadian government decided we would make a department that was the IT department for the whole government.
- 1:04:17And they just wouldn't give us a server.
- 1:04:19So we just took a server from another room and repurposed it and decided that was our code repo survey.
- 1:04:24And so I set up a whole network.
- 1:04:26I set up Active Directory and all this.
- 1:04:28I installed Team Foundation server.
- 1:04:30I did all the stuff.
- 1:04:31I set it all up.
- 1:04:32And I was like, listen, bud, I did this for you, but you need to back it up every night.
- 1:04:35And he promised me he would.
- 1:04:37And then five months later, it crashed.
- 1:04:39And he's like, are you going to make it go again?
- 1:04:42And it was a RAID server.
- 1:04:43And it automatically deleted everything.
- 1:04:45Yeah.
- 1:04:46And so I was like, well, get your backup.
- 1:04:48Did its job.
- 1:04:49It replicated.
- 1:04:50He had not backed it up a single time in five months.
- 1:04:53And we lost 11 contractors work for like months, like five months.
- 1:04:57I was like, I am so angry.
- 1:05:00And he's like, could you spend this weekend making us a new?
- 1:05:02I was like, no.
- 1:05:03I'm so angry at you.
- 1:05:05You'll make it.
- 1:05:06He's like, but I don't know how.
- 1:05:07I'm like, I guess it's tough to be you.
- 1:05:08You're going to learn.
- 1:05:10You're going to learn the hard way.
- 1:05:12Yeah.
- 1:05:13So we're pretty much out of time here.
- 1:05:15But I do want to maybe just point out that there is a whole section on Flask, which is pretty awesome.
- 1:05:20And you talk a lot about different extensions that you can use, like Flask Secrets for secret management or Flask WTF for SEO.
- 1:05:30There are CSRF protection and things like that.
- 1:05:33So there's a bunch of stuff in there.
- 1:05:35People want to go check that out.
- 1:05:36But I think we might need to call it for time.
- 1:05:38But yeah, this is good stuff.
- 1:05:40If you want to learn more besides obviously purchasing all of my books, I have a free online academy at academy.semgrep.dev.
- 1:05:50I don't know if you want me to spell it because that's like...
- 1:05:52Yeah.
- 1:05:53I'm going to...
- 1:05:58I'll find it.
- 1:06:00I'll find it.
- 1:06:00There.
- 1:06:01Oh, it's like it's through my full-time job.
- 1:06:04So I train on the side and I do stuff for them full-time.
- 1:06:07But I put it in our private chat.
- 1:06:09And basically, I have a free secure coding course in there.
- 1:06:13It's a few years old.
- 1:06:14Like the book is all brand new stuff.
- 1:06:16But it covers like agnostic, you know, how to do input validation, how to do output encoding, how to make sure that you are using parametrized queries, how to configure every single security header.
- 1:06:27And it's just free.
- 1:06:29And I do that because I need us to do better real bad.
- 1:06:35Please, please, please, please, please.
- 1:06:37Yeah.
- 1:06:37Well, thank you.
- 1:06:38That's awesome.
- 1:06:39Thank you.
- 1:06:40Yeah.
- 1:06:41It's been a really fun conversation.
- 1:06:43And I feel like we could probably talk for another two hours, but...
- 1:06:47I know.
- 1:06:48Well, maybe in another year or two, we'll come back if you'll have me.
- 1:06:52Yeah.
- 1:06:52Yeah.
- 1:06:53That'd be amazing.
- 1:06:53Well, let's leave it with a final call to action.
- 1:06:56People are...
- 1:06:57You have their attention.
- 1:06:58They thought, well, maybe I should validate that or learn more or do more.
- 1:07:02What do you tell them before we wrap it up?
- 1:07:04I want you to go look at whatever framework that you are using and see if there are security features and start using them in your code.
- 1:07:13So if you're using Flask, there's a whole bunch of super awesome things in Flask.
- 1:07:17Please use them.
- 1:07:18Your life will be better.
- 1:07:20Yeah, absolutely.
- 1:07:21Well, thank you for sharing all your experience and the story.
- 1:07:25That's been a lot of fun.
- 1:07:26Thank you so much for having me, Michael.
- 1:07:28Yeah, you bet.
- 1:07:29Bye.
- 1:07:29This has been another episode of Talk Python To Me.
- 1:07:33Thank you to our sponsors.
- 1:07:35Be sure to check out what they're offering.
- 1:07:36It really helps support the show.
- 1:07:38This episode is sponsored by Posit Connect from the makers of Shiny.
- 1:07:42Publish, share, and deploy all of your data projects that you're creating using Python.
- 1:07:47Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, Reports, Dashboards, and APIs.
- 1:07:53Posit Connect supports all of them.
- 1:07:56Try Posit Connect for free by going to talkpython.fm/Posit.
- 1:08:00P-O-S-I-T.
- 1:08:01And this episode is brought to you by Bluehost.
- 1:08:04Do you need a website fast?
- 1:08:06Get Bluehost.
- 1:08:07Their AI builds your WordPress site in minutes, and their built-in tools optimize your growth.
- 1:08:12Don't wait.
- 1:08:13Visit talkpython.fm/bluehost to get started.
- 1:08:17Want to level up your Python?
- 1:08:19We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:08:22Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:08:27And best of all, there's not a subscription in sight.
- 1:08:30Check it out for yourself at training.talkpython.fm.
- 1:08:33Be sure to subscribe to the show.
- 1:08:35Open your favorite podcast app and search for Python.
- 1:08:38We should be right at the top.
- 1:08:39You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:08:44and the direct RSS feed at /rss on talkpython.fm.
- 1:08:49We're live streaming most of our recordings these days.
- 1:08:52If you want to be part of the show and have your comments featured on the air,
- 1:08:55be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:08:59This is your host, Michael Kennedy.
- 1:09:01Thanks so much for listening.
- 1:09:03I really appreciate it.
- 1:09:04Now get out there and write some Python code.
- 1:09:06I really appreciate it.