Просмотр исходного кода

Ensure empty times are not serialized on orders

The `omitempty` annotation on Go structs will not elide zero-valued time
values if they're not pointers. Setting `omitzero` as well will ensure
we do not send these values if they're not.

Per RFC 8555, these fields are optional and are requested values by the
user; it makes sense that server implementers would reject
less-than-useful time values if present. Additionally, RFC 3339 defines
date values as being between 0000 and 9999, making it ambiguous outside
of the Go ecosystem if 0001-01-01T00:00:00Z would be the zero date.
Notably, RFC 3339 Appendix B remarks:

> which may be used to obtain the day of the week for dates on or
> after 0000-03-01

which suggests dates in the year 0000 would work and a reasonable
implementation could conclude 0001 is not the zero year.

See also: https://datatracker.ietf.org/doc/html/rfc8555#section-7.1.3
See also: https://datatracker.ietf.org/doc/html/rfc3339#appendix-B
See also: https://github.com/openbao/openbao/issues/3147

Signed-off-by: Alexander Scheel <alexander.m.scheel@gmail.com>
Alexander Scheel 6 месяцев назад
Родитель
Сommit
52ab2a5329
2 измененных файлов с 18 добавлено и 2 удалено
  1. 2 2
      acme/api/order.go
  2. 16 0
      acme/api/order_test.go

+ 2 - 2
acme/api/order.go

@@ -26,8 +26,8 @@ import (
 // NewOrderRequest represents the body for a NewOrder request.
 type NewOrderRequest struct {
 	Identifiers []acme.Identifier `json:"identifiers"`
-	NotBefore   time.Time         `json:"notBefore,omitempty"`
-	NotAfter    time.Time         `json:"notAfter,omitempty"`
+	NotBefore   time.Time         `json:"notBefore,omitempty,omitzero"`
+	NotAfter    time.Time         `json:"notAfter,omitempty,omitzero"`
 }
 
 // Validate validates a new-order request body.

+ 16 - 0
acme/api/order_test.go

@@ -12,6 +12,7 @@ import (
 	"net/http/httptest"
 	"net/url"
 	"reflect"
+	"strings"
 	"testing"
 	"time"
 
@@ -2747,3 +2748,18 @@ func TestTrimIfWildcard(t *testing.T) {
 		})
 	}
 }
+
+func TestNewOrderRequest_SkipSerialization(t *testing.T) {
+	order := NewOrderRequest{
+		Identifiers: []acme.Identifier{
+			{Type: "dns", Value: "example.com"},
+		},
+	}
+	assert.NoError(t, order.Validate())
+
+	data, err := json.Marshal(order)
+	assert.NoError(t, err)
+
+	assert.False(t, strings.Contains(string(data), "not_before"))
+	assert.False(t, strings.Contains(string(data), "not_after"))
+}