Skip to content

Commit b54b0aa

Browse files
committed
make start/endtime check more strict
1 parent aaadc6a commit b54b0aa

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

coderd/insights.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package coderd
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"time"
@@ -89,6 +90,10 @@ func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) {
8990
return
9091
}
9192

93+
if !verifyInsightsStartAndEndTime(ctx, rw, startTime, endTime) {
94+
return
95+
}
96+
9297
// Should we verify all template IDs exist, or just return no rows?
9398
// _, err := api.Database.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{
9499
// IDs: templateIDs,
@@ -201,6 +206,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
201206
return
202207
}
203208

209+
if !verifyInsightsStartAndEndTime(ctx, rw, startTime, endTime) {
210+
return
211+
}
212+
204213
// Should we verify all template IDs exist, or just return no rows?
205214
// _, err := api.Database.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{
206215
// IDs: templateIDs,
@@ -313,3 +322,53 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
313322
}
314323
httpapi.Write(ctx, rw, http.StatusOK, resp)
315324
}
325+
326+
func verifyInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, startTime, endTime time.Time) bool {
327+
for _, v := range []struct {
328+
name string
329+
t time.Time
330+
}{
331+
{"start_time", startTime},
332+
{"end_time", endTime},
333+
} {
334+
if v.t.IsZero() {
335+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
336+
Message: "Query parameter has invalid value.",
337+
Validations: []codersdk.ValidationError{
338+
{
339+
Field: v.name,
340+
Detail: "must be not be zero",
341+
},
342+
},
343+
})
344+
return false
345+
}
346+
h, m, s := v.t.Clock()
347+
if h != 0 || m != 0 || s != 0 {
348+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
349+
Message: "Query parameter has invalid value.",
350+
Validations: []codersdk.ValidationError{
351+
{
352+
Field: v.name,
353+
Detail: "clock must be 00:00:00",
354+
},
355+
},
356+
})
357+
return false
358+
}
359+
}
360+
if endTime.Before(startTime) {
361+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
362+
Message: "Query parameter has invalid value.",
363+
Validations: []codersdk.ValidationError{
364+
{
365+
Field: "end_time",
366+
Detail: "must be after start_time",
367+
},
368+
},
369+
})
370+
return false
371+
}
372+
373+
return true
374+
}

0 commit comments

Comments
 (0)