Skip to content

Commit 58abd55

Browse files
chore(docs): improve security documentation (#319)
1 parent e2b2de4 commit 58abd55

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ To send `null` instead of a struct, use `param.NullObj[T]()`, where `T` is a str
9393
To send a custom value instead of a struct, use `param.OverrideObj[T](value)`.
9494

9595
To override request structs contain a `.WithExtraFields(map[string]any)` method which can be used to
96-
send non-conforming fields in the request body. Extra fields take higher precedence than normal
97-
fields.
96+
send non-conforming fields in the request body. Extra fields overwrite any struct fields with a matching
97+
key, so only use with trusted data.
9898

9999
```go
100100
params := FooParams{

option/requestoption.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
type RequestOption = requestconfig.RequestOption
2525

2626
// WithBaseURL returns a RequestOption that sets the BaseURL for the client.
27+
//
28+
// For security reasons, ensure that the base URL is trusted.
2729
func WithBaseURL(base string) RequestOption {
2830
u, err := url.Parse(base)
2931
if err != nil {

packages/param/option.go

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package param
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"reflect"
67
"time"
78
)
@@ -78,6 +79,16 @@ func (o Opt[T]) Or(v T) T {
7879
return v
7980
}
8081

82+
func (o Opt[T]) String() string {
83+
if o.IsNull() {
84+
return "null"
85+
}
86+
if s, ok := any(o.Value).(fmt.Stringer); ok {
87+
return s.String()
88+
}
89+
return fmt.Sprintf("%v", o.Value)
90+
}
91+
8192
// This is a sketchy way to implement time Formatting
8293
var timeType = reflect.TypeOf(time.Time{})
8394
var timeTimeValueLoc, _ = reflect.TypeOf(Opt[time.Time]{}).FieldByName("Value")

packages/param/param.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ func (m metadata) GetExtraFields() map[string]any {
9494
return nil
9595
}
9696

97-
func (m *metadata) WithExtraFields(fields map[string]any) {
98-
m.any = metadataExtraFields(fields)
97+
// WithExtraFields adds extra fields to the JSON object.
98+
//
99+
// WithExtraFields will override any existing fields with the same key.
100+
// For security reasons, ensure this is only used with trusted input data.
101+
func (m *metadata) WithExtraFields(extraFields map[string]any) {
102+
m.any = metadataExtraFields(extraFields)
99103
}
100104

101105
func (m *metadata) setMetadata(override any) {

0 commit comments

Comments
 (0)