|
8 | 8 | "fmt" |
9 | 9 | "io" |
10 | 10 | "net/http" |
| 11 | + "os" |
11 | 12 | "regexp" |
12 | 13 | "runtime/debug" |
13 | 14 | "strconv" |
@@ -1857,6 +1858,121 @@ func TestCloseFails(t *testing.T) { |
1857 | 1858 | } |
1858 | 1859 | } |
1859 | 1860 |
|
| 1861 | +func TestUploadKeepFiles(t *testing.T) { |
| 1862 | + if !environment.HasAccessToFS { |
| 1863 | + t.Skip("skipping test as it requires access to filesystem") |
| 1864 | + } |
| 1865 | + |
| 1866 | + createTmpFile := func(t *testing.T) string { |
| 1867 | + t.Helper() |
| 1868 | + f, err := os.CreateTemp(t.TempDir(), "crztest*") |
| 1869 | + if err != nil { |
| 1870 | + t.Fatal(err) |
| 1871 | + } |
| 1872 | + name := f.Name() |
| 1873 | + if err := f.Close(); err != nil { |
| 1874 | + t.Fatalf("failed to close temp file: %v", err) |
| 1875 | + } |
| 1876 | + return name |
| 1877 | + } |
| 1878 | + |
| 1879 | + t.Run("Off deletes files", func(t *testing.T) { |
| 1880 | + waf := NewWAF() |
| 1881 | + waf.UploadKeepFiles = types.UploadKeepFilesOff |
| 1882 | + tx := waf.NewTransaction() |
| 1883 | + tmpFile := createTmpFile(t) |
| 1884 | + |
| 1885 | + col := tx.Variables().FilesTmpNames().(*collections.Map) |
| 1886 | + col.Add("", tmpFile) |
| 1887 | + |
| 1888 | + if err := tx.Close(); err != nil { |
| 1889 | + t.Fatal(err) |
| 1890 | + } |
| 1891 | + |
| 1892 | + if _, err := os.Stat(tmpFile); !os.IsNotExist(err) { |
| 1893 | + t.Fatal("expected temp file to be deleted when UploadKeepFiles is Off") |
| 1894 | + } |
| 1895 | + }) |
| 1896 | + |
| 1897 | + t.Run("On keeps files", func(t *testing.T) { |
| 1898 | + waf := NewWAF() |
| 1899 | + waf.UploadKeepFiles = types.UploadKeepFilesOn |
| 1900 | + tx := waf.NewTransaction() |
| 1901 | + tmpFile := createTmpFile(t) |
| 1902 | + |
| 1903 | + col := tx.Variables().FilesTmpNames().(*collections.Map) |
| 1904 | + col.Add("", tmpFile) |
| 1905 | + |
| 1906 | + if err := tx.Close(); err != nil { |
| 1907 | + t.Fatal(err) |
| 1908 | + } |
| 1909 | + |
| 1910 | + if _, err := os.Stat(tmpFile); err != nil { |
| 1911 | + t.Fatal("expected temp file to be kept when UploadKeepFiles is On") |
| 1912 | + } |
| 1913 | + }) |
| 1914 | + |
| 1915 | + t.Run("RelevantOnly keeps files when log rules matched", func(t *testing.T) { |
| 1916 | + waf := NewWAF() |
| 1917 | + waf.UploadKeepFiles = types.UploadKeepFilesRelevantOnly |
| 1918 | + tx := waf.NewTransaction() |
| 1919 | + tmpFile := createTmpFile(t) |
| 1920 | + |
| 1921 | + // Simulate a matched rule with Log enabled |
| 1922 | + tx.matchedRules = append(tx.matchedRules, &corazarules.MatchedRule{Log_: true}) |
| 1923 | + |
| 1924 | + col := tx.Variables().FilesTmpNames().(*collections.Map) |
| 1925 | + col.Add("", tmpFile) |
| 1926 | + |
| 1927 | + if err := tx.Close(); err != nil { |
| 1928 | + t.Fatal(err) |
| 1929 | + } |
| 1930 | + |
| 1931 | + if _, err := os.Stat(tmpFile); err != nil { |
| 1932 | + t.Fatal("expected temp file to be kept when UploadKeepFiles is RelevantOnly and log rules matched") |
| 1933 | + } |
| 1934 | + }) |
| 1935 | + |
| 1936 | + t.Run("RelevantOnly deletes files when only nolog rules matched", func(t *testing.T) { |
| 1937 | + waf := NewWAF() |
| 1938 | + waf.UploadKeepFiles = types.UploadKeepFilesRelevantOnly |
| 1939 | + tx := waf.NewTransaction() |
| 1940 | + tmpFile := createTmpFile(t) |
| 1941 | + |
| 1942 | + // Simulate a matched rule with Log disabled (e.g. CRS initialization rules) |
| 1943 | + tx.matchedRules = append(tx.matchedRules, &corazarules.MatchedRule{Log_: false}) |
| 1944 | + |
| 1945 | + col := tx.Variables().FilesTmpNames().(*collections.Map) |
| 1946 | + col.Add("", tmpFile) |
| 1947 | + |
| 1948 | + if err := tx.Close(); err != nil { |
| 1949 | + t.Fatal(err) |
| 1950 | + } |
| 1951 | + |
| 1952 | + if _, err := os.Stat(tmpFile); !os.IsNotExist(err) { |
| 1953 | + t.Fatal("expected temp file to be deleted when UploadKeepFiles is RelevantOnly and only nolog rules matched") |
| 1954 | + } |
| 1955 | + }) |
| 1956 | + |
| 1957 | + t.Run("RelevantOnly deletes files when no rules matched", func(t *testing.T) { |
| 1958 | + waf := NewWAF() |
| 1959 | + waf.UploadKeepFiles = types.UploadKeepFilesRelevantOnly |
| 1960 | + tx := waf.NewTransaction() |
| 1961 | + tmpFile := createTmpFile(t) |
| 1962 | + |
| 1963 | + col := tx.Variables().FilesTmpNames().(*collections.Map) |
| 1964 | + col.Add("", tmpFile) |
| 1965 | + |
| 1966 | + if err := tx.Close(); err != nil { |
| 1967 | + t.Fatal(err) |
| 1968 | + } |
| 1969 | + |
| 1970 | + if _, err := os.Stat(tmpFile); !os.IsNotExist(err) { |
| 1971 | + t.Fatal("expected temp file to be deleted when UploadKeepFiles is RelevantOnly and no rules matched") |
| 1972 | + } |
| 1973 | + }) |
| 1974 | +} |
| 1975 | + |
1860 | 1976 | func TestRequestFilename(t *testing.T) { |
1861 | 1977 | tests := []struct { |
1862 | 1978 | name string |
|
0 commit comments