File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change
1
+ extension Timestamp
2
+ {
3
+ /// Timestamp sanity is an often-overlooked aspect of server security. Because Swift’s
4
+ /// “crash early” philosophy around trapping arithmetic overflows is very bad for servers,
5
+ /// server applications should always perform sanity checks on timestamps before using them
6
+ /// to perform **any** kind of date computation.
7
+ @frozen public
8
+ enum Sanity
9
+ {
10
+ /// The year is in the given range.
11
+ case year( in: ClosedRange < Year > )
12
+ /// No sanity checks will be performed.
13
+ case unchecked
14
+ }
15
+ }
Original file line number Diff line number Diff line change @@ -64,9 +64,22 @@ extension UnixInstant
64
64
return . init( second: second, nanoseconds: milliseconds * 1_000_000 )
65
65
}
66
66
67
+ /// Y3K bug!
67
68
@inlinable public
68
- init ? ( utc timestamp: Timestamp . Components )
69
+ init ? ( utc timestamp: Timestamp . Components ,
70
+ sanity checks: Timestamp . Sanity = . year( in: 1970 ... 2970 ) )
69
71
{
72
+ switch checks
73
+ {
74
+ case . unchecked: break
75
+ case . year( in: let range) :
76
+ guard range. contains ( timestamp. date. year)
77
+ else
78
+ {
79
+ return nil
80
+ }
81
+ }
82
+
70
83
var time : tm = . init(
71
84
tm_sec: timestamp. time. second,
72
85
tm_min: timestamp. time. minute,
You can’t perform that action at this time.
0 commit comments