oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package dashboard
2
3import (
4 "net/url"
5 "strings"
6 "time"
7 "unicode/utf8"
8)
9
10// A document is an object, not an edit log entry. An event is one occurrence.
11type Item struct {
12 ID string `json:"id"`
13 Title string `json:"title"`
14 URL string `json:"url"`
15 Updated time.Time `json:"updated,omitzero"`
16 Start time.Time `json:"start,omitzero"`
17 End time.Time `json:"end,omitzero"`
18 AllDay bool `json:"all_day,omitempty"`
19 StartDate string `json:"start_date,omitempty"`
20 EndDate string `json:"end_date,omitempty"`
21 Calendar string `json:"calendar,omitempty"`
22}
23type Response struct {
24 Version int `json:"v"`
25 Team string `json:"team"`
26 Kind string `json:"kind"`
27 Items []Item `json:"items"`
28 Truncated bool `json:"truncated"`
29 Timezone string `json:"timezone,omitempty"`
30}
31
32func SafeLink(raw, origin string) bool {
33 u, e := url.Parse(raw)
34 return e == nil && u.Scheme+"://"+u.Host == origin && u.User == nil && u.Opaque == "" && strings.HasPrefix(u.Path, "/") && !strings.ContainsAny(raw, "\r\n\\")
35}
36func (r Response) Valid(origin, team, kind string) bool {
37 if r.Version != 1 || r.Team != team || r.Kind != kind || r.Items == nil || len(r.Items) > MaxItems || (kind != "documents" && kind != "events") {
38 return false
39 }
40 if kind == "events" {
41 if len(r.Timezone) == 0 || len(r.Timezone) > 128 {
42 return false
43 }
44 if _, e := time.LoadLocation(r.Timezone); e != nil {
45 return false
46 }
47 }
48 seen := map[string]bool{}
49 for _, i := range r.Items {
50 if len(i.ID) == 0 || len(i.ID) > 256 || seen[i.ID] || !utf8.ValidString(i.Title) || len(i.Title) > 2048 || len(i.Calendar) > 512 || len(i.URL) > 2048 || !SafeLink(i.URL, origin) {
51 return false
52 }
53 seen[i.ID] = true
54 if kind == "documents" && i.Updated.IsZero() {
55 return false
56 }
57 if kind == "events" {
58 if i.Start.IsZero() || !i.End.After(i.Start) {
59 return false
60 }
61 if i.AllDay {
62 s, e := time.Parse("2006-01-02", i.StartDate)
63 d, f := time.Parse("2006-01-02", i.EndDate)
64 if e != nil || f != nil || !d.After(s) {
65 return false
66 }
67 }
68 }
69 }
70 return true
71}
72