Latest / Talk Python To Me / #457: Software Supply Chain Security with Phylum
Transcript
- 0:00We've spoken previously about security and software supply chains, and we're back at it on this episode.
- 0:05We're diving in again with Charlie Coggins. Charlie works at a software supply chain company
- 0:10and is on the episode to give us an insider's look and a defender's perspective on how to keep
- 0:16our Python apps and infrastructure safe. This is Talk Python To Me, episode 457, recorded January 24th, 2024.
- 0:25Welcome to Talk Python To Me, a weekly podcast on Python. This is your host, Michael Kennedy.
- 0:45Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 0:50both on fosstodon.org. Keep up with the show and listen to over seven years of past
- 0:55episodes at talkpython.fm. We've started streaming most of our episodes live on YouTube. Subscribe to
- 1:02our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and
- 1:07be part of that episode. This episode is brought to you by Sentry. Don't let those errors go unnoticed.
- 1:13Use Sentry like we do here at Talk Python. Sign up at talkpython.fm/sentry. And it's brought to
- 1:20you by Mailtrap, an email delivery platform that developers love. Use their email sandbox to inspect
- 1:27debug emails and debug emails in staging, dev, and QA environments before sending them to recipients in
- 1:32production. Try Mailtrap for free at talkpython.fm/Mailtrap. Hey, Charlie, welcome to Talk Python To Me.
- 1:40Hi, Michael.
- 1:40Great to have you here. We have corresponded back and forth about security things. And now,
- 1:48you're scared. It's going to seem that way. There are threats everywhere, especially when you start
- 1:54looking. And that's the problem. You look, you'll find them. And if you're not looking, you might get
- 2:00affected without even knowing it.
- 2:03Yeah, but that's true. But we're also going to come with some tools and techniques and tips on
- 2:07how to avoid security problems with your Python code.
- 2:11Yes, absolutely.
- 2:12Yeah, I think it's especially concerning. That certainly catches my attention that if you mess
- 2:21with somebody's software, like the software builders, the developers, it gets shipped to however many
- 2:27users are on the other side of that equation, right? It's not like I just took over some
- 2:31teenagers gaming PC, and now what can I do? It's like, I took over, you know, name your big web app,
- 2:39and now we're going to start shipping some stuff around. All right, that's where the sort of
- 2:44multiplicative aspect of this gets more concerning than just standard personal computer safety, right?
- 2:50Oh, absolutely. You know, a single developer can have very broad impacts. You know, maybe they
- 2:58publish one package, but that one package could be included in hundreds, thousands of other packages
- 3:04as a dependency. And then everyone using those packages could be affected, whether the code is
- 3:10good and works as intended or poorly written and has bugs and vulnerabilities.
- 3:15Yeah, this is not to say there's any chance of there being a problem with Pydantic. But just to make
- 3:21your point, if you go to like Pydantic or Request or something like that, a lot of these have used by
- 3:27projects, right? And this Pydantic is used by 315,000 people, not people, software projects that
- 3:35themselves have users, right? And so that's the kind of stuff that I'm thinking about when I said that
- 3:40multiplicative effect, right? It's a big multiplier, not just a couple.
- 3:44Oh, yeah. Yeah, for sure.
- 3:45Yeah. Now, before we dive into our main topic, of course, you know, tell people a bit about yourself.
- 3:51Hi, well, my name is Charles Coggins. I usually go by Charlie. And I'm a Python developer. I'm a
- 3:59software developer, but not through the traditional sense. I don't have a computer science degree. I
- 4:03didn't come to this, you know, straight out of school. I got my first taste of programming long
- 4:11enough ago, back in the 80s, in 1987. My dad got a computer for us. And, you know, I was messing
- 4:18around on there with some games, always with games, right? You know, at the time, it was basic.
- 4:25You know, it was this bowling game that my brother and I would play. And I saw that I could look at the
- 4:30code. I could look at the source. And I went in there and modified it a bit to make it so that I would
- 4:34always win whenever I played him.
- 4:38How long did it take him to catch on?
- 4:39Oh, he figured out pretty quickly. And he was in there to change in, you know, ball speed and,
- 4:44you know, how often he could get a gutter or get a gutter. But yeah, I, you know, took a class or two in high
- 4:52school and college. But I was an electrical engineering major and then went to work for the government
- 4:59doing something that wasn't even really that. So I spent 10 years working for the government before
- 5:07they stood up the U.S. Cyber Command and decided or figured out that they needed to hire 6,000 new
- 5:17developers to fill the positions. And there weren't that many available in the industry, let alone those
- 5:24who could, you know, pass the clearances and work in that environment. So they looked to people already
- 5:30working in the government. And I raised my hand. I said, yes, yes, I want to cross train. I'll be a
- 5:35developer. And so they trained me.
- 5:38Excellent. What did they teach you for language in that program?
- 5:41We started with C, C++, and then there was some Python. So I went through a couple of boot camps and
- 5:49a lot of self-learning, self-teaching. Python's the one that really clicked for me. It just made sense
- 5:55in my head.
- 5:56Yeah, of course. If you're learning to do cybersecurity stuff, you know, a lot of times I'd be happy to tell
- 6:02people like, ah, you don't really need to learn C or Rust or Java. If you just know Python, you're
- 6:09probably 90% of the time golden. But if you're trying to do cybersecurity, a lot of times it's about
- 6:15like the machine level stuff, right? Understanding things like C and pointers and buffer overflows and
- 6:21all of that kind of stuff is where you actually kind of need to be.
- 6:24And they taught us all that as well. In fact, we learned assembly language as well. And that one
- 6:30really didn't fit in my brain.
- 6:33You're not like, I didn't want to become an assembly language programmer.
- 6:38I mean, yeah, that's a whole different breed.
- 6:41Yeah, it sure is. And, you know, it used to be, I remember when I first got into programming,
- 6:47I was doing some C, C++ and inline assembly was something people would do a lot to optimize. A
- 6:53lot like people might do Cython or Numba or something like that to make Python fast. Like we'll find this
- 6:59little part and we'll rewrite it in this way and be like, we're just going to do inline assembly. I'm like,
- 7:03that just doesn't seem like worthwhile. I don't need that much performance. We're going to not do that.
- 7:09Yeah. Yeah.
- 7:10Fun. So now you're working at Phylum. Is it Python focused or just software security?
- 7:20It's not Python focused. In fact, the company primarily develops with Rust, as you were mentioning.
- 7:28Okay. Yeah.
- 7:29Yeah. Yeah. We've got some excellent Rust developers at our company. And I think that's what attracted
- 7:36a lot of them is that that is the primary language we use. But we also have some elements in Python. And
- 7:43when I came on board, I got assigned to work on our integrations. So like GitHub,
- 7:51integrations, GitLab, pre-commit hooks, things like that. And so I was able to kind of architect it the
- 7:59way I thought best. And because I love Python, I made it all in Python and exposed it through Docker
- 8:06containers.
- 8:07Are you doing direct integration with Rust, like Py03? Or is it more issuing commands out?
- 8:17The Rust elements that our company works on, like our API, the command line interface, a lot of the
- 8:25backend, it's just written straight Rust. And then the Python is just plain Python. There's no
- 8:31interface between the two, really.
- 8:33Yeah. Okay. Consuming APIs and Docker containers and stuff like that.
- 8:37Right. Right. Right. Although I am interested in the Py03. And I think there's room to
- 8:45bridge the two languages at our company.
- 8:47I mean, for sure, people are adopting Rust for the performance foundations of Python. It's pretty
- 8:56interesting.
- 8:57Yeah. Yeah. I've been at the company almost two years now. I keep saying it's what I'm going to
- 9:03learn next is Rust. And I felt like I would just kind of absorb it by going through code reviews and
- 9:09the people on my team. It hasn't happened yet. I can kind of understand what's going on by reading it,
- 9:14but I just, yeah, I need to jump in.
- 9:17It's in depth. Okay. Got it. Those are the same. Okay. Got it.
- 9:19Yeah. Yeah.
- 9:20No, it's interesting. Okay. Well, we're not here to talk about Rust, although I do think
- 9:26it's becoming one of those things that is sort of, I don't know if you need to be a little one level
- 9:32deeper in the Python space that used to be C and now it's, I think it's pretty solidly moving to be
- 9:38Rust, right? It's, there's a lot of popular things. Identic, for example, I pulled up earlier where
- 9:43that's the foundation, but that also seems to be where the momentum is.
- 9:46Yeah. The oxidation of Python libraries is a real thing. I mean, you know, look at Ruff, right?
- 9:53Yeah. Ruff. I just heard about how Granian, I think it was, which is a new, similar,
- 10:01similar to Gunicorn and uWSGI is a Rust-based async server. You know, there's, it goes on and on.
- 10:08This portion of Talk Python is brought to you by OpenTelemetry support at Sentry.
- 10:16In the previous two episodes, you heard how we use Sentry's error monitoring at Talk Python and how
- 10:22distributed tracing connects errors, performance, and slowdowns, and more across services and tiers.
- 10:27But you may be thinking, our company uses OpenTelemetry, so it doesn't make sense for us to switch to
- 10:34Sentry. After all, OpenTelemetry is a standard and you've already adopted it, right? Well, did you know,
- 10:40with just a couple of lines of code, you can connect OpenTelemetry's monitoring and reporting
- 10:45to Sentry's backend. OpenTelemetry does not come with a backend to store your data, analytics on top
- 10:51of that data, a UI, or error monitoring. And that's exactly what you get when you integrate Sentry with
- 10:58your OpenTelemetry setup. Don't fly blind. Fix and monitor code faster with Sentry. Integrate your
- 11:05OpenTelemetry systems with Sentry and see what you've been missing. Create your Sentry account at
- 11:09talkpython.fm/sentry dash telemetry. And when you sign up, use the code TALKPYTHON,
- 11:16all caps, no spaces. It's good for two free months of Sentry's business plan, which will give you
- 11:2020 times as many monthly events as well as other features. My thanks to Sentry for supporting Talk
- 11:27Python and me. All right, well, let's talk about software security though. You know, like we touched
- 11:34on it a little bit with the multiplicative aspect of like why software developers should care.
- 11:39But maybe let's start with some ways in which viruses might get on a computer from a software
- 11:46perspective. Not from like, oh, you know, I found this cool app on BitTor and normally it's paid,
- 11:51but this one's free. It's like, maybe don't install that. But, you know, not that kind of advice,
- 11:56right? But, you know, specifically for software developers.
- 11:59Right, right. So for software developers, I think the primary vector, you know, for malicious code
- 12:07running in your environment or really any developer environment along the way, it doesn't just have to
- 12:12be your system. It could be your CI, CD servers and your runners. It's going to be software
- 12:19dependencies, third-party code, right? Code from strangers on the internet, right? That's really what it boils down to.
- 12:26They just, Charlie, they're just here to help out. They're just giving you the code to help out.
- 12:32They have no bad intentions. Right, right.
- 12:35Except for that one. That one over there, don't take that one.
- 12:39Yeah. And it's hard to tell, you know, what's good, what's bad. And I think we all rely on third-party code.
- 12:49I mean, I think it's a rare company, rare project that writes everything from scratch on their own without any dependencies.
- 12:57Yeah.
- 12:59So that's a vector for sure, is allowing code from strangers on the internet to run. I think, like, the name of the game, right,
- 13:07for attackers and threat actors is arbitrary code execution. Like, that's the key phrase.
- 13:16Arbitrary code execution. If I can get arbitrary code execution with this vulnerability, then I've won.
- 13:21I can attack your stuff.
- 13:22You're going to get a CVE score of nine or above. It's right there.
- 13:26Yeah, exactly. And that's for vulnerabilities. That's just, you know, poorly written code or code with bugs.
- 13:32But forget about vulnerabilities. I mean, if you're an attacker, you're a threat actor, you've already got the perfect means to run arbitrary code,
- 13:41to gain arbitrary code execution on a developer system. That's with third-party dependencies.
- 13:46Open source software is just the perfect target for writing malware or slipping malware into packages.
- 13:55Now, when people hear this, we've talked about it enough. It actually came as quite a surprise a few years ago.
- 14:02People theoretically knew that it could happen, but that it was happening is that packages on package stores like PyPI and npm and so on
- 14:12got published vulnerabilities that people could then install and make part of theirs.
- 14:16But there's a whole software supply chain, right?
- 14:19Maybe talk us through some of the different elements that make that up.
- 14:21Only one of which is these libraries, right?
- 14:24That's right. That's right.
- 14:25So the software supply chain is really it's using third-party code securely, as well as securing the end-to-end development process.
- 14:35So that process is very broadly broken into three phases.
- 14:39You've got the source phase.
- 14:43That's source control management systems and then actually coding.
- 14:49Developers coding on their systems, committing to repositories.
- 14:55Yeah.
- 14:55And you mentioned the dependencies like pip install this or that.
- 15:01There's also, for many of the really popular IDs and editors, there's a whole massive array of varying levels of trusted plugins or extensions, right?
- 15:13As well.
- 15:14That's right.
- 15:14Yeah.
- 15:15Like Visual Studio Code, that's what I use for my IDE.
- 15:19It's got an extensive extension ecosystem.
- 15:22Just about anything you want to do.
- 15:26I get a little pop-up when I open a new project and it says, oh, I recognize you're using a YAML file.
- 15:30Do you want to download this extension that will lend to YAML files, right?
- 15:35Yeah.
- 15:35I got one for CBEs.
- 15:37It was like rainbow CSV syntax highlighter or something.
- 15:42I'm like, you know what?
- 15:43That's not really made by a trusted company.
- 15:46It's probably fine.
- 15:47It's probably fine.
- 15:49But I don't need my CSV files highlighted so much so that I'm willing to just run arbitrary code from a stranger on the internet.
- 15:56That's right.
- 15:57Right?
- 15:58Yep.
- 15:58And, you know, I use both PyCharm and VS Code and they both, especially PyCharm, has sort of a warning that says, this is untrusted.
- 16:07It's a third-party thing.
- 16:08Are you sure you want it?
- 16:10Right.
- 16:10Just saying no.
- 16:11That's a pretty light warning.
- 16:12Yeah.
- 16:13And also, they're not the same, right?
- 16:15Is it installed by a million people used every day?
- 16:18Or is it for you the fourth person to use it?
- 16:21And it hasn't, you know, hasn't had the experience of people going, why is it opening a network socket?
- 16:27What's it doing?
- 16:28You know, something like that.
- 16:31Yeah.
- 16:31Yeah.
- 16:32That's another entry point you got to be careful about.
- 16:36All right.
- 16:37Well, I cut you off.
- 16:38We're only in like square one of maybe nine.
- 16:40Yeah.
- 16:41Square one, source code.
- 16:42And then there's the build phase.
- 16:45That's where you take the code.
- 16:48You take the commits that have gone into source control.
- 16:50And you build something with it, right?
- 16:54This usually happens in, you know, your CI, CD systems, GitHub, and Git Labs of the world.
- 17:02And it's at that point where, you know, your third party dependencies get included and wrapped up into your artifacts, right?
- 17:13Which brings us to the third stage of the software supply chain, which is the package and deploy phase.
- 17:20That's where you're creating your artifacts and making them available to the world to use.
- 17:27Could be anything.
- 17:28Could be a wheel for a library that other parts of your company use to build software.
- 17:34Yep.
- 17:34Could be some app you ship.
- 17:37It could actually be a website, an API.
- 17:39Who knows, right?
- 17:40Yeah.
- 17:41Docker container.
- 17:42Docker container.
- 17:43Yeah.
- 17:43Yeah.
- 17:44Yeah, exactly.
- 17:44And then by the time you get to that, you know, the end of the supply chain and, you know, the products or the packaged product that people are going to see and use and work with, you know,
- 17:57you've baked in so many elements at that point, you know, from your third party dependencies to, you know, any other external resources that are getting called.
- 18:11So there's lots of points along the way that it's possible to.
- 18:17Yeah.
- 18:18One of the things that can be sneaky is, you know, it doesn't happen that often in Python, but you're shipping like a Windows or a Mac app.
- 18:26There's a digital signature proof of we're going to sign this with our trusted certificate.
- 18:32So it doesn't even give you any warnings.
- 18:34Like, look, this is it's signed by the company.
- 18:36It is trusted.
- 18:37Here you go.
- 18:38Pick it.
- 18:39Right.
- 18:39And somewhere upstream from that, there's an issue like with packages or other things.
- 18:45Well, that issue is now that that problem is signed and verified as well.
- 18:50Yeah.
- 18:50Yeah.
- 18:51Yeah.
- 18:51You know, so you mentioned you mentioned Claude Code signing the research team at our company.
- 18:57I mean, they're amazing, amazing group there.
- 19:00They're always finding new and novel attacks.
- 19:04And one they found just this past week involves something kind of cool where the attacker had bundled up a valid Microsoft binary.
- 19:15It had been signed by Microsoft.
- 19:17But they bundled it with a DLL that was malicious.
- 19:22It was named something to be expected.
- 19:25Right.
- 19:26So when you run the executable on the binary, you know, you could see that there's this Microsoft signs application looking for permissions, looking to continue.
- 19:38And you're like, oh, yeah, great.
- 19:39Signed by Microsoft.
- 19:39No problem.
- 19:40But then it uses this technique called like DLL search order hijacking.
- 19:45Okay.
- 19:47That technique.
- 19:47Right.
- 19:47So if you have a DLL that's being called by the application more locally than not, that's what it's called.
- 19:56So it's looking for something in like.
- 19:57Yeah.
- 19:58It'll look for the name of the DLL in the same directory first, basically, is what's happening.
- 20:04Right.
- 20:04Right.
- 20:05They had shipped their bad DLL with a good binary.
- 20:09So you pick something in system 32 that's got like a real common name like VC runtime whatever dot DLL or, you know, some of the standard ones.
- 20:20But then you completely reprogram it.
- 20:22Yeah.
- 20:23And stick it in there with that app.
- 20:24Or maybe not completely because you need the app to not crash.
- 20:26But you give it some extra boost when it does something.
- 20:30Right.
- 20:30Yeah.
- 20:31Yeah.
- 20:31In this case, they had just copied all the files needed for execution into a new directory, including the known good binary, the known bad DLL.
- 20:40And then, you know, it had everything it needed in that directory to run.
- 20:44And it looked like it was legitimate.
- 20:45Right.
- 20:46Because a lot of the OS dependent, a lot of these OS checks are on the executable, the system libraries that they use.
- 20:53Right?
- 20:53Right.
- 20:54Right.
- 20:54Right.
- 20:55You'll see like this, this executable is downloaded from the internet.
- 20:57Are you sure you want to run it?
- 20:59Like that doesn't say this executable, what you trust is maybe possibly using a library that you downloaded.
- 21:05Like it doesn't say that.
- 21:06Right.
- 21:06Yeah.
- 21:07Cause we could never get work done if there was that level of checking all over the place.
- 21:11This is what updated somewhere.
- 21:12This portion of talk Python to me is brought to you by Mailtrap.
- 21:18We're going to keep this super short.
- 21:20So please pay attention or you'll miss it.
- 21:22Mailtrap is an email delivery platform that developers love.
- 21:26An email sending solution with industry best analytics, SMTP, and email APIs and SDKs for major programming languages with 24 seven human support.
- 21:36What makes them unique is their email sandbox.
- 21:38Use email sandbox to inspect and debug emails in staging, dev, and QA environments before sending them to recipients in production.
- 21:46Try Mailtrap for free at talkpython.fm/mailtrap.
- 21:53That's kind of the space that we're talking about, right?
- 21:56We've got editors.
- 21:57We've got libraries that you use.
- 22:00CI, CD pipelines.
- 22:01Containers are super interesting as well.
- 22:04And all the tools to go with those.
- 22:07So let's talk through some of the posts that you've written and also just selected about some of these things.
- 22:14And maybe starting to the front of that list there with block files.
- 22:17Yeah.
- 22:18Okay.
- 22:18So, yes, I wrote a blog post.
- 22:22I guess it's looking at the date on your screen.
- 22:24It looks like it was over a year ago now.
- 22:26And probably seems like yesterday, but no.
- 22:28Yeah, that's right.
- 22:29That's right.
- 22:31So I'm sure the landscape has changed since then a bit.
- 22:34And maybe there's some new players out there.
- 22:36But, yeah.
- 22:37Yeah.
- 22:38I think one thing you can do as a developer, a big one I would recommend, is use lock files for your dependencies, right?
- 22:47And, you know, what's a lock file?
- 22:51Well, it's the fully resolved set of dependencies that are used by your application, your package.
- 23:01And, you know, if nothing else, like, you should know what's going into your code, right?
- 23:09Like, what?
- 23:10Right.
- 23:10Well, one of the ways this helps.
- 23:12Yeah, exactly.
- 23:13That's a really, that's a bit of a challenge, right?
- 23:15And I think I'll admit when I first got into Python, I didn't do this that well.
- 23:20And, you know, to me, it felt like probably the biggest issue I might run into is instability in my app, right?
- 23:27Like, for example, if I don't pin a dependency, some new thing comes out, I reinstall it on a new computer.
- 23:33Maybe it gets an upgraded version.
- 23:35And there's some library that doesn't work, right?
- 23:37I mean, there's been certainly popular libraries that just said we're having a major version change and we're fixing the mistakes we made 10 years ago.
- 23:44And these three functions are changing or whatever, right?
- 23:47That would break it.
- 23:48But it could also be there's now a malicious version of library X and that's version two.
- 23:55But if you pinned it on version one, even though it's bad, you're still not getting the bad one, at least for a while, right?
- 24:01Absolutely.
- 24:02Yes.
- 24:02So I think I got to look it up.
- 24:05I always forget.
- 24:06Pep 665.
- 24:08Okay.
- 24:09Yeah.
- 24:10Pep 665.
- 24:11665.
- 24:12It's a rejected PEP.
- 24:14Unfortunately, but it was written by Brett Cannon, some others.
- 24:18I know you've had Brett on the show a number of times.
- 24:20I love the stuff he does.
- 24:22Yeah, he does excellent work.
- 24:24And it's kind of a shame this was rejected, but this Pep tried to create a standard block file format for Python.
- 24:35And, you know, if you look into the Pep a little bit, you know, there's some motivation about like why you'd want to do this and, you know, four big reasons.
- 24:43And the third one is when I really key on, which is that, you know, lock files allow for reproducibility.
- 24:48And reproducibility is just more secure.
- 24:51Because when, you know, I'm quoting here from the Pep says, when you control exactly what files are installed, you can make sure no malicious actor is attempting to slip nefarious code into your application.
- 25:01IE, some supply chain attacks.
- 25:03By using a lock file, which always leads to reproducible installs, we can avoid certain risks entirely.
- 25:09And, I mean, that's the name of the game.
- 25:12That's like, that's what our company focuses on, which is avoiding those risks by ensuring you know which dependencies you're using and you're knowing that those dependencies are benign or good, you know, doing no harm.
- 25:27Even if there's something that happens, usually it's going to happen to a popular library because you're using it, hence probably other people are using it, other than type of squatting, which we can talk about.
- 25:40But, you know, if you pin your dependencies, chances are it's, these things only stick around for a little while.
- 25:47It's not like, oh, they discovered it had been there for eight months.
- 25:50It's like, oh my gosh, we heard about it.
- 25:52A few people got it and then we got rid of it, right?
- 25:55Yes.
- 25:55The folks at Pype are pretty excellent.
- 25:57So it's, to some degree, a timing issue as well.
- 26:00Yes.
- 26:00Yeah.
- 26:01Vulnerabilities are different, right?
- 26:03Where that's what a lot of people focus on.
- 26:06A lot of the tooling exists to, you know, discover vulnerabilities in your dependencies, which is good to know about those.
- 26:13But those exist for a long time, right?
- 26:16You have CVEs for known vulnerabilities and they end up in these databases and they're there for years.
- 26:21And if you're using old dependencies or maybe transitive dependencies or using old ones and you're stuck on it, then you're going to be exposed to those vulnerabilities.
- 26:31But what's different about that?
- 26:33Examples of those include the WebP library not too long ago, right?
- 26:38That was baked into Python and then also open SSL, right?
- 26:43So people discovered issues in those.
- 26:45Those are baked into different aspects of Python or some of the libraries.
- 26:48And it's like, well, all of a sudden there's this fire drill.
- 26:51Yes.
- 26:52Which is different than somebody going, I'm going to sneak a thing into the library.
- 26:55Right.
- 26:56And then it is a timing matter.
- 26:58So malicious dependencies, that's a whole other story.
- 27:01Because if a malicious package is discovered, there's not a CVE created for it.
- 27:06The package is just taken off of the registry.
- 27:09You know, you report it to the good people at PyPI and, you know, they'll review the submission and take it down.
- 27:17I've done a few of those myself and they're really fast.
- 27:21But there's still a window of time where that malicious package, that malicious dependency is up and available.
- 27:28And that's, you know, often all that's needed.
- 27:32Yeah, exactly.
- 27:33I do think having a pin dependency there is worthwhile.
- 27:36Because if you make a commit, your CI runs, et cetera, et cetera, right?
- 27:39Like the chances that you just bump the version to this malicious thing is pretty low.
- 27:44Yeah, exactly.
- 27:45So, yeah.
- 27:46Yeah.
- 27:47And having version ranges is not enough.
- 27:50You know, you need to have explicit versions, you know.
- 27:53Let's talk more about these lock files then, right?
- 27:55So there's actually a bunch of choices these days.
- 27:59You know, Brett's PEP tried to make it less of a choice.
- 28:03Say, well, it doesn't matter if you use hatch or pip or poetry or whatever.
- 28:07The outcome is the same.
- 28:09And for reasons that I don't haven't learned enough about, I don't know why that didn't work.
- 28:14But let's talk about what's out there now.
- 28:16Because there's a couple options at this point.
- 28:18Sure.
- 28:19I think the, yeah.
- 28:21So most Python developers are going to be most familiar with pip, right?
- 28:25That's the standard.
- 28:29And pip has requirements files.
- 28:31And, you know, they're unique in the lock file world because they can be named anything, right?
- 28:40Most other lock files have a defined name.
- 28:43We were talking about Rust earlier.
- 28:44You know, they're the gold standard for a lot of this stuff.
- 28:47And, you know, they're very clear.
- 28:49They have cargo.lock.
- 28:50That's their lock file.
- 28:51You can't name it anything else.
- 28:53Its contents are well defined.
- 28:55It is what it is.
- 28:57But in Python with pip, I mean, you could name it whatever you want.
- 29:01You know, dev requirements.ext.
- 29:03You could name it cargo.lock.
- 29:05But it can contain Python dependencies in it.
- 29:08Surprise.
- 29:09I'm not Rust.
- 29:11Basically, you can just put more or less arbitrary commands that are sent to pip in a text file, right?
- 29:18Yes.
- 29:18Which is more or less what it is.
- 29:19Yeah.
- 29:19Yep.
- 29:20Yep.
- 29:21Any command line option you can feed the pip, you can put in a requirements file.
- 29:25It's cool because you can have an import by saying dash or some other file.
- 29:29Yes.
- 29:30Yes.
- 29:31But it's also not super structured.
- 29:33You can get a hierarchy that way.
- 29:33Mm-hmm.
- 29:34Yeah.
- 29:34Yeah.
- 29:34So there are some tools available to turn those, like, loose requirements files, the pip requirements files, into strict lock files, right?
- 29:47Where every entry is pinned to a specific version.
- 29:51And pip itself can do it with the pip freeze command.
- 29:54So that's the one most people know about.
- 29:57But that one's kind of not so great because it only freezes the packages for the environment that you ran pip freeze in.
- 30:05You know?
- 30:06And maybe you're trying to publish your lock file for users of a different platform or system.
- 30:13The other thing that I don't like about it is you want to put just the things you actually use into your requirements file.
- 30:19Like, I'm using HTTPX and Pydantic.
- 30:21That's it.
- 30:22But what it really installs when you run that is the transitive closure of all those things.
- 30:27Yes.
- 30:28Which is fine.
- 30:28But you're not necessarily expressing that with just your requirements.txt, right?
- 30:35Right.
- 30:36Yeah.
- 30:37Yeah.
- 30:37Your two packages could balloon to, you know, 100 dependencies.
- 30:41And that's not uncommon.
- 30:43It's not even that bad.
- 30:44Like, in the JavaScript ecosystem, you know, the same handful of top-level dependencies could have two orders of magnitude explosion where you end up with thousands.
- 30:54There's a really...
- 30:56Oh, gosh.
- 30:56I can't find out.
- 30:57You know what?
- 30:57I think it's on...
- 30:59I think I put it on the Python bytes.
- 31:00But there's a really funny...
- 31:01I want to be able to pull this up for people so they can find it.
- 31:03There's a funny, funny thing that somebody did.
- 31:07Well, for some definition of funny.
- 31:11They put...
- 31:12Somebody created an npm package called Everything.
- 31:16Yes.
- 31:17And there's an article called Everything Becomes Too Much, the npm Package Chaos of 2024.
- 31:23Yeah.
- 31:23An npm user named Patrick JS launched a troll campaign with a package called Everything, which depends on every package in npm.
- 31:31Yeah.
- 31:32Yeah.
- 31:32And that's...
- 31:33I think it's the...
- 31:34NPMs are the largest package registry out there.
- 31:37So it's...
- 31:38I mean, it's already massive.
- 31:40I remember your early episodes, you would recount how many packages were on PyPI.
- 31:46And then we got to that...
- 31:47I don't even know.
- 31:47Are we past half a million?
- 31:49...6-figure number?
- 31:49Well, yeah.
- 31:50I remember it was a big deal.
- 31:51It got up to 100,000.
- 31:52And now it's probably, what?
- 31:53400,000?
- 31:55500,000?
- 31:55Over 500,000.
- 31:55Over 500,000.
- 31:57509 by rounding.
- 31:58Yeah.
- 31:59Half a million.
- 32:00Congratulations, world.
- 32:01Amazing.
- 32:03Yeah.
- 32:04Yeah.
- 32:04I just added two new ones last week.
- 32:06So I guess I've made a huge difference in that number.
- 32:08Nice.
- 32:10Yeah.
- 32:11So basically, the pip is awesome and it does a bunch of great stuff.
- 32:14And one of the things I really like about working with pip is I don't need to teach people anything if they want to work with my project.
- 32:20Right.
- 32:21I don't need to teach them like, oh, I know you love poetry, but I'm using a combination of the hatch build back end with PDM.
- 32:27You're like, what?
- 32:28I don't even know what those are.
- 32:29Right?
- 32:29Like, there's a lot of, like, ways in which you work that are brought in with a lot of these tools here.
- 32:36So pip is kind of like, you know, it just kind of works, right?
- 32:39Yes.
- 32:40But having this transitive closure managed is not part of what it does, but it's super important because if I need to upgrade something, I can't just change my version number in my requirements.
- 32:50Because that doesn't affect its dependency possibly, right?
- 32:53Like, it depends what it's said.
- 32:55So I'm a huge fan of pip-tools.
- 32:57This is actually what I do most of the time.
- 32:59Yes.
- 32:59pip-tools is another one.
- 33:01You can, it's great.
- 33:04I think it has this pip compile command that will take as an input, I think, just about any Python manifest type that's out there.
- 33:15So you can do setup.py, requirements.txt.
- 33:20I'm forgetting the other ones.
- 33:24The pip.ev.lock, maybe.
- 33:27Setup.cfg, pyproject.toml.
- 33:31It just recognizes all the different ways people could express their loose requirements, you know, the manifest files.
- 33:41Yeah.
- 33:41So, yeah.
- 33:42Yeah.
- 33:42I really like it.
- 33:43And you can say pip compile upgrade and it'll look at all the dependencies and upgrade them all as high as they can go.
- 33:50But what's nice about that is you'll be working for a while.
- 33:52Then you choose, like, well, let me just do a refresh on the dependencies right now and repin them and see how that works.
- 33:58And then just carry on with your business for a while, right?
- 34:01And it'll manage that transitive closure as well with, like, actually a really nice lock file where it described, like, these are all the things in the lock file.
- 34:09And the reason that, you know, for example, in your blog post, you say they're certified of this version.
- 34:13And it's there because you asked for it and because request needs it.
- 34:16You know, if you're like, why is this in my virtual environment?
- 34:19Why do I have this weird thing that I don't know?
- 34:21Like, it'll tell you, here's why it's there.
- 34:23Yeah.
- 34:24Yeah.
- 34:25One of the downsides, though, I think pip-tools has this issue.
- 34:29I know pip does, is that in determining that transitive dependency resolution, it is very possible.
- 34:39In fact, it usually happens that you have arbitrary code execution on your system, right?
- 34:43Like, if you start with the two top-level dependencies, like you mentioned, and it lists dependencies, well, then it'll pull those in and it acquires the metadata.
- 34:51From the wheel, if that exists.
- 34:54But if it doesn't, it'll build the package just to get the metadata filed, just to figure out which dependencies that needs.
- 35:00Are you saying I should set up a Docker container to execute this?
- 35:05I mean, that's, yeah, that's kind of what's happening.
- 35:07Maybe I should, yeah.
- 35:08Maybe I should.
- 35:09And, you know, yeah, running in a sandbox is another option, right?
- 35:15Where that's what my company, Phylum, that's one of the solutions we offer.
- 35:19You know, we have extensions for our CLI where you can wrap pip by just calling Phylum pip.
- 35:27And then everything runs in a sandbox.
- 35:29So that's another solution.
- 35:31Yeah.
- 35:32Yeah.
- 35:32Yeah.
- 35:32So good.
- 35:33Because, I mean, pip is a funny one because they even have a command line option called dry run, tac-tac-dry run, which you would think, oh, nothing's going to happen on my system.
- 35:43It's just separate.
- 35:43Running code from strangers on the internet.
- 35:45But it does.
- 35:46Yes.
- 35:47Dry run, even using dry run for pip install and pip download commands will or has the possibility of downloading and running arbitrary code from strangers on the internet.
- 35:59If we had, oh, wheels came along far after pip, right?
- 36:03And we've got the source distributions and setup.py and all that kind of stuff.
- 36:07And so if wheels existed from day one, it very well would be the case that this is not a problem, right?
- 36:13But, you know, what is pip supposed to do?
- 36:15Like, it has to evaluate this dynamic thing to figure out what it wants.
- 36:18Yes.
- 36:19Yes.
- 36:20Yeah.
- 36:20Yeah.
- 36:20Wheels are great because, you know, they have a metadata file in there that clearly lays out what the dependencies are.
- 36:29And there's no arbitrary code running when you install a wheel.
- 36:32It's just extracting and copying, you know?
- 36:35Yeah.
- 36:35A wheel is just a zip file.
- 36:37You extract that zip file and then copy the contents to various locations.
- 36:41But, yes, as you said, because we've had source distributions, tarballs, and then even eggs before that,
- 36:49and I'm probably never going to fully get rid of those.
- 36:53It just takes one.
- 36:55One dependency anywhere in your chain that is only distributed as a source distribution.
- 37:00Before now, you're downloading and building a package just to get metadata to continue.
- 37:07Yeah.
- 37:07And maybe you didn't actually choose it, right?
- 37:09It's the dependency of a dependency of a dependency.
- 37:12Absolutely.
- 37:13Yeah.
- 37:13Yeah.
- 37:14Yeah.
- 37:14That's, yeah.
- 37:15Yeah.
- 37:15You know, people often respond to some of the findings our company has where we'll, you know,
- 37:23we'll post these malicious packages with all sorts of crazy names.
- 37:26And people will respond to say, like, you know, why would I install that?
- 37:30Like, why would I ever install this, you know, random package that no one's heard of?
- 37:36It's like, well, you wouldn't.
- 37:37It's, it's, it's, it could be, but it could be included in, you know, the transit dependencies, right?
- 37:43If it gets, if it gets added to a slightly more legitimate package or, you know, worked up the chain that way, then, then yes, eventually, you know, you'll be running it unknowingly.
- 37:54Yeah.
- 37:55I think there's two important things we should talk about this before we move on, because there are some interesting ways in which you might unknow it.
- 38:02You might even try to do the right thing and you might actually shoot yourself in the foot by doing so.
- 38:07So number one, these like super strict lock files are awesome when you're building an application.
- 38:14I want to ship Talk Python training out.
- 38:16It's got its strict APIs.
- 38:17It runs on this version.
- 38:19It uses that version of Pydantic, that version of Beanie and whatever.
- 38:22Yeah.
- 38:22I want that to be fixed, fixed, zero flexibility until I decide through maybe a pip compile upgrade or whatever.
- 38:29I want a new one.
- 38:30However, if I was building a library that someone else was using, I would do them many headaches and a disservice to say, I depend on Pydantic 2.7.0.
- 38:41You're like, well, my other library needs Pydantic 8, 2.8.
- 38:45Right.
- 38:46And I can't use it and your library together.
- 38:48Right.
- 38:49So you need the, it's, it's a different story when you're building a library that others are going to consume than it is when you're building an application.
- 38:56And there was some, some disagreement, I guess, about the recommendation of pipenv for a while.
- 39:01And it's because I believe that pipenv is really focused on the application side.
- 39:05And it, I don't think it was made super clear that maybe it doesn't make as much sense for libraries.
- 39:10Right.
- 39:10So you want to speak to that a little?
- 39:11Yeah.
- 39:12Yeah.
- 39:12I'm, I'm an advocate for lock files for everyone.
- 39:16Right.
- 39:16Applications for sure, but also libraries and their developers.
- 39:20Right.
- 39:21Cause you know, if when you, when you, when you distribute a library, sure.
- 39:27You know, loose dependencies is, is probably the way to go there.
- 39:31But library developers, people who want to contribute to your projects, the developers themselves, maybe you work on a team.
- 39:38Having, having a lock file alongside your library is still going to be useful.
- 39:45Right.
- 39:46Like.
- 39:46Yeah.
- 39:46Cause that way you can say everyone, if somebody makes a change or they report a bug or whatever.
- 39:50Yeah.
- 39:50They're not bringing in a change from a different version of a dependency or like maybe something changed.
- 39:55Right.
- 39:55Yes.
- 39:56Yes.
- 39:56Yeah.
- 39:57and then, and it, plus it still allows you to, start from a known good spot.
- 40:03And then, maybe, maybe if you, if you know, you want to get the latest, then you can do it in a controlled environment, you know, like a sandbox or maybe a, on CI, you know, in a, in a throwaway runner that has no access to any, any secrets.
- 40:20Or, sensitive.
- 40:22sensitive.
- 40:23That's interesting.
- 40:24I hadn't really thought about having a specific requirements lock file type of thing for the libraries that I've been working on for the developers, right?
- 40:34For people who want to contribute.
- 40:35because it's just been like a loose requirement so that people that built against it aren't pinned into some very specific thing.
- 40:42But yeah, that makes a lot of sense.
- 40:43I think.
- 40:43Yeah.
- 40:43There's a, there's a link in that blog post.
- 40:46It's kind of dated now, but it's from the folks who built yarn, you know, JavaScript ecosystem.
- 40:51But, they had, they say it a lot more eloquently than I can.
- 40:55yeah, that's the one, lock files should be committed on all projects.
- 41:00Yeah.
- 41:00It's, I mean, it's a bit old now, but, but they, they go down the lists and spell it out a lot more clearly than me.
- 41:06And that's why, libraries even can benefit from, from publishing a lock file.
- 41:12Yeah.
- 41:12People can check that out.
- 41:13That's cool.
- 41:14Yeah.
- 41:14Yeah.
- 41:14And Java, that's the JavaScript package manager.
- 41:16So in JavaScript years, like a hundred years or something that's been a couple of years.
- 41:20That's right.
- 41:20Yeah.
- 41:21You got dog years.
- 41:22You got JavaScript years.
- 41:23JavaScript years just tick by like second, the second hand.
- 41:26Yeah.
- 41:26Yeah.
- 41:27All right.
- 41:27cool.
- 41:28So I see we're making great progress through our list of things to talk about here.
- 41:32I've gone through three and I like 15 left.
- 41:35We'll have plenty of time.
- 41:36so yeah, let's see.
- 41:41So another one, another pep, I think we're talking about here is 517, a build system independent
- 41:48format for source trees.
- 41:49I have no idea what this is.
- 41:51What is this?
- 41:51Yeah.
- 41:51Pep 517 and 518 kind of go together.
- 41:54This is, this was like the transition away from setup.py towards pyproject.toml.
- 42:00518 is the one that specifies pyproject.toml.
- 42:04kind of things that go in it.
- 42:07And then five, 517 is all about, build systems and build backends.
- 42:12so, so like in your pyproject.toml and your, in your, in your, build system
- 42:19key, you know, you'll often see things like, poetry core or flit or hatchling or,
- 42:26these kinds of things.
- 42:26And, and so it's five, PEP 517 is, is specifying what it means to be one of those build backends.
- 42:33it's really just defining two mandatory hooks.
- 42:37What does it mean to build wheel and build sdst?
- 42:40there's three optional hooks as well.
- 42:43And I think there's even another PEP that followed on from this that talks about, building
- 42:47editable, packages or, or, right.
- 42:51The dash, the dash E equivalents.
- 42:54Yeah.
- 42:54Yeah, exactly.
- 42:56but really it just boils down to, defining a way to build a wheel and build
- 43:01a source distribution.
- 43:02Yeah.
- 43:03And this is part of what opened up all the different choices we now have for package management and
- 43:09things like that.
- 43:10Right.
- 43:10Cause now there's a common way they can all work together a little bit like WSGI.
- 43:14Yes.
- 43:15Yeah.
- 43:15Yeah.
- 43:16I've been using hatchling for my build back in recently and it's been working real nicely.
- 43:20Okay.
- 43:20Yeah.
- 43:21I, I was just looking at hatchling the other day and they've got, yeah, yeah.
- 43:27They, they're one of the, they're one of the build backends that offers, build hooks,
- 43:31which, you know, so prior to, pipe project.toml and, and, and, and, wheels
- 43:41and beat us wheels and you go back to the source distributions and your setup.py files where
- 43:46it's just Python code.
- 43:47You can be, you can be doing anything in your setup.py file, which runs when you
- 43:53install the package.
- 43:53well now we're starting to see, you know, methods to do the same thing in these, in these
- 43:59more modern packaging or build backends.
- 44:01So like hatch has their, build hooks, build system hooks where you can, you can, you
- 44:07can, you can, point it to think, yeah, just Python code and have it, have it run as
- 44:13part of the build.
- 44:14Yeah.
- 44:15At least it only runs at build time, not install time.
- 44:17right.
- 44:19Yeah.
- 44:20I'm looking at the documentation now.
- 44:22I, I, I, yeah, this is still new to me, but there might be hooks for, for install as well.
- 44:29Okay.
- 44:29While you're thinking about it, one of the things I got a couple of questions I want to highlight
- 44:34from the audience here, but also one of the, one of the things that I think maybe was considered,
- 44:41I have no awareness of this, but if it wasn't would be excellent is what if the people at
- 44:46pip just pre-computed all that metadata from, at least for the common platforms that you would
- 44:53get that pip needs to download, run, set up pie, and then throw it away just to get that
- 44:58data.
- 44:59Like for Mac, Windows, and Linux, you know, if it would just go, okay, we're just going
- 45:03to like, as you upload it, it would just kick off a job that does that on those three platforms
- 45:08and put it in a JSON blob.
- 45:09Yeah.
- 45:10It seems like that would be worthwhile.
- 45:11I, I, I'm fairly certain there's discussions already around that type of a solution and
- 45:17maybe even a PEP for proposal, for it, but, yeah, getting away from having to build
- 45:23a package just to get metadata.
- 45:24yeah.
- 45:25You got packages that are downloaded billions of times with a B it's insane.
- 45:32And if somebody could do that three times instead of a billion times, it would make
- 45:37it work faster and it would also make it safe.
- 45:39Right.
- 45:39I think it'd be great.
- 45:40Yeah.
- 45:40All right.
- 45:41A couple of questions here.
- 45:42this one.
- 45:45So Tony on the audience says pip compile is great for finding your transitive dependencies.
- 45:50One interesting thing that they've done is package up code with pants build, which supports
- 45:56locks files just to look through what code gets packaged up.
- 45:59Is this anything you've explored?
- 46:02I've heard of pants.
- 46:03I haven't looked into it myself yet.
- 46:05Mm-hmm.
- 46:06okay.
- 46:07Yeah.
- 46:07Yeah.
- 46:07So just use it like, go, okay, you're going to have to build this thing and give me a little
- 46:11manifest and whatnot.
- 46:13And then we can just look at that.
- 46:14That's cool.
- 46:14Yeah.
- 46:14And then Tamir says, do you have a solution for taking already locked dependencies with you
- 46:19when you start a new app?
- 46:21I'm guessing, you know, maybe, yeah, I don't know.
- 46:24I, I guess maybe you've already got a project you're working on.
- 46:26You want to say like, I want this project to use that.
- 46:28Probably you could just copy the lock file.
- 46:30Right.
- 46:31Yeah.
- 46:31Yeah.
- 46:32Yeah.
- 46:32If you, I mean, if you really, I mean, really you're going to, if you start a new project,
- 46:36um, or new application, you're going to, you're going to have new, manifest file, you know,
- 46:42pyproject.toml, maybe you have the same dependencies, the top level dependencies or not,
- 46:46but the fully resolved set of dependencies that makes up your lock file, that, that, that,
- 46:51that can very easily be different.
- 46:52So I'm, I'm not exactly sure how you just poured over one to another.
- 46:58One more bit from Tony.
- 46:59This is, something that I now remember from pants is this, if it just looks through
- 47:05your code and if you use the import statement, regardless of whether you've put it in your
- 47:09requirements files, it'll figure out what your requirements file should have been.
- 47:13If you were a bad developer, basically.
- 47:16That's kind of cool.
- 47:18Just to see what it uses.
- 47:19Yeah.
- 47:19Nice.
- 47:20All right.
- 47:20onto the next thing.
- 47:22Specifying PEP 518, specifying minimum build system requirements for Python projects.
- 47:28Yeah.
- 47:28I'm guessing related.
- 47:29This is pyproject.toml.
- 47:31This is the, this is the PEP for that.
- 47:33Okay.
- 47:34There's not much to it other than to say that they've settled on that name, rejecting a bunch
- 47:39of other possibilities.
- 47:40And then they've got the, you know, the few entries that are required, like for your,
- 47:45your finding your build system.
- 47:46Excellent.
- 47:46Yeah.
- 47:47Yeah.
- 47:47You don't have to have a pyproject.toml for Python, but.
- 47:50No.
- 47:51Yeah.
- 47:51If you're building a Python library and you don't want to use setup.py, then you're much
- 47:56better off having a pyproject.toml, right?
- 47:58Yes.
- 47:59Yeah.
- 47:59Yeah.
- 48:00Yeah.
- 48:00Yeah.
- 48:00It's more in the library side that it, I mean, it's not that you can't use it on an application,
- 48:04but it's more required on the library side.
- 48:07Yeah.
- 48:07That's the thing.
- 48:09All right.
- 48:09So let's talk about some of the ways in which your packages might go wrong.
- 48:14We've already talked about typosquatting and we also talked about everything that's different.
- 48:18Yeah.
- 48:19But yeah, typosquatting is, it is tricky.
- 48:23I think it's pretty well understood at this, this point, but maybe just tell people real
- 48:27quick to cover that base, you know?
- 48:29Sure.
- 48:29Typosquatting is, is, you know, publishing a package with a name that's similar, but not
- 48:36the same as, as a, as a existing known good package.
- 48:39Right.
- 48:40So like, instead of requests, maybe you, you get request without the S or, you know, one
- 48:48that gets me cause I, cause I make the typo all the time was, is the cryptography package.
- 48:52Like, like if I, you know, if I put you on the spot, would you know how to spell cryptography?
- 48:57I always get the first couple of letters, you know, jumbled up a bit and, and there have
- 49:02been malicious packages published and then taken down with, with the, you know, spelled C-R-P-Y
- 49:10instead of C-R-Y-P cryptography.
- 49:12Right.
- 49:13Yeah.
- 49:13But, but the idea is that, you know you, you, you can overlook a package cause it looks like
- 49:20a, it looks like a good one.
- 49:21No, it's not necessarily that you're going to, you're going to install it because you type
- 49:25it wrong.
- 49:26although that is, that is, you know, one technique, right?
- 49:30The drive-by installs where someone just bat fingers, the package name.
- 49:34but really having a, typo squatted package is going to allow these threat actors to,
- 49:43be a little more stealthy in their inclusion of that package in, in legitimate, code
- 49:48reviews and commits and, dependencies of dependencies.
- 49:52Right.
- 49:52And so the other, the other thing that goes with typo squatting, I don't know if I had a link
- 49:57for you there yet, is, is star jacking.
- 50:00So, a lot of times if you're going to typo squat on a known good package, okay, there,
- 50:05there it is.
- 50:06you know, these, these, these threat actors, they just, they just straight up copy the
- 50:12known good project, right?
- 50:14It just cloned the repository and then changed the package name.
- 50:19and, and then when they, when they post the package to, PyPI, for instance,
- 50:26the metadata that goes with the package, still exists, right?
- 50:31So, on PyPI for a given package, you can see on the left-hand side, it shows like some,
- 50:36some statistics.
- 50:37If, if the, URL was given to like a GitHub.
- 50:42hosted project, for instance, it'll go in there and tell you how many stars.
- 50:48Right, right, right.
- 50:50You know, how many downloads.
- 50:51That's actually a signal that it seems like it should be good, right?
- 50:53It'll have.
- 50:54Yeah.
- 50:54A lot.
- 50:55And that's what star jacking is doing is just copying the metadata of a known good package.
- 51:01so that on first look.
- 51:04Yeah, there you go.
- 51:05You can see.
- 51:05Like I did pull up pytest and it says statistics, GitHub statistics, 11,000 stars, 2,000 forks.
- 51:12Okay, this is legit.
- 51:12Let's install it.
- 51:13Right.
- 51:14So I could go clone pytest repository right now, change the name to pytest spelled P-I-T-E-S-T.
- 51:20Mm-hmm.
- 51:21And then, and then push that to P-I.
- 51:23The math version of testing, yeah.
- 51:24And you're going to get these same statistics and you're going to get the same, maintainers
- 51:28that you see if you scroll down a little bit, in the, the, metadata.
- 51:34Yeah.
- 51:34So you get the maintainers list.
- 51:35All of that metadata that you, you, you enter in your pyproject.toml or setup.py file,
- 51:42gets read here on PyPI and just, just publish.
- 51:45So you can, you can fake people out.
- 51:48Yeah.
- 51:48Yeah.
- 51:48Yeah.
- 51:48That's actually really, okay.
- 51:51Well, there's a new terrifying thing that I hadn't thought about.
- 51:53Yeah.
- 51:53Yeah.
- 51:53So, so star jacking and typosquatting where you just take a known good package, clone it,
- 51:58and then maybe you, you make a change to, you know, existing function, you know, the
- 52:04function does what it's supposed to do, but it also does some other stuff like ship off
- 52:07secrets from your, your CI server or, you know, it could lay dormant and wait for,
- 52:14uh, some sort of production environment and grab some SSH queues or something terrible.
- 52:19Yeah.
- 52:20Yeah.
- 52:20Yeah.
- 52:20Yeah.
- 52:20That's, that's, that's the other, the other dependency confusion.
- 52:24Okay.
- 52:24That's the next one you've got up.
- 52:26Yeah.
- 52:26This is the one that we kind of talk, it's similar to what we talked about, before
- 52:30with, I can't remember, but I said there's, there's going to come back to this.
- 52:34So here, here it is again, this is a dependency confusion where, if you get the wrong version
- 52:39or the wrong name, it could actually, you try to be safe by having a white listed list or
- 52:45say, well, it's, it's, so this is one where it's the same, same package name, different source
- 52:51of where you acquire that package.
- 52:53Yes.
- 52:53So this is, you'll, these attacks are mostly like, companies, enterprises.
- 52:59This is the enterprise attack.
- 53:01Yeah.
- 53:02Yeah.
- 53:02So it's an artifactory and we, we only put our stuff there and we're, we're going to call
- 53:08it like, you know, international company underscore data access.
- 53:11That's right.
- 53:12And, and it's, and it's, and it's tricky because if you don't know, like if you don't
- 53:17have your build system set up in a way and then, your CI server set up in a way to
- 53:22install your dependencies in the proper order, like excluding public registries first and only
- 53:28looking for packages in your private registry, then it's very easy, especially with pip, which
- 53:34defaults to looking on pipe PI, the public registry first, and then only falling back to your, your
- 53:40extra index URL specifications.
- 53:42Secondly, that if you, if someone had the knowledge or just guessed at the package
- 53:50name that you had published on your internal registry, and then they made their own package
- 53:54with the same name, but put it on pipe PI, that's the one that's going to get installed.
- 53:58and there was like a whole series of, you know, bug bounties that were claimed over this
- 54:05back a few years ago, because people just went around, you know, guessing at internal package
- 54:10names or maybe they used to work there or new people.
- 54:13Yeah.
- 54:14Yeah.
- 54:14Yeah.
- 54:14I'll pay a hundred bucks just to share your quorum at Sot.txt with me.
- 54:18Right.
- 54:18Right.
- 54:19Right.
- 54:19Right.
- 54:20You know, it's, it's kind of, it's extra sneaky because it only affects people.
- 54:27It only affects people who are going out of their way to be more secure, right?
- 54:32They're going out of their way to say, we're only going to, we're going to actually set up
- 54:35a whole server and we're going to whitelist a bunch of stuff.
- 54:39You can only ask for the names of the things on this server.
- 54:41And, ah, yes.
- 54:43Yes.
- 54:43And that, that might still work if you limit it to your internal registry only or a mirror
- 54:49perhaps of, of the public registries.
- 54:53but it's pretty easy to create your own internal copy, download a bunch of external
- 55:00ones and mirror them locally and say like, these are the ones that are pre-approved at
- 55:04our company.
- 55:04Nothing else.
- 55:05Yeah.
- 55:06Yeah.
- 55:06I, I, I've worked in a environment where that's exactly what we did.
- 55:10And, I think there is merit to that.
- 55:13You just have to know that anything you're mirroring to the trusted internal network is in
- 55:19fact secure.
- 55:20You know?
- 55:21Yeah.
- 55:21Yeah.
- 55:22For sure.
- 55:22I think it doesn't really make sense except for a few very rare cases to say you cannot
- 55:29use external dependencies.
- 55:30Right.
- 55:31Right.
- 55:31You're just saying what we want is to not build software, but while the rest of the world
- 55:36does, you know?
- 55:38Yeah.
- 55:38Because that's part of the magic.
- 55:39We just saw there's over half a million libraries you can choose from.
- 55:42When you say we, we have zero of those, you're really, really constraining the type of software
- 55:48and the velocity at which you can build.
- 55:51Yeah.
- 55:52Yeah.
- 55:53Yeah.
- 55:53It reminds me of, there's that line, you know, like why, why do you rob banks?
- 55:58Cause they have the money.
- 56:00Cause that's where the money is.
- 56:01Right.
- 56:01It's like, well, why do attackers, why are attackers going after open source software now?
- 56:05Like, well, that's, that's where it's easiest to get arbitrary code to run.
- 56:11That's where developers are.
- 56:12That's what, to be fair though.
- 56:14It's not only, it's not only right.
- 56:16There's solar winds, which really had almost nothing to do with open source, but it had
- 56:20to do with CI, CD systems and other sneakiness.
- 56:23Right.
- 56:23Yeah.
- 56:23Yeah.
- 56:24Yeah.
- 56:24And got into places that, you know, instead of getting into libraries, you get into the
- 56:29build system and you just give it a little extra, a little extra include tag there.
- 56:33Bringing that deal out, like you said, right.
- 56:37So dependency and confusion is sneaky because you're asking for a local version off a local
- 56:42server.
- 56:42It doesn't exist on PyPI, but if it could be made to exist on PyPI, all of a sudden that
- 56:47gets installed.
- 56:48That's potentially, that's not good.
- 56:50Potentially.
- 56:50Yeah.
- 56:50Yeah.
- 56:51It's, it's, that's, that's how it works in all the, in all the default cases.
- 56:54And it's, it's pretty tricky actually to, to exclude, to do it in the correct order and
- 56:59exclude those public registries.
- 57:01Yeah.
- 57:01What's what I do to help this is I just, I just run the UUID command to get one of those
- 57:0816 digit arbitrary X things.
- 57:10And I just name all my libraries that.
- 57:12And so it's like, oh, you have the F3DC.
- 57:16Yeah.
- 57:16That's the, that's the API one.
- 57:18That's right.
- 57:18That's important.
- 57:19That, right.
- 57:20No one is going to do this.
- 57:21It's such a safe space.
- 57:23I tell you.
- 57:23All right.
- 57:25On to the next one.
- 57:26That, that would work.
- 57:28Expired author domains.
- 57:30This is super sneaky.
- 57:32Yeah.
- 57:33Yeah.
- 57:33So this is one, you know, it, it might be less of a factor now.
- 57:41I think, I think it was just earlier this month that PyPI enforced two factor authentication
- 57:46for all their users.
- 57:48But a lot of sites and, you know, even PyPI, I think before this month have, you know,
- 57:59password reset features where if, if you lose access to your account or you forget your password,
- 58:04just, you know, send me an email and reset your password.
- 58:07But it's, it's, it's very possible that people, you know, years ago submitted a package.
- 58:13They, they don't maintain it anymore.
- 58:15They submitted it under an old email account that has expired.
- 58:20Right.
- 58:20Maybe they, they had some domain.
- 58:22Yeah.
- 58:23Special.
- 58:23It doesn't work that well for Gmail or Outlook.
- 58:26Right.
- 58:27Right.
- 58:27If you had.
- 58:28Custom domain.
- 58:28If you had a custom domain and, as would be awesome, have your own, you know, Michael at
- 58:34talkpython.fm, that kind of thing.
- 58:36Yeah.
- 58:36Yeah.
- 58:37Say you, you win the lottery and, and, you know, decide to quit your pay job.
- 58:42Yeah.
- 58:42And then you let your domain expire and, well, maybe there's still a linkage for the talkpython
- 58:48domain to PyPI.
- 58:50And then I go and buy that domain and, you know, request a password.
- 58:54Set up the server.
- 58:54Yeah.
- 58:55Account reset.
- 58:56Set up email.
- 58:56Yeah.
- 58:56And then now I, now I can publish new versions of, of the, of the packages there.
- 59:02Yeah.
- 59:02Yeah.
- 59:03It's not good.
- 59:03Yeah.
- 59:04Yeah.
- 59:04So I don't really know what to do about that one, but there's an amazing, there's an amazing
- 59:09joke that I found on Mastodon.
- 59:11Somebody posted, sit here.
- 59:13It's two big red buttons.
- 59:17Think Ren and Stimpy or whatever.
- 59:18And one of the red buttons says, admit to yourself that your dream is dead.
- 59:22The other one says pay $12 for domain renewal.
- 59:26Right.
- 59:27I mean, it's funny, but there's plenty of people who will get a domain and I totally go in and
- 59:33then it's like, you know what?
- 59:33I haven't done anything without five years.
- 59:35I'm not paying another 12 bucks.
- 59:36But if they had set up an account under that, right?
- 59:39This is what you're talking about.
- 59:40Yeah.
- 59:41Yeah.
- 59:41Yeah, exactly.
- 59:42Yeah.
- 59:43That's why you got to buy your domains for that hundred year renewal period.
- 59:47Exactly.
- 59:48Take out that loan.
- 59:50You get to the loan.
- 59:51All right.
- 59:54We're getting short on time here.
- 59:55I want to, let me, let's just go through, I'll just list off a few real quick.
- 59:59Maybe we do lightning round.
- 1:00:00Okay.
- 1:00:00Okay.
- 1:00:00Unverifiable dependency.
- 1:00:01Okay.
- 1:00:02These are for specifying dependencies that are not necessarily published to PyPI, right?
- 1:00:11So that maybe you're pointing to a GitHub repository.
- 1:00:15You know, pip calls these VCS project URLs, you know, if you look in their help output.
- 1:00:22Yeah.
- 1:00:22It's like pip install git plus HTTP to a thing that has a PyProject autonomous.
- 1:00:27Yeah.
- 1:00:27And that thing, it can point to a repository.
- 1:00:31Maybe it points to a tag.
- 1:00:32Maybe it points to a branch.
- 1:00:34None of that is stable, right?
- 1:00:37Like you, the tag could change out from under you.
- 1:00:40The code that's related to that tag could change out from under you.
- 1:00:44The code at the branch you're pointing to could change while the name remains the same.
- 1:00:49So, you know, those are risky for that reason, right?
- 1:00:52If you're not pinning to a very specific version or a very specific hash, right?
- 1:00:56If you're going to point to a repository or a git URL.
- 1:00:59Interesting.
- 1:01:00Yeah.
- 1:01:00Yeah.
- 1:01:00Make sure it's true.
- 1:01:01I've gotten to feel a lot of times like the hash is maybe a little bit redundant given the immutability of PyPI.
- 1:01:06But if you're pointing at something like this, then maybe all of a sudden you really do want that, right?
- 1:01:11Yes.
- 1:01:11For sure.
- 1:01:11Yeah.
- 1:01:12Okay.
- 1:01:12Repo jacking?
- 1:01:15Yeah.
- 1:01:15This is similar to the expired author domain, right?
- 1:01:20So if someone was pointing to one of those git dependencies, a VCS project URL, as pip calls it, and that account went dormant or expired, relinquished, whatever, and someone else took it over, then yeah, they can now dictate.
- 1:01:39What's there, yeah.
- 1:01:40Yeah, exactly.
- 1:01:41A lot of people are acquiring.
- 1:01:42All right.
- 1:01:44And then maybe last bit, get a chance to talk a bit about your Phylum CI project.
- 1:01:50I do want to point out really quick, though, that Phylum was a sponsor of the show.
- 1:01:54Yes.
- 1:01:55A while ago.
- 1:01:55But this is not a sponsored episode.
- 1:01:57This is just you and I had been talking prior to that, actually, and decided to put this show together.
- 1:02:03So just to be clear, but let's talk about this project you guys got anyway.
- 1:02:06Yeah.
- 1:02:07Yeah.
- 1:02:08So you can pip install Phylum right now, or like I prefer, pipx install Phylum.
- 1:02:14Yeah.
- 1:02:14I love pipx.
- 1:02:15It's awesome.
- 1:02:16Yeah, me too.
- 1:02:17Yeah.
- 1:02:17I think I heard about it from you, actually.
- 1:02:19So the circle goes.
- 1:02:23Yes, yes.
- 1:02:23So this package, it does two main things.
- 1:02:26One is it can, it'll expose us to entry points.
- 1:02:30One of them is called Phylum init, and that'll get you the Phylum command line interface written in Rust, but installed with Python.
- 1:02:40It'll get you the Phylum CLI locally.
- 1:02:45And then the other one is called Phylum CI.
- 1:02:48That's just a catch-all entry point, the thing that gets exposed through our Docker container.
- 1:02:53to handle almost all of our integrations.
- 1:02:56So if you want to monitor your PRs on GitHub, for instance, we've got an integration for that.
- 1:03:03Nice.
- 1:03:04So the idea is basically that I could set this up in GitHub.
- 1:03:07A PR comes in, I could set up an action.
- 1:03:09Phylum will scan it for known mischievousness.
- 1:03:13That's right.
- 1:03:14And make that part of the PR, maybe even block it out, right?
- 1:03:17Yeah, exactly.
- 1:03:18It'll fail your build if you don't pass your default policy or established policy on any of your given lock files or manifests.
- 1:03:28We deal with manifests as well.
- 1:03:30And you mentioned GitHub.
- 1:03:31So even with GitHub, we went a step further.
- 1:03:33We have an app as well.
- 1:03:34So you don't even have to modify a workflow.
- 1:03:36You could just install a GitHub app and automatically monitor your repositories.
- 1:03:42But a lot of the other ecosystems don't have that.
- 1:03:46So we just provide Docker containers.
- 1:03:49I love the Docker container.
- 1:03:51So use Docker run against your code or whatever.
- 1:03:54So yeah.
- 1:03:56And then there's even a pre-commit hook we expose as well.
- 1:04:02Nice.
- 1:04:03I genuinely don't know the answer to this question.
- 1:04:06Does this cost money?
- 1:04:07No.
- 1:04:08We have anyone.
- 1:04:10Anyone can sign up for free.
- 1:04:12There's a community edition where you can have up to five projects.
- 1:04:17Okay, cool.
- 1:04:18You guys have to eat.
- 1:04:19There must be some way you charge for something.
- 1:04:21Oh, exactly.
- 1:04:21Yeah, yeah.
- 1:04:22So there's the paid version, right?
- 1:04:24Which, you know, unlimited projects.
- 1:04:26You get access to group-based management.
- 1:04:29You know, there's a few extra features.
- 1:04:30It's a freemium model.
- 1:04:32More of a Teams enterprise-y angle.
- 1:04:34Yeah, yeah.
- 1:04:35But for this audience, I mean, I would love if everyone just went that little extra step of securing their open source software and, you know, go with the free option.
- 1:04:46I'm not trying to sell you anything here.
- 1:04:47You know, monitor your manifest, your lock files.
- 1:04:52Make sure that you remain secure.
- 1:04:55You're not exposing your secrets because that's what we're finding now is that developers are the new high-value targets.
- 1:05:01Yeah.
- 1:05:03That's what attackers want to go after because we know that developers, they have the secrets.
- 1:05:07They've got the keys, you know.
- 1:05:09We write the code that then gets run on the production server inside the firewalls.
- 1:05:15Yeah.
- 1:05:15Yeah.
- 1:05:16We have all the access, all the secrets, all the keys.
- 1:05:20So, you know, if you can find a way to get arbitrary code from strangers to run on developer systems, you're going to have a much better chance.
- 1:05:28We have a good time.
- 1:05:29Yeah.
- 1:05:29We have a good time.
- 1:05:30By that, I mean having a bad time.
- 1:05:32Right.
- 1:05:33Yeah.
- 1:05:33Doing bad things.
- 1:05:35Okay.
- 1:05:35Let's not do that.
- 1:05:36Awesome.
- 1:05:37Well, excellent work.
- 1:05:38I think probably we'll kind of just leave it there.
- 1:05:40We're pretty much out of time for the rest of the stuff.
- 1:05:42But close it out for us, Charlie.
- 1:05:44People are maybe both have a few new tools to work with, but also techniques, but maybe also a little freaked out.
- 1:05:51What do you tell them?
- 1:05:51I recommend everyone to restrict their use of dependencies to lock files and then carefully gate or guard the inclusion of new lock files or updates of existing ones.
- 1:06:04Or sorry, dependencies in those lock files with careful analysis.
- 1:06:08Don't allow arbitrary code to run anywhere in your development process and give FileM a try.
- 1:06:13We've got the free community edition.
- 1:06:15We will provide that analysis and ensure that you don't have malware running on your system through bad dependencies.
- 1:06:22Awesome.
- 1:06:23All right.
- 1:06:23Well, it's been very interesting and a lot of new things to think about.
- 1:06:27So thanks for being here.
- 1:06:28Thank you, Michael.
- 1:06:28Yep.
- 1:06:29See you later.
- 1:06:29This has been another episode of Talk Python To Me.
- 1:06:33Thank you to our sponsors.
- 1:06:35Be sure to check out what they're offering.
- 1:06:36It really helps support the show.
- 1:06:38Take some stress out of your life.
- 1:06:40Get notified immediately about errors and performance issues in your web or mobile applications with Sentry.
- 1:06:45Just visit talkpython.fm/sentry and get started for free.
- 1:06:50And be sure to use the promo code talkpython, all one word.
- 1:06:54Mailtrap, an email delivery platform that developers love.
- 1:06:58Use their email sandbox to inspect and debug emails in staging, dev, and QA environments before sending them to recipients in production.
- 1:07:06Try Mailtrap for free at talkpython.fm/Mailtrap.
- 1:07:10Want to level up your Python?
- 1:07:11We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:07:15Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:07:20And best of all, there's not a subscription in sight.
- 1:07:23Check it out for yourself at training.talkpython.fm.
- 1:07:26Be sure to subscribe to the show.
- 1:07:28Open your favorite podcast app and search for Python.
- 1:07:31We should be right at the top.
- 1:07:32You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:07:42We're live streaming most of our recordings these days.
- 1:07:45If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:07:52This is your host, Michael Kennedy.
- 1:07:54Thanks so much for listening.
- 1:07:56I really appreciate it.
- 1:07:57Now get out there and write some Python code.
- 1:07:59I'll see you next time.