Skip to content

Commit 5904448

Browse files
authored
Avoid exposing password and token from git repositories (#105220)
Try to detect if the git remote URL has a password or a Github token and return an error teaching the user how to avoid leaking their password or token.
1 parent 99a2354 commit 5904448

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

llvm/cmake/modules/VersionFromVCS.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ function(get_source_info path revision repository)
3939
OUTPUT_VARIABLE git_output
4040
ERROR_QUIET)
4141
if(git_result EQUAL 0)
42+
# Passwords or tokens should not be stored in the remote URL at the
43+
# risk of being leaked. In case we find one, error out and teach the
44+
# user the best practices.
45+
string(REGEX MATCH "https?://[^/]*:[^/]*@.*"
46+
http_password "${git_output}")
47+
if(http_password)
48+
message(SEND_ERROR "The git remote repository URL has an embedded \
49+
password. Remove the password from the URL or use \
50+
`-DLLVM_FORCE_VC_REPOSITORY=<URL without password>` in order to avoid \
51+
leaking your password (see https://git-scm.com/docs/gitcredentials for \
52+
alternatives).")
53+
endif()
54+
# GitHub token formats are described at:
55+
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-authentication-to-github#githubs-token-formats
56+
string(REGEX MATCH
57+
"https?://(gh[pousr]|github_pat)_[^/][email protected].*"
58+
github_token "${git_output}")
59+
if(github_token)
60+
message(SEND_ERROR "The git remote repository URL has an embedded \
61+
GitHub Token. Remove the token from the URL or use \
62+
`-DLLVM_FORCE_VC_REPOSITORY=<URL without token>` in order to avoid leaking \
63+
your token (see https://git-scm.com/docs/gitcredentials for alternatives).")
64+
endif()
65+
4266
string(STRIP "${git_output}" git_output)
4367
set(${repository} ${git_output} PARENT_SCOPE)
4468
else()

0 commit comments

Comments
 (0)