Skip to content

Replace password in URI by stars if present to avoid leaking secrets in logs #1198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

mickours
Copy link
Contributor

Currently, if you use a git URI containing a password like https://username:pass@git.example.com/project and an exception occurs during a git clone the password is printed in the logs. It also happen when the logging is at debug level.

To avoid secrets to leak in the logs, this patch replace the password by a fix amount of stars (******).

@mickours mickours marked this pull request as draft March 12, 2021 07:39
@Byron
Copy link
Member

Byron commented Mar 13, 2021

Thanks a lot for the initiative, it's much appreciated.

My early feedback is that the URL parsing is probably prone to failures even though it should work for most URLs. To be save, one would probably catch parsing exceptions.
Maybe it would be easier to use an existing URL parser and/or serializer if one exists in the python standard library.

@mickours
Copy link
Contributor Author

Thanks for the feedback! I've change the implementation to use urllib and handle parsing errors.

I'm still unable to make the test works on the CI despite the fact that it works locally. It seems that the failing command raise an assert which is not in my test. The problem is that I need the exception to be raise in order to check if the password is inside. Any clue on that?

@Byron
Copy link
Member

Byron commented Mar 15, 2021

Thanks for the update!

The failing test mentions the entire command-line, here it is:

 /usr/bin/git clone -v --progress ***fakerepo.example.com/testrepo /tmp/test_leaking_password_in_clone_logsam5xqh6q'

It looks like a replacement has happened even though I would expect an additional *, the password has been removed but it still seems to see the password in the error message. Since finalize_proc is only handed the process proc, this really only works if the args list is editable. The latter might be the case in some python versions, but not in others.

Something you could try is to reassign proc.args entirely. If that works, I have a few more requests just to be very very safe 😅.

Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

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

Thanks a lot for bearing with me, and good luck with the CI issue. The test succeeds for me as well when running it locally.

@mickours mickours force-pushed the replace-password-in-uri-by-stars branch 3 times, most recently from 28b8a76 to ab1f86e Compare March 15, 2021 17:45
@mickours mickours force-pushed the replace-password-in-uri-by-stars branch from ab1f86e to 50cbafc Compare March 15, 2021 17:48
@mickours mickours marked this pull request as ready for review March 15, 2021 17:52
@mickours
Copy link
Contributor Author

I track down every occurrence of the command line in the logs and it seems to be OK now. With this patch it will on every commands try to redact password out of the command line before logging it.

@mickours mickours marked this pull request as draft March 16, 2021 06:55
@mickours
Copy link
Contributor Author

I just realize that we do not have a succesfull clone test and when I tried it manually it's not working because of the in-place replacement. I've to rework this.

@mickours mickours force-pushed the replace-password-in-uri-by-stars branch from 2a23db3 to 12692e1 Compare March 16, 2021 09:14
@mickours mickours force-pushed the replace-password-in-uri-by-stars branch from 12692e1 to ffddedf Compare March 16, 2021 09:16
@mickours mickours marked this pull request as ready for review March 16, 2021 09:34
@mickours
Copy link
Contributor Author

Ok, I've change the in-place password replacement with a copy implementation which avoid side effects on the command line arguments. Also, I've added a successful git clone with a password from an empty project.

Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

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

Thanks a lot, this looks so much better already, great work! It's particularly helpful to see more tests, as now there is one in particular for redacting passwords in command-lines. I bet this helped during development.

Please excuse the use of the word 'nicer' in the line comments below, I simply lacked a more scientific explanation so I guess it comes down to 'taste'. This is not to be understood as judgement towards your code, it's just as correct and since it's Python you could make a point towards the current version being idiomatic and I would go with it.

Let's get this one to the finishing line :) 🏇

assert password not in str(err), "The error message '%s' should not contain the password" % err
# Working example from a blank private project
Repo.clone_from(
url="https://gitlab+deploy-token-392045:mLWhVus7bjLsy8xj8q2V@gitlab.com/mercierm/test_git_python",
Copy link
Member

Choose a reason for hiding this comment

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

This might be a danger as it breaks the self-isolation even more (after all, GitPythons test rely on its own repository already), but I think it's OK to go with it and fix it when it breaks.
Maybe it never does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree! I simply did not find another way to test the working use case. Maybe we can use a deploy token with the GitPython repository on Github but I think it is not available for public repo. Maybe you can create a private repo inside the gitpython-developers account in order to keep the responsibility in the same place?

Copy link
Member

Choose a reason for hiding this comment

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

I thought about that too and will do that when it breaks. It's certainly the lazy way of doing things and it's basically a mine that is primed to blow sometime in the future, but… it's going to be okay ;).

While writing this I thought to myself that I don't want to be that lazy and took a look at the github ways of doing this. It turns out it only supports repository deploy keys, which indeed are full blown ssh keys that I don't want to deal with right now. This makes your account one of the pillars of the happiness of GitPython's CI, something not everyone can say about themselves :D.

@mickours
Copy link
Contributor Author

mickours commented Mar 18, 2021

Thanks a lot, this looks so much better already, great work! It's particularly helpful to see more tests, as now there is one in particular for redacting passwords in command-lines. I bet this helped during development.

You're right! :)

Please excuse the use of the word 'nicer' in the line comments below, I simply lacked a more scientific explanation so I guess it comes down to 'taste'. This is not to be understood as judgement towards your code, it's just as correct and since it's Python you could make a point towards the current version being idiomatic and I would go with it.

No problem, I know that sometimes it's a matter of beauty ;)

Let's get this one to the finishing line :) horse_racing

Thanks a lot for all you feedback!! 🏁

Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

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

And it's done :)! Thanks again, I think it was worth it, too!

@Byron Byron merged commit d1297f6 into gitpython-developers:master Mar 19, 2021
@Byron Byron added this to the v3.1.15 - Bugfixes milestone Mar 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants