Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

4.1 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

package common

import (
	"errors"
	"testing"
)

// TestAllowConfiguredPrivateHost checks the ALLOW_ANY_HOST truthy values
// accepted by the Python host guard.
func TestAllowConfiguredPrivateHost(t *testing.T) {
	cases := []struct {
		value string
		want  bool
	}{
		{value: "", want: false},
		{value: "0", want: false},
		{value: "false", want: false},
		{value: "no", want: false},
		{value: "off", want: false},
		{value: "invalid", want: false},
		{value: "1", want: true},
		{value: "true", want: true},
		{value: "TRUE", want: true},
		{value: " yes ", want: true},
		{value: "on", want: true},
	}
	for _, tc := range cases {
		t.Run(tc.value, func(t *testing.T) {
			t.Setenv(EnvAllowAnyHost, tc.value)
			if got := AllowConfiguredPrivateHost(); got != tc.want {
				t.Fatalf("AllowConfiguredPrivateHost(%q) = %v, want %v", tc.value, got, tc.want)
			}
			if _, err := AssertHostSafe("127.0.0.1"); (err == nil) != tc.want {
				t.Fatalf("AssertHostSafe(loopback) = %v, want allowed=%v", err, tc.want)
			}
			_, ip, err := AssertURLSafe("http://127.0.0.1/")
			if (err == nil) != tc.want || (err == nil && ip != "127.0.0.1") {
				t.Fatalf("AssertURLSafe(loopback) = %q, %v, want allowed=%v", ip, err, tc.want)
			}
			_, ip, err = AssertURLSafeWithContext(t.Context(), "http://127.0.0.1/")
			if (err == nil) != tc.want || (err == nil && ip != "127.0.0.1") {
				t.Fatalf("AssertURLSafeWithContext(loopback) = %q, %v, want allowed=%v", ip, err, tc.want)
			}
		})
	}
}

// TestPinConfiguredPrivateHost checks that the shared opt-in is a no-op when
// the flag is unset, and that an enabled flag pins the resolved address.
func TestPinConfiguredPrivateHost(t *testing.T) {
	t.Setenv(EnvAllowAnyHost, "")
	if _, enabled, err := PinConfiguredPrivateHost("10.0.0.1"); enabled || err != nil {
		t.Fatalf("disabled flag: enabled=%v err=%v", enabled, err)
	}

	t.Setenv(EnvAllowAnyHost, "1")
	orig := LookupHost
	LookupHost = func(host string) ([]string, error) {
		if host == "host.docker.internal" {
			return []string{"192.168.65.254"}, nil
		}
		return nil, errors.New("no such host")
	}
	t.Cleanup(func() { LookupHost = orig })

	got, enabled, err := PinConfiguredPrivateHost("host.docker.internal")
	if err != nil || !enabled || got != "192.168.65.254" {
		t.Fatalf("PinConfiguredPrivateHost = %q, %v, %v", got, enabled, err)
	}
	if _, enabled, err = PinConfiguredPrivateHost("missing.internal"); !enabled || err == nil {
		t.Fatalf("unresolvable host: enabled=%v err=%v", enabled, err)
	}
}

// TestResolveHostPinSkipsPublicCheck checks that a private resolution is
// returned as the pin, and that empty or unresolvable hosts still fail.
func TestResolveHostPinSkipsPublicCheck(t *testing.T) {
	orig := LookupHost
	LookupHost = func(host string) ([]string, error) {
		if host == "host.docker.internal" {
			return []string{"192.168.65.254", "10.0.0.2"}, nil
		}
		return nil, errors.New("no such host")
	}
	t.Cleanup(func() { LookupHost = orig })

	got, err := ResolveHostPin("host.docker.internal")
	if err != nil {
		t.Fatalf("ResolveHostPin: %v", err)
	}
	if got != "192.168.65.254" {
		t.Fatalf("ResolveHostPin = %q, want 192.168.65.254", got)
	}

	literal, err := ResolveHostPin("10.1.2.3")
	if err != nil || literal != "10.1.2.3" {
		t.Fatalf("ResolveHostPin(literal) = %q, %v", literal, err)
	}
	if _, err = ResolveHostPin("missing.internal"); err == nil {
		t.Fatal("unresolvable host must still fail")
	}
	if _, err = ResolveHostPin("  "); err == nil {
		t.Fatal("empty host must still fail")
	}
}