2021-10-14 23:30:51 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
2021-11-05 03:01:41 +05:30
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2021-11-25 17:09:20 +02:00
|
|
|
|
2021-11-05 03:01:41 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
|
2023-08-11 17:00:43 +03:00
|
|
|
permissionutil "github.com/projectdiscovery/utils/permission"
|
2021-10-14 23:30:51 +02:00
|
|
|
)
|
|
|
|
|
|
2023-07-28 18:50:57 +03:00
|
|
|
var loaderTestcases = []TestCaseInfo{
|
|
|
|
|
{Path: "loader/template-list.yaml", TestCase: &remoteTemplateList{}},
|
|
|
|
|
{Path: "loader/workflow-list.yaml", TestCase: &remoteWorkflowList{}},
|
|
|
|
|
{Path: "loader/excluded-template.yaml", TestCase: &excludedTemplate{}},
|
|
|
|
|
{Path: "loader/nonexistent-template-list.yaml", TestCase: &nonExistentTemplateList{}},
|
|
|
|
|
{Path: "loader/nonexistent-workflow-list.yaml", TestCase: &nonExistentWorkflowList{}},
|
|
|
|
|
{Path: "loader/template-list-not-allowed.yaml", TestCase: &remoteTemplateListNotAllowed{}},
|
2021-10-14 23:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type remoteTemplateList struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *remoteTemplateList) Execute(templateList string) error {
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
|
2021-11-25 17:18:54 +02:00
|
|
|
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2021-10-14 23:30:51 +02:00
|
|
|
fmt.Fprintf(w, "This is test matcher text")
|
|
|
|
|
if strings.EqualFold(r.Header.Get("test"), "nuclei") {
|
|
|
|
|
fmt.Fprintf(w, "This is test headers matcher text")
|
|
|
|
|
}
|
2021-11-25 17:18:54 +02:00
|
|
|
})
|
2021-10-14 23:30:51 +02:00
|
|
|
|
2021-11-25 17:18:54 +02:00
|
|
|
router.GET("/template_list", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2021-10-14 23:30:51 +02:00
|
|
|
file, err := os.ReadFile(templateList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}
|
2021-10-26 15:34:33 +02:00
|
|
|
_, err = w.Write(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}
|
2021-11-25 17:18:54 +02:00
|
|
|
})
|
2021-10-14 23:30:51 +02:00
|
|
|
ts := httptest.NewServer(router)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
2022-01-27 17:19:23 +05:30
|
|
|
configFileData := `remote-template-domain: [ "` + ts.Listener.Addr().String() + `" ]`
|
2023-08-11 17:00:43 +03:00
|
|
|
err := os.WriteFile("test-config.yaml", []byte(configFileData), permissionutil.ConfigFilePermission)
|
2022-01-27 17:19:23 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer os.Remove("test-config.yaml")
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, "-target", ts.URL, "-tu", ts.URL+"/template_list", "-config", "test-config.yaml")
|
2021-10-14 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-15 16:03:57 +02:00
|
|
|
|
|
|
|
|
return expectResultsCount(results, 2)
|
2021-10-14 23:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 18:09:29 +05:30
|
|
|
type excludedTemplate struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *excludedTemplate) Execute(templateList string) error {
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
|
|
|
|
|
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
fmt.Fprintf(w, "This is test matcher text")
|
|
|
|
|
if strings.EqualFold(r.Header.Get("test"), "nuclei") {
|
|
|
|
|
fmt.Fprintf(w, "This is test headers matcher text")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
ts := httptest.NewServer(router)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, "-target", ts.URL, "-t", templateList, "-include-templates", templateList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return expectResultsCount(results, 1)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 17:19:23 +05:30
|
|
|
type remoteTemplateListNotAllowed struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *remoteTemplateListNotAllowed) Execute(templateList string) error {
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
|
|
|
|
|
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
fmt.Fprintf(w, "This is test matcher text")
|
|
|
|
|
if strings.EqualFold(r.Header.Get("test"), "nuclei") {
|
|
|
|
|
fmt.Fprintf(w, "This is test headers matcher text")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.GET("/template_list", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
file, err := os.ReadFile(templateList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}
|
|
|
|
|
_, err = w.Write(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
ts := httptest.NewServer(router)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
_, err := testutils.RunNucleiBareArgsAndGetResults(debug, "-target", ts.URL, "-tu", ts.URL+"/template_list")
|
|
|
|
|
if err == nil {
|
|
|
|
|
return fmt.Errorf("expected error for not allowed remote template list url")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 23:30:51 +02:00
|
|
|
type remoteWorkflowList struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *remoteWorkflowList) Execute(workflowList string) error {
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
|
2021-11-25 17:18:54 +02:00
|
|
|
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2021-10-14 23:30:51 +02:00
|
|
|
fmt.Fprintf(w, "This is test matcher text")
|
|
|
|
|
if strings.EqualFold(r.Header.Get("test"), "nuclei") {
|
|
|
|
|
fmt.Fprintf(w, "This is test headers matcher text")
|
|
|
|
|
}
|
2021-11-25 17:18:54 +02:00
|
|
|
})
|
2021-10-14 23:30:51 +02:00
|
|
|
|
2021-11-25 17:18:54 +02:00
|
|
|
router.GET("/workflow_list", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2021-10-14 23:30:51 +02:00
|
|
|
file, err := os.ReadFile(workflowList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}
|
2021-10-26 15:34:33 +02:00
|
|
|
_, err = w.Write(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}
|
2021-11-25 17:18:54 +02:00
|
|
|
})
|
2021-10-14 23:30:51 +02:00
|
|
|
ts := httptest.NewServer(router)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
2022-01-27 17:19:23 +05:30
|
|
|
configFileData := `remote-template-domain: [ "` + ts.Listener.Addr().String() + `" ]`
|
2023-08-11 17:00:43 +03:00
|
|
|
err := os.WriteFile("test-config.yaml", []byte(configFileData), permissionutil.ConfigFilePermission)
|
2022-01-27 17:19:23 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer os.Remove("test-config.yaml")
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, "-target", ts.URL, "-wu", ts.URL+"/workflow_list", "-config", "test-config.yaml")
|
2021-10-14 23:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-15 16:03:57 +02:00
|
|
|
|
|
|
|
|
return expectResultsCount(results, 3)
|
2021-10-14 23:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type nonExistentTemplateList struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *nonExistentTemplateList) Execute(nonExistingTemplateList string) error {
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
ts := httptest.NewServer(router)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
_, err := testutils.RunNucleiBareArgsAndGetResults(debug, "-target", ts.URL, "-tu", ts.URL+"/404")
|
|
|
|
|
if err == nil {
|
|
|
|
|
return fmt.Errorf("expected error for nonexisting workflow url")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type nonExistentWorkflowList struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *nonExistentWorkflowList) Execute(nonExistingWorkflowList string) error {
|
|
|
|
|
router := httprouter.New()
|
|
|
|
|
ts := httptest.NewServer(router)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
_, err := testutils.RunNucleiBareArgsAndGetResults(debug, "-target", ts.URL, "-wu", ts.URL+"/404")
|
|
|
|
|
if err == nil {
|
|
|
|
|
return fmt.Errorf("expected error for nonexisting workflow url")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|