| // |
| 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 | package http |
| 16 |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "strings" |
| 20 | "testing" |
| 21 |
| 22 | configtypes "github.com/fatedier/frp/pkg/config/types" |
| 23 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 24 | "github.com/fatedier/frp/pkg/metrics/mem" |
| 25 | "github.com/fatedier/frp/server/http/model" |
| 26 | ) |
| 27 |
| 28 | func TestBuildV2ProxySpecAllTypesAndRedaction(t *testing.T) { |
| 29 | tests := []struct { |
| 30 | proxyType string |
| 31 | cfg v1.ProxyConfigurer |
| 32 | blockKeys []string |
| 33 | }{ |
| 34 | { |
| 35 | proxyType: "tcp", |
| 36 | cfg: &v1.TCPProxyConfig{ |
| 37 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "tcp"), |
| 38 | RemotePort: 6000, |
| 39 | }, |
| 40 | blockKeys: []string{"annotations", "loadBalancer", "metadatas", "remotePort", "transport"}, |
| 41 | }, |
| 42 | { |
| 43 | proxyType: "udp", |
| 44 | cfg: &v1.UDPProxyConfig{ |
| 45 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "udp"), |
| 46 | RemotePort: 7000, |
| 47 | }, |
| 48 | blockKeys: []string{"annotations", "loadBalancer", "metadatas", "remotePort", "transport"}, |
| 49 | }, |
| 50 | { |
| 51 | proxyType: "http", |
| 52 | cfg: &v1.HTTPProxyConfig{ |
| 53 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "http"), |
| 54 | DomainConfig: v1.DomainConfig{CustomDomains: []string{"app.example.com"}, SubDomain: "app"}, |
| 55 | Locations: []string{"/api"}, |
| 56 | HTTPUser: "secret-http-user", |
| 57 | HTTPPassword: "secret-http-password", |
| 58 | HostHeaderRewrite: "backend.example.com", |
| 59 | RequestHeaders: v1.HeaderOperations{Set: map[string]string{"X-Secret": "secret-request-header"}}, |
| 60 | ResponseHeaders: v1.HeaderOperations{Set: map[string]string{"X-Secret": "secret-response-header"}}, |
| 61 | RouteByHTTPUser: "secret-http-route-user", |
| 62 | }, |
| 63 | blockKeys: []string{"annotations", "customDomains", "hostHeaderRewrite", "loadBalancer", "locations", "metadatas", "subdomain", "transport"}, |
| 64 | }, |
| 65 | { |
| 66 | proxyType: "https", |
| 67 | cfg: &v1.HTTPSProxyConfig{ |
| 68 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "https"), |
| 69 | DomainConfig: v1.DomainConfig{CustomDomains: []string{"secure.example.com"}, SubDomain: "secure"}, |
| 70 | }, |
| 71 | blockKeys: []string{"annotations", "customDomains", "loadBalancer", "metadatas", "subdomain", "transport"}, |
| 72 | }, |
| 73 | { |
| 74 | proxyType: "tcpmux", |
| 75 | cfg: &v1.TCPMuxProxyConfig{ |
| 76 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "tcpmux"), |
| 77 | DomainConfig: v1.DomainConfig{CustomDomains: []string{"mux.example.com"}, SubDomain: "mux"}, |
| 78 | HTTPUser: strings.Join([]string{"secret", "mux-http-user"}, "-"), |
| 79 | HTTPPassword: strings.Join([]string{"secret", "mux-http-password"}, "-"), |
| 80 | RouteByHTTPUser: "displayed-mux-user", |
| 81 | Multiplexer: "httpconnect", |
| 82 | }, |
| 83 | blockKeys: []string{"annotations", "customDomains", "loadBalancer", "metadatas", "multiplexer", "routeByHTTPUser", "subdomain", "transport"}, |
| 84 | }, |
| 85 | { |
| 86 | proxyType: "stcp", |
| 87 | cfg: &v1.STCPProxyConfig{ |
| 88 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "stcp"), |
| 89 | Secretkey: strings.Join([]string{"secret", "stcp-key"}, "-"), |
| 90 | AllowUsers: []string{strings.Join([]string{"secret", "stcp-user"}, "-")}, |
| 91 | }, |
| 92 | blockKeys: []string{"annotations", "loadBalancer", "metadatas", "transport"}, |
| 93 | }, |
| 94 | { |
| 95 | proxyType: "sudp", |
| 96 | cfg: &v1.SUDPProxyConfig{ |
| 97 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "sudp"), |
| 98 | Secretkey: strings.Join([]string{"secret", "sudp-key"}, "-"), |
| 99 | AllowUsers: []string{strings.Join([]string{"secret", "sudp-user"}, "-")}, |
| 100 | }, |
| 101 | blockKeys: []string{"annotations", "loadBalancer", "metadatas", "transport"}, |
| 102 | }, |
| 103 | { |
| 104 | proxyType: "xtcp", |
| 105 | cfg: &v1.XTCPProxyConfig{ |
| 106 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "xtcp"), |
| 107 | Secretkey: strings.Join([]string{"secret", "xtcp-key"}, "-"), |
| 108 | AllowUsers: []string{strings.Join([]string{"secret", "xtcp-user"}, "-")}, |
| 109 | }, |
| 110 | blockKeys: []string{"annotations", "loadBalancer", "metadatas", "transport"}, |
| 111 | }, |
| 112 | } |
| 113 |
| 114 | for _, tt := range tests { |
| 115 | t.Run(tt.proxyType, func(t *testing.T) { |
| 116 | spec := buildV2ProxySpec(tt.proxyType, tt.cfg) |
| 117 | raw := mustMarshalJSON(t, spec) |
| 118 |
| 119 | var specObject map[string]json.RawMessage |
| 120 | if err := json.Unmarshal(raw, &specObject); err != nil { |
| 121 | t.Fatalf("unmarshal spec failed: %v", err) |
| 122 | } |
| 123 | assertRawJSONKeys(t, specObject, tt.proxyType, "type") |
| 124 |
| 125 | var gotType string |
| 126 | if err := json.Unmarshal(specObject["type"], &gotType); err != nil { |
| 127 | t.Fatalf("unmarshal spec type failed: %v", err) |
| 128 | } |
| 129 | if gotType != tt.proxyType { |
| 130 | t.Fatalf("spec type mismatch, want %q got %q", tt.proxyType, gotType) |
| 131 | } |
| 132 |
| 133 | var block map[string]json.RawMessage |
| 134 | if err := json.Unmarshal(specObject[tt.proxyType], &block); err != nil { |
| 135 | t.Fatalf("unmarshal active block failed: %v", err) |
| 136 | } |
| 137 | assertRawJSONKeys(t, block, tt.blockKeys...) |
| 138 | assertV2ProxyCommonSpec(t, block) |
| 139 | assertV2ProxyTypeFields(t, tt.proxyType, specObject[tt.proxyType]) |
| 140 | assertNoV2ProxySensitiveFields(t, block) |
| 141 |
| 142 | content := string(raw) |
| 143 | for _, secret := range []string{ |
| 144 | "secret-proxy-name", |
| 145 | "secret-group-key", |
| 146 | "secret-local-host", |
| 147 | "secret-plugin-user", |
| 148 | "secret-plugin-password", |
| 149 | "secret-health-path", |
| 150 | "secret-http-user", |
| 151 | "secret-http-password", |
| 152 | "secret-request-header", |
| 153 | "secret-response-header", |
| 154 | "secret-http-route-user", |
| 155 | "secret-mux-http-user", |
| 156 | "secret-mux-http-password", |
| 157 | "secret-stcp-key", |
| 158 | "secret-stcp-user", |
| 159 | "secret-sudp-key", |
| 160 | "secret-sudp-user", |
| 161 | "secret-xtcp-key", |
| 162 | "secret-xtcp-user", |
| 163 | } { |
| 164 | if strings.Contains(content, secret) { |
| 165 | t.Fatalf("sensitive value %q leaked in spec: %s", secret, content) |
| 166 | } |
| 167 | } |
| 168 | }) |
| 169 | } |
| 170 | } |
| 171 |
| 172 | func assertV2ProxyTypeFields(t *testing.T, proxyType string, raw json.RawMessage) { |
| 173 | t.Helper() |
| 174 |
| 175 | switch proxyType { |
| 176 | case "tcp": |
| 177 | var block model.V2TCPProxySpec |
| 178 | if err := json.Unmarshal(raw, &block); err != nil { |
| 179 | t.Fatalf("unmarshal tcp block failed: %v", err) |
| 180 | } |
| 181 | if block.RemotePort == nil || *block.RemotePort != 6000 { |
| 182 | t.Fatalf("tcp remote port mismatch: %#v", block.RemotePort) |
| 183 | } |
| 184 | case "udp": |
| 185 | var block model.V2UDPProxySpec |
| 186 | if err := json.Unmarshal(raw, &block); err != nil { |
| 187 | t.Fatalf("unmarshal udp block failed: %v", err) |
| 188 | } |
| 189 | if block.RemotePort == nil || *block.RemotePort != 7000 { |
| 190 | t.Fatalf("udp remote port mismatch: %#v", block.RemotePort) |
| 191 | } |
| 192 | case "http": |
| 193 | var block model.V2HTTPProxySpec |
| 194 | if err := json.Unmarshal(raw, &block); err != nil { |
| 195 | t.Fatalf("unmarshal http block failed: %v", err) |
| 196 | } |
| 197 | if len(block.CustomDomains) != 1 || block.CustomDomains[0] != "app.example.com" || |
| 198 | block.Subdomain != "app" || len(block.Locations) != 1 || block.Locations[0] != "/api" || |
| 199 | block.HostHeaderRewrite != "backend.example.com" { |
| 200 | t.Fatalf("http fields mismatch: %#v", block) |
| 201 | } |
| 202 | case "https": |
| 203 | var block model.V2HTTPSProxySpec |
| 204 | if err := json.Unmarshal(raw, &block); err != nil { |
| 205 | t.Fatalf("unmarshal https block failed: %v", err) |
| 206 | } |
| 207 | if len(block.CustomDomains) != 1 || block.CustomDomains[0] != "secure.example.com" || block.Subdomain != "secure" { |
| 208 | t.Fatalf("https fields mismatch: %#v", block) |
| 209 | } |
| 210 | case "tcpmux": |
| 211 | var block model.V2TCPMuxProxySpec |
| 212 | if err := json.Unmarshal(raw, &block); err != nil { |
| 213 | t.Fatalf("unmarshal tcpmux block failed: %v", err) |
| 214 | } |
| 215 | if len(block.CustomDomains) != 1 || block.CustomDomains[0] != "mux.example.com" || |
| 216 | block.Subdomain != "mux" || block.Multiplexer != "httpconnect" || block.RouteByHTTPUser != "displayed-mux-user" { |
| 217 | t.Fatalf("tcpmux fields mismatch: %#v", block) |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 |
| 222 | func TestBuildV2ProxyRespOfflineTypedShells(t *testing.T) { |
| 223 | for _, proxyType := range apiV2ProxyTypes { |
| 224 | t.Run(proxyType, func(t *testing.T) { |
| 225 | resp := (&Controller{}).buildV2ProxyResp(&mem.ProxyStats{ |
| 226 | Name: "offline-" + proxyType, |
| 227 | Type: proxyType, |
| 228 | }) |
| 229 | if resp.Status.State != "offline" { |
| 230 | t.Fatalf("offline phase mismatch: %#v", resp.Status) |
| 231 | } |
| 232 |
| 233 | var specObject map[string]json.RawMessage |
| 234 | if err := json.Unmarshal(mustMarshalJSON(t, resp.Spec), &specObject); err != nil { |
| 235 | t.Fatalf("unmarshal offline spec failed: %v", err) |
| 236 | } |
| 237 | assertRawJSONKeys(t, specObject, proxyType, "type") |
| 238 | assertRawJSONKeysFromMessage(t, specObject[proxyType]) |
| 239 | }) |
| 240 | } |
| 241 | } |
| 242 |
| 243 | func TestBuildV2ProxySpecDoesNotPopulateMismatchedBlock(t *testing.T) { |
| 244 | spec := buildV2ProxySpec("tcp", &v1.UDPProxyConfig{ |
| 245 | ProxyBaseConfig: newV2ProxyTestBaseConfig(t, "udp"), |
| 246 | RemotePort: 7000, |
| 247 | }) |
| 248 |
| 249 | var specObject map[string]json.RawMessage |
| 250 | if err := json.Unmarshal(mustMarshalJSON(t, spec), &specObject); err != nil { |
| 251 | t.Fatalf("unmarshal mismatched spec failed: %v", err) |
| 252 | } |
| 253 | assertRawJSONKeys(t, specObject, "tcp", "type") |
| 254 | assertRawJSONKeysFromMessage(t, specObject["tcp"]) |
| 255 | } |
| 256 |
| 257 | func newV2ProxyTestBaseConfig(t *testing.T, proxyType string) v1.ProxyBaseConfig { |
| 258 | t.Helper() |
| 259 |
| 260 | bandwidthLimit, err := configtypes.NewBandwidthQuantity("10MB") |
| 261 | if err != nil { |
| 262 | t.Fatalf("create bandwidth limit failed: %v", err) |
| 263 | } |
| 264 | enabled := false |
| 265 | return v1.ProxyBaseConfig{ |
| 266 | Name: "secret-proxy-name", |
| 267 | Type: proxyType, |
| 268 | Enabled: &enabled, |
| 269 | Annotations: map[string]string{"annotation-key": "annotation-value"}, |
| 270 | Metadatas: map[string]string{"metadata-key": "metadata-value"}, |
| 271 | Transport: v1.ProxyTransport{ |
| 272 | UseEncryption: true, |
| 273 | UseCompression: true, |
| 274 | BandwidthLimit: bandwidthLimit, |
| 275 | BandwidthLimitMode: configtypes.BandwidthLimitModeServer, |
| 276 | ProxyProtocolVersion: "v2", |
| 277 | }, |
| 278 | LoadBalancer: v1.LoadBalancerConfig{ |
| 279 | Group: "public-group", |
| 280 | GroupKey: "secret-group-key", |
| 281 | }, |
| 282 | HealthCheck: v1.HealthCheckConfig{ |
| 283 | Type: "http", |
| 284 | Path: "secret-health-path", |
| 285 | }, |
| 286 | ProxyBackend: v1.ProxyBackend{ |
| 287 | LocalIP: "secret-local-host", |
| 288 | LocalPort: 8080, |
| 289 | Plugin: v1.TypedClientPluginOptions{ |
| 290 | Type: v1.PluginHTTPProxy, |
| 291 | ClientPluginOptions: &v1.HTTPProxyPluginOptions{ |
| 292 | Type: v1.PluginHTTPProxy, |
| 293 | HTTPUser: "secret-plugin-user", |
| 294 | HTTPPassword: "secret-plugin-password", |
| 295 | }, |
| 296 | }, |
| 297 | }, |
| 298 | } |
| 299 | } |
| 300 |
| 301 | func assertV2ProxyCommonSpec(t *testing.T, block map[string]json.RawMessage) { |
| 302 | t.Helper() |
| 303 |
| 304 | var annotations map[string]string |
| 305 | if err := json.Unmarshal(block["annotations"], &annotations); err != nil { |
| 306 | t.Fatalf("unmarshal annotations failed: %v", err) |
| 307 | } |
| 308 | if annotations["annotation-key"] != "annotation-value" { |
| 309 | t.Fatalf("annotations mismatch: %#v", annotations) |
| 310 | } |
| 311 |
| 312 | var metadatas map[string]string |
| 313 | if err := json.Unmarshal(block["metadatas"], &metadatas); err != nil { |
| 314 | t.Fatalf("unmarshal metadatas failed: %v", err) |
| 315 | } |
| 316 | if metadatas["metadata-key"] != "metadata-value" { |
| 317 | t.Fatalf("metadatas mismatch: %#v", metadatas) |
| 318 | } |
| 319 |
| 320 | assertRawJSONKeysFromMessage(t, block["transport"], |
| 321 | "bandwidthLimit", |
| 322 | "bandwidthLimitMode", |
| 323 | "useCompression", |
| 324 | "useEncryption", |
| 325 | ) |
| 326 | var transport model.V2ProxyTransportSpec |
| 327 | if err := json.Unmarshal(block["transport"], &transport); err != nil { |
| 328 | t.Fatalf("unmarshal transport failed: %v", err) |
| 329 | } |
| 330 | if !transport.UseEncryption || !transport.UseCompression || |
| 331 | transport.BandwidthLimit != "10MB" || transport.BandwidthLimitMode != "server" { |
| 332 | t.Fatalf("transport mismatch: %#v", transport) |
| 333 | } |
| 334 |
| 335 | assertRawJSONKeysFromMessage(t, block["loadBalancer"], "group") |
| 336 | var loadBalancer model.V2ProxyLoadBalancerSpec |
| 337 | if err := json.Unmarshal(block["loadBalancer"], &loadBalancer); err != nil { |
| 338 | t.Fatalf("unmarshal load balancer failed: %v", err) |
| 339 | } |
| 340 | if loadBalancer.Group != "public-group" { |
| 341 | t.Fatalf("load balancer mismatch: %#v", loadBalancer) |
| 342 | } |
| 343 | } |
| 344 |
| 345 | func assertNoV2ProxySensitiveFields(t *testing.T, value any) { |
| 346 | t.Helper() |
| 347 |
| 348 | forbidden := map[string]struct{}{ |
| 349 | "allowUsers": {}, |
| 350 | "enabled": {}, |
| 351 | "groupKey": {}, |
| 352 | "healthCheck": {}, |
| 353 | "httpPassword": {}, |
| 354 | "httpUser": {}, |
| 355 | "localIP": {}, |
| 356 | "localPort": {}, |
| 357 | "name": {}, |
| 358 | "natTraversal": {}, |
| 359 | "plugin": {}, |
| 360 | "proxyProtocolVersion": {}, |
| 361 | "requestHeaders": {}, |
| 362 | "responseHeaders": {}, |
| 363 | "secretKey": {}, |
| 364 | "type": {}, |
| 365 | } |
| 366 |
| 367 | var walk func(any) |
| 368 | walk = func(current any) { |
| 369 | switch current := current.(type) { |
| 370 | case map[string]any: |
| 371 | for key, nested := range current { |
| 372 | if _, ok := forbidden[key]; ok { |
| 373 | t.Fatalf("sensitive field %q leaked in active block", key) |
| 374 | } |
| 375 | walk(nested) |
| 376 | } |
| 377 | case []any: |
| 378 | for _, nested := range current { |
| 379 | walk(nested) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 |
| 384 | raw, err := json.Marshal(value) |
| 385 | if err != nil { |
| 386 | t.Fatalf("marshal active block failed: %v", err) |
| 387 | } |
| 388 | var decoded any |
| 389 | if err := json.Unmarshal(raw, &decoded); err != nil { |
| 390 | t.Fatalf("decode active block failed: %v", err) |
| 391 | } |
| 392 | walk(decoded) |
| 393 | } |