Skip to content

chore(docs): improve security documentation #319

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

Merged
merged 1 commit into from
Mar 25, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ To send `null` instead of a struct, use `param.NullObj[T]()`, where `T` is a str
To send a custom value instead of a struct, use `param.OverrideObj[T](value)`.

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

```go
params := FooParams{
Expand Down
2 changes: 2 additions & 0 deletions option/requestoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
type RequestOption = requestconfig.RequestOption

// WithBaseURL returns a RequestOption that sets the BaseURL for the client.
//
// For security reasons, ensure that the base URL is trusted.
func WithBaseURL(base string) RequestOption {
u, err := url.Parse(base)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions packages/param/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"encoding/json"
"fmt"
"reflect"
"time"
)
Expand Down Expand Up @@ -78,6 +79,16 @@ func (o Opt[T]) Or(v T) T {
return v
}

func (o Opt[T]) String() string {
if o.IsNull() {
return "null"
}
if s, ok := any(o.Value).(fmt.Stringer); ok {
return s.String()
}
return fmt.Sprintf("%v", o.Value)
}

// This is a sketchy way to implement time Formatting
var timeType = reflect.TypeOf(time.Time{})
var timeTimeValueLoc, _ = reflect.TypeOf(Opt[time.Time]{}).FieldByName("Value")
Expand Down
8 changes: 6 additions & 2 deletions packages/param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ func (m metadata) GetExtraFields() map[string]any {
return nil
}

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

func (m *metadata) setMetadata(override any) {
Expand Down