Skip to content

Commit e33f1a1

Browse files
authored
Use host check instead of substring search for GitLab URLs (#242)
1 parent 6edd81a commit e33f1a1

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

‎src/Package_Command.php‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function install( $args, $assoc_args ) {
283283
'insecure' => $insecure,
284284
];
285285
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
286-
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
286+
$headers = $gitlab_token && 'gitlab.com' === strtolower( (string) Utils\parse_url( $package_name, PHP_URL_HOST ) ) ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
287287
$response = Utils\http_request( 'GET', $package_name, null, $headers, $options );
288288
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
289289
@unlink( $temp ); // @codingStandardsIgnoreLine
@@ -1115,8 +1115,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
11151115
// Fall back to GitLab URL if we had no match yet.
11161116
$url = "https://gitlab.com/{$package_name}.git";
11171117
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
1118-
$headers = $github_token ? [ 'Authorization' => 'token ' . $github_token ] : [];
1119-
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
1118+
$headers = $gitlab_token ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
11201119
$response = Utils\http_request( 'GET', $url, null /*data*/, $headers, $options );
11211120
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
11221121
return $url;
@@ -1467,7 +1466,7 @@ private function check_github_package_name( $package_name, $version = '', $insec
14671466
* to false.
14681467
*/
14691468
private function check_git_package_name( $package_name, $url = '', $version = '', $insecure = false ) {
1470-
if ( $url && ( ( strpos( $url, '://gitlab.com/' ) !== false ) || ( strpos( $url, 'git@gitlab.com:' ) !== false ) ) ) {
1469+
if ( $url && ( ( 'gitlab.com' === strtolower( (string) Utils\parse_url( $url, PHP_URL_HOST ) ) ) || ( 0 === strpos( $url, 'git@gitlab.com:' ) ) ) ) {
14711470
$matches = [];
14721471
preg_match( '#gitlab.com[:/](.*?)\.git#', $url, $matches );
14731472
return $this->check_gitlab_package_name( $matches[1], $version, $insecure );

0 commit comments

Comments
 (0)