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

fix: mock body render missing context #630

Merged
merged 2 commits into from
Feb 25, 2025
Merged

Conversation

LinuxSuRen
Copy link
Owner

We highly recommend you read the contributor's documentation before starting the review process especially since this is your first contribution to this project.

It was updated on 2024/5/27

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

@LinuxSuRen LinuxSuRen added the bug Something isn't working label Feb 25, 2025
@LinuxSuRen LinuxSuRen requested a review from yuluo-yx as a code owner February 25, 2025 01:10
Copy link

There are 1 test cases, failed count 0:

Name Average Max Min Count Error
9.875106ms 10.465689ms 9.245315ms 3 0

Reported by api-testing.

} else {
if h.item.Response.BodyData, err = render.RenderAsBytes("start-item", h.item.Response.Body, h.item); err != nil {
fmt.Printf("failed to render body: %v", err)
}

Check warning

Code scanning / CodeQL

Reflected cross-site scripting Medium

Cross-site scripting vulnerability due to
user-provided value
.

Copilot Autofix AI 4 days ago

To fix the reflected cross-site scripting vulnerability, we need to ensure that any user-controlled data is properly sanitized or escaped before being written to the HTTP response. In this case, we can use the html.EscapeString function from the html package to escape any potentially dangerous characters in the response body.

  • We will modify the writeResponse function to escape the data before writing it to the response.
  • This change will be made in the pkg/mock/in_memory.go file.
  • We need to import the html package to use the html.EscapeString function.
Suggested changeset 1
pkg/mock/in_memory.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/pkg/mock/in_memory.go b/pkg/mock/in_memory.go
--- a/pkg/mock/in_memory.go
+++ b/pkg/mock/in_memory.go
@@ -40,2 +40,3 @@
 	"github.com/gorilla/mux"
+	"html"
 )
@@ -365,3 +366,4 @@
 	if err == nil {
-		w.Write(data)
+		escapedData := html.EscapeString(string(data))
+		w.Write([]byte(escapedData))
 	} else {
EOF
@@ -40,2 +40,3 @@
"github.com/gorilla/mux"
"html"
)
@@ -365,3 +366,4 @@
if err == nil {
w.Write(data)
escapedData := html.EscapeString(string(data))
w.Write([]byte(escapedData))
} else {
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.22% (target: -1.00%) 71.47% (target: 80.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (cc5a463) 19110 7873 41.20%
Head commit (0122bbc) 19399 (+289) 8035 (+162) 41.42% (+0.22%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#630) 375 268 71.47%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

@LinuxSuRen LinuxSuRen merged commit 6908433 into master Feb 25, 2025
20 of 21 checks passed
@LinuxSuRen LinuxSuRen deleted the fix/mock-body-render branch February 25, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant