Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docker_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func dockerImageAuth(ctx context.Context, image string, configs map[string]regis
defaultRegistry := defaultRegistryFn(ctx)
reg := core.ExtractRegistry(image, defaultRegistry)

if reg == "docker.io" {
reg = defaultRegistry
}
Comment on lines +45 to +47
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Fix correctly remaps docker.io to canonical registry address.

The remapping ensures credential lookup uses the canonical registry address (https://index.docker.io/v1) where Docker stores authentication data, addressing the stated issue.

Please verify the following edge cases:

  1. Case sensitivity: Should the comparison be case-insensitive? Docker registries treat hostnames as case-insensitive.
  2. Other Docker registry aliases: Does registry-1.docker.io or other aliases require similar remapping?

Consider extracting the hardcoded string to a constant for better maintainability:

+const dockerIORegistry = "docker.io"
+
 func dockerImageAuth(ctx context.Context, image string, configs map[string]registry.AuthConfig) (string, registry.AuthConfig, error) {
 	defaultRegistry := defaultRegistryFn(ctx)
 	reg := core.ExtractRegistry(image, defaultRegistry)
 
-	if reg == "docker.io" {
+	if reg == dockerIORegistry {
 		reg = defaultRegistry
 	}

🏁 Script executed:

#!/bin/bash
# Verify if core.ExtractRegistry normalizes case and check for other registry aliases

echo "=== Searching for registry extraction logic ==="
rg -n -C5 'func ExtractRegistry' 

echo -e "\n=== Searching for other docker.io references ==="
rg -n -C3 'docker\.io|registry-1\.docker\.io' --type go -g '!*_test.go'

echo -e "\n=== Checking for registry-related constants ==="
rg -n -C3 'const.*[Rr]egistry|IndexDockerIO' --type go

Length of output: 7590


Now I need to check the actual implementation of ExtractRegistry and see if there are test cases demonstrating the expected behavior.


🏁 Script executed:

#!/bin/bash
# Check full ExtractRegistry implementation
echo "=== ExtractRegistry full implementation ==="
sed -n '95,120p' internal/core/images.go

echo -e "\n=== Check how dockerImageAuth is tested with different registry cases ==="
rg -n -A 10 -B 2 'dockerImageAuth' docker_auth_test.go | head -60

echo -e "\n=== Search for test cases with case variations ==="
rg -n 'docker\.io|Docker\.IO' docker_auth_test.go internal/core/images_test.go

Length of output: 1274


Fix addresses docker.io remapping but has exploitable gaps that need addressing.

The remapping correctly uses the canonical registry for credential lookup. However, the current implementation has two issues:

  1. Case sensitivity: The comparison reg == "docker.io" is case-sensitive, so images using mixed case (e.g., Docker.io/myimage) won't be remapped. Since ExtractRegistry doesn't normalize case, this could cause credential lookups to fail for such images.

  2. Incomplete alias coverage: The code only handles docker.io, but registry.hub.docker.com (visible in options.go:233) is another Docker Hub alias that should receive the same treatment.

Recommended fixes:

// Make comparison case-insensitive and handle both aliases
if strings.EqualFold(reg, "docker.io") || strings.EqualFold(reg, "registry.hub.docker.com") {
    reg = defaultRegistry
}

Or, consider normalizing registry names consistently in ExtractRegistry by using strings.ToLower().

🤖 Prompt for AI Agents
In docker_auth.go around lines 45 to 47, the remapping only checks for the
literal "docker.io" which is case-sensitive and misses the alias
registry.hub.docker.com; update the check to treat registry names
case-insensitively and include both aliases (e.g., use strings.EqualFold for
"docker.io" and "registry.hub.docker.com") or normalize registry names with
strings.ToLower earlier (such as in ExtractRegistry) so both mixed-case names
and the hub alias are remapped to defaultRegistry.


if cfg, ok := getRegistryAuth(reg, configs); ok {
return reg, cfg, nil
}
Expand Down
14 changes: 14 additions & 0 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ func TestDockerImageAuth(t *testing.T) {
require.Equal(t, base64, cfg.Auth)
})

t.Run("match the default registry authentication by host", func(t *testing.T) {
imageReg := "docker.io"
imagePath := "/my/image:latest"
reg := defaultRegistry(context.Background())
base64 := setAuthConfig(t, reg, "gopher", "secret")

registry, cfg, err := DockerImageAuth(context.Background(), imageReg+imagePath)
require.NoError(t, err)
require.Equal(t, reg, registry)
require.Equal(t, "gopher", cfg.Username)
require.Equal(t, "secret", cfg.Password)
require.Equal(t, base64, cfg.Auth)
})

t.Run("fail to match registry authentication due to invalid host", func(t *testing.T) {
imageReg := "example-auth.com"
imagePath := "/my/image:latest"
Expand Down
Loading