Skip to content
Merged
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
8 changes: 6 additions & 2 deletions internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ func (h *Handlers) create(w http.ResponseWriter, r *http.Request) {
http.Error(w, "invalid domain in 'from'", http.StatusBadRequest)
return
}
if req.To == "" {
to := strings.TrimSpace(req.To)
if to == "" {
http.Error(w, "'to' is required", http.StatusBadRequest)
return
}
if !strings.HasPrefix(to, "http://") && !strings.HasPrefix(to, "https://") {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: The scheme prefix check is case-sensitive, so uppercase schemes like 'HTTP://' or 'HTTPS://' are not recognized and get a duplicate 'https://' prepended, producing a malformed URL.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/api/handlers.go, line 77:

<comment>The scheme prefix check is case-sensitive, so uppercase schemes like 'HTTP://' or 'HTTPS://' are not recognized and get a duplicate 'https://' prepended, producing a malformed URL.</comment>

<file context>
@@ -69,10 +69,14 @@ func (h *Handlers) create(w http.ResponseWriter, r *http.Request) {
 		http.Error(w, "'to' is required", http.StatusBadRequest)
 		return
 	}
+	if !strings.HasPrefix(to, "http://") && !strings.HasPrefix(to, "https://") {
+		to = "https://" + to
+	}
</file context>
Suggested change
if !strings.HasPrefix(to, "http://") && !strings.HasPrefix(to, "https://") {
if !strings.HasPrefix(strings.ToLower(to), "http://") && !strings.HasPrefix(strings.ToLower(to), "https://") {

to = "https://" + to
}
ns := h.nsOrDefault(req.Namespace)

rd := &decositesv1alpha1.DecoRedirect{
Expand All @@ -82,7 +86,7 @@ func (h *Handlers) create(w http.ResponseWriter, r *http.Request) {
},
Spec: decositesv1alpha1.DecoRedirectSpec{
From: from, // original domain preserved for CEL validation
To: req.To,
To: to,
},
}
if err := h.client.Create(r.Context(), rd); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ func TestCreate_HappyPath(t *testing.T) {
}
}

func TestCreate_NormalizesToScheme(t *testing.T) {
scheme := runtime.NewScheme()
_ = clientgoscheme.AddToScheme(scheme)
_ = decositesv1alpha1.AddToScheme(scheme)
fc := fake.NewClientBuilder().WithScheme(scheme).Build()
h := api.NewHandlers(fc, "deco-redirect-system")
srv := api.NewServer(":0", "user", "pass", h)

body, _ := json.Marshal(map[string]string{"from": "example.com", "to": "www.example.com"})
req := httptest.NewRequest(http.MethodPost, "/redirects", bytes.NewReader(body))
req.SetBasicAuth("user", "pass")
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, req)
if rec.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", rec.Code, rec.Body.String())
}

list := &decositesv1alpha1.DecoRedirectList{}
_ = fc.List(context.Background(), list)
if list.Items[0].Spec.To != "https://www.example.com" {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Missing guard against empty Items slice before accessing Items[0]. If the create handler returns 201 without persisting the object, list.Items will be empty and this line will panic with an index-out-of-range error.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At internal/api/server_test.go, line 80:

<comment>Missing guard against empty Items slice before accessing Items[0]. If the create handler returns 201 without persisting the object, list.Items will be empty and this line will panic with an index-out-of-range error.</comment>

<file context>
@@ -58,6 +58,30 @@ func TestCreate_HappyPath(t *testing.T) {
+
+	list := &decositesv1alpha1.DecoRedirectList{}
+	_ = fc.List(context.Background(), list)
+	if list.Items[0].Spec.To != "https://www.example.com" {
+		t.Fatalf("expected to=https://www.example.com, got %s", list.Items[0].Spec.To)
+	}
</file context>
Suggested change
if list.Items[0].Spec.To != "https://www.example.com" {
if len(list.Items) != 1 {
t.Fatalf("expected 1 DecoRedirect, got %d", len(list.Items))
}
if list.Items[0].Spec.To != "https://www.example.com" {

t.Fatalf("expected to=https://www.example.com, got %s", list.Items[0].Spec.To)
}
}

func TestDelete_HappyPath(t *testing.T) {
scheme := runtime.NewScheme()
_ = clientgoscheme.AddToScheme(scheme)
Expand Down
Loading