-
Notifications
You must be signed in to change notification settings - Fork 589
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
Refactor span formatter function to use a html template to generate autoescaped HTML #5280
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm | |
### Added | ||
|
||
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108) | ||
- Add a new package `go.opentelemetry.io/contrib/zpages/internal` to enable the use of internal.Templates within the parseTemplate function. | ||
- Refactored the spanRowFormatter function to utilize HTML templates for auto-escaping HTML, ensuring protection against Cross-site Scripting (XSS) vulnerabilities. (#4451) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rewrite this in terms that an end-user will understand and move to the "### Fixed" section. Reference: https://keepachangelog.com/en/1.1.0/ |
||
|
||
### Removed | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
package zpages // import "go.opentelemetry.io/contrib/zpages" | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"io" | ||
|
@@ -60,7 +61,6 @@ | |
return template.Must(template.New(name).Funcs(templateFunctions).Parse(string(text))) | ||
} | ||
|
||
//nolint:gosec // G203: The used method does not auto-escape HTML. Tracked under https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4451. | ||
func spanRowFormatter(r spanRow) template.HTML { | ||
if !r.SpanContext.IsValid() { | ||
return "" | ||
|
@@ -69,10 +69,21 @@ | |
if r.SpanContext.IsSampled() { | ||
col = "blue" | ||
} | ||
|
||
var tpl string | ||
|
||
if r.ParentSpanContext.IsValid() { | ||
return template.HTML(fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s parent_span_id: %s`, col, r.SpanContext.TraceID(), r.SpanContext.SpanID(), r.ParentSpanContext.SpanID())) | ||
tpl = fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s parent_span_id: %s`, col, template.HTMLEscapeString(r.SpanContext.TraceID().String()), template.HTMLEscapeString(r.SpanContext.SpanID().String()), template.HTMLEscapeString(r.ParentSpanContext.SpanID().String())) | ||
} else { | ||
tpl = fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s `, col, template.HTMLEscapeString(r.SpanContext.TraceID().String()), template.HTMLEscapeString(r.SpanContext.SpanID().String())) | ||
} | ||
|
||
t := template.Must(template.New("spanRow").Parse(tpl)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will panic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what would you advise i do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really grateful for your corrections @MrAlias There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be better if i just |
||
var buf bytes.Buffer | ||
if err := t.Execute(&buf, nil); err != nil { | ||
return template.HTML(template.HTMLEscapeString(fmt.Sprintf("Error executing template: %s", err.Error()))) | ||
} | ||
return template.HTML(fmt.Sprintf(`trace_id: <b style="color:%s">%s</b> span_id: %s`, col, r.SpanContext.TraceID(), r.SpanContext.SpanID())) | ||
return template.HTML(template.HTMLEscapeString(buf.String())) | ||
} | ||
|
||
func even(x int) bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be removed. It is not included in these changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted