Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type homeBookmark struct {
|
|
Label string `json:"label"`
|
|
Href string `json:"href"`
|
|
Kind string `json:"kind,omitempty"`
|
|
Children bool `json:"children,omitempty"`
|
|
}
|
|
|
|
func bookmarksFromForm(r *http.Request) ([]homeBookmark, error) {
|
|
labels, targets, kinds := r.Form["bookmark_label"], r.Form["bookmark_target"], r.Form["bookmark_kind"]
|
|
if len(labels) > 64 || len(labels) != len(targets) || len(labels) != len(kinds) {
|
|
return nil, errors.New("invalid bookmark rows")
|
|
}
|
|
if children := r.Form["bookmark_children"]; len(children) != 0 && len(children) != len(labels) {
|
|
return nil, errors.New("invalid bookmark toggle rows")
|
|
}
|
|
bookmarks := []homeBookmark{}
|
|
sections := 0
|
|
for i, label := range labels {
|
|
label, target := strings.TrimSpace(label), strings.TrimSpace(targets[i])
|
|
if label == "" && target == "" {
|
|
continue
|
|
}
|
|
if label == "" || len(label) > 200 || len(target) > 4096 {
|
|
return nil, errors.New("invalid bookmark")
|
|
}
|
|
switch kinds[i] {
|
|
case "section":
|
|
sections++
|
|
if sections > 16 || strings.ContainsAny(target, "?#\\\r\n") {
|
|
return nil, errors.New("invalid bookmark section")
|
|
}
|
|
children := len(r.Form["bookmark_children"]) > i && r.Form["bookmark_children"][i] == "true"
|
|
if children && target == "" {
|
|
return nil, errors.New("missing section parent path")
|
|
}
|
|
if target != "" {
|
|
target = (&url.URL{Path: path.Clean("/" + strings.TrimPrefix(target, "/"))}).String()
|
|
}
|
|
bookmarks = append(bookmarks, homeBookmark{Label: label, Href: target, Kind: "section", Children: children})
|
|
continue
|
|
case "path":
|
|
if target == "" || strings.ContainsAny(target, "?#\\\r\n") {
|
|
return nil, errors.New("invalid bookmark path")
|
|
}
|
|
target = path.Clean("/" + strings.TrimPrefix(target, "/"))
|
|
// Store an escaped local URL so spaces, % and Unicode retain their
|
|
// literal semantic Path meaning when the link is opened.
|
|
target = (&url.URL{Path: target}).String()
|
|
case "url":
|
|
if !validHomeBookmarkURL(target) {
|
|
return nil, errors.New("invalid bookmark URL")
|
|
}
|
|
default:
|
|
return nil, errors.New("invalid bookmark kind")
|
|
}
|
|
bookmarks = append(bookmarks, homeBookmark{Label: label, Href: target, Kind: kinds[i]})
|
|
}
|
|
return bookmarks, nil
|
|
}
|
|
|
|
func (s *Server) managedBookmarks(domainUUID string) []homeBookmark {
|
|
items := s.homeBookmarks(domainUUID)
|
|
var out []homeBookmark
|
|
for _, item := range items {
|
|
kind := item.Kind
|
|
if kind == "" {
|
|
kind = "url"
|
|
if strings.HasPrefix(item.Href, "/") {
|
|
kind = "path"
|
|
}
|
|
}
|
|
target := item.Href
|
|
if kind == "path" || kind == "section" {
|
|
if u, err := url.Parse(target); err == nil {
|
|
target = u.Path
|
|
}
|
|
}
|
|
out = append(out, homeBookmark{Label: item.Label, Href: target, Kind: kind, Children: item.Children})
|
|
}
|
|
return out
|
|
}
|