Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for simulator api #68

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3039,3 +3039,49 @@ func (sc *Client) UpdateEdgeDeploymentBackends(corpName, siteName, fastlySID str

return err
}

// Simulation request the sample request and response for the simulation test
type SimulationBody struct {
SampleRequest string `json:"sample_request"`
SampleResponse string `json:"sample_response"`
}

// SimulationResponse the response of the simulation test
type ResponseSimulationBody struct {
Data struct {
WafResponse int `json:"waf_response"`
ResponseCode int `json:"response_code"`
ResponseSize int `json:"response_size"`
Signals []struct {
Type string `json:"type"`
Location string `json:"location"`
Name string `json:"name"`
Value string `json:"value"`
Detector string `json:"detector"`
Redaction int `json:"redaction"`
} `json:"signals"`
} `json:"data"`
}

// getResponseSimulationBody gets the simulation response
func getResponseSimulationBody(response []byte) (ResponseSimulationBody, error) {
var responseSimulation ResponseSimulationBody
err := json.Unmarshal(response, &responseSimulation)
if err != nil {
return ResponseSimulationBody{}, err
}
return responseSimulation, nil
}

// SendSimulation sends a simulation test and returns the response
func (sc *Client) SendSimulation(corpName, siteName string, body SimulationBody) (ResponseSimulationBody, error) {
b, err := json.Marshal(body)
if err != nil {
return ResponseSimulationBody{}, err
}
resp, err := sc.doRequest("POST", fmt.Sprintf("/v0/corps/%s/sites/%s/simulator", corpName, siteName), string(b))
if err != nil {
return ResponseSimulationBody{}, err
}
return getResponseSimulationBody(resp)
}
25 changes: 25 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1886,3 +1886,28 @@ func TestCRUDSiteRequestRule(t *testing.T) {
t.Fatal(err)
}
}

func TestSendSimulation(t *testing.T) {
sc := NewTokenClient(testcreds.email, testcreds.token)
corp := testcreds.corp
site := testcreds.site
body := SimulationBody{
// sample request with xss paylaod
SampleRequest: `POST /?q=<script>alert(1)</script> HTTP/1.1\nHost: sample.foo\n\n`,
SampleResponse: `HTTP/1.1 200 OK`,
}
responseSimulation, err := sc.SendSimulation(corp, site, body)
if err != nil {
t.Fatal(err)
}
// check for XSS signal
found := false
for _, signal := range responseSimulation.Data.Signals {
if signal.Type == "XSS" {
found = true
}
}
if !found {
t.Errorf("XSS signal expected, but not found")
}
}
Loading