|
| 1 | +// Copyright 2026 HAProxy Technologies |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package test |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/haproxytech/client-native/v6/configuration" |
| 22 | + "github.com/haproxytech/client-native/v6/configuration/options" |
| 23 | + "github.com/haproxytech/client-native/v6/misc" |
| 24 | + "github.com/haproxytech/client-native/v6/models" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +// TestSerializeReturnContentValidation guards against the silent-drop bug where |
| 29 | +// an unquoted multi-word return content serializes to an invalid HAProxy config |
| 30 | +// line (e.g. `... string Missing Auth`) that fails to parse and is silently |
| 31 | +// dropped on the next config reload, while the API reports success. |
| 32 | +// |
| 33 | +// It covers all serialize sites that emit return content: http-request and |
| 34 | +// http-response deny/return, and http-error status. |
| 35 | +func TestSerializeReturnContentValidation(t *testing.T) { |
| 36 | + opt := &options.ConfigurationOptions{} |
| 37 | + |
| 38 | + cases := []struct { |
| 39 | + name string |
| 40 | + format string |
| 41 | + content string |
| 42 | + wantErr bool |
| 43 | + }{ |
| 44 | + {"unquoted_multiword", "string", "Missing Auth", true}, |
| 45 | + {"quoted_multiword", "string", `"Missing Auth"`, false}, |
| 46 | + {"escaped_space", "string", `Missing\ Auth`, false}, |
| 47 | + {"single_word", "string", "MissingAuth", false}, |
| 48 | + {"empty_content", "string", "", false}, |
| 49 | + // Content is never emitted when the format is empty or default-errorfiles, |
| 50 | + // so it cannot break the round-trip and must not be rejected. |
| 51 | + {"empty_format_multiword", "", "Missing Auth", false}, |
| 52 | + {"default_errorfiles_multiword", "default-errorfiles", "Missing Auth", false}, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tc := range cases { |
| 56 | + t.Run(tc.name, func(t *testing.T) { |
| 57 | + // http-request deny |
| 58 | + _, err := configuration.SerializeHTTPRequestRule(models.HTTPRequestRule{ |
| 59 | + Type: "deny", |
| 60 | + ReturnContentType: misc.Ptr("text/html"), |
| 61 | + ReturnContentFormat: tc.format, |
| 62 | + ReturnContent: tc.content, |
| 63 | + }, opt) |
| 64 | + assertContentErr(t, "http-request deny", tc.wantErr, err) |
| 65 | + |
| 66 | + // http-request return |
| 67 | + _, err = configuration.SerializeHTTPRequestRule(models.HTTPRequestRule{ |
| 68 | + Type: "return", |
| 69 | + ReturnContentType: misc.Ptr("text/html"), |
| 70 | + ReturnContentFormat: tc.format, |
| 71 | + ReturnContent: tc.content, |
| 72 | + }, opt) |
| 73 | + assertContentErr(t, "http-request return", tc.wantErr, err) |
| 74 | + |
| 75 | + // http-response deny |
| 76 | + _, err = configuration.SerializeHTTPResponseRule(models.HTTPResponseRule{ |
| 77 | + Type: "deny", |
| 78 | + ReturnContentType: misc.Ptr("text/html"), |
| 79 | + ReturnContentFormat: tc.format, |
| 80 | + ReturnContent: tc.content, |
| 81 | + }, opt) |
| 82 | + assertContentErr(t, "http-response deny", tc.wantErr, err) |
| 83 | + |
| 84 | + // http-response return |
| 85 | + _, err = configuration.SerializeHTTPResponseRule(models.HTTPResponseRule{ |
| 86 | + Type: "return", |
| 87 | + ReturnContentType: misc.Ptr("text/html"), |
| 88 | + ReturnContentFormat: tc.format, |
| 89 | + ReturnContent: tc.content, |
| 90 | + }, opt) |
| 91 | + assertContentErr(t, "http-response return", tc.wantErr, err) |
| 92 | + |
| 93 | + // http-error status (403 is a valid error status code) |
| 94 | + _, err = configuration.SerializeHTTPErrorRule(models.HTTPErrorRule{ |
| 95 | + Type: "status", |
| 96 | + Status: 403, |
| 97 | + ReturnContentType: misc.Ptr("text/html"), |
| 98 | + ReturnContentFormat: tc.format, |
| 99 | + ReturnContent: tc.content, |
| 100 | + }) |
| 101 | + assertContentErr(t, "http-error status", tc.wantErr, err) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func assertContentErr(t *testing.T, site string, wantErr bool, err error) { |
| 107 | + t.Helper() |
| 108 | + if wantErr { |
| 109 | + require.Error(t, err, "%s: value should be rejected, not silently dropped", site) |
| 110 | + } else { |
| 111 | + require.NoError(t, err, "%s: value should be accepted", site) |
| 112 | + } |
| 113 | +} |
0 commit comments