package server import ( "context" "crypto/tls" "crypto/x509" "encoding/json" "errors" "fmt" "io" "log" "net" "net/http" "net/netip" "net/url" "strings" "time" "pageswiki/internal/calendar" "pageswiki/internal/store" ) const calendarSyncType = "calendar.source.sync" const calendarSweepType = "calendar.source.sweep" var errCalendarSourceBlocked = errors.New("calendar source address blocked: only public HTTPS endpoints are allowed") var errCalendarSourceDNS = errors.New("calendar source DNS lookup failed") // Return only a classified cause, never URL.Error (which can contain tokens). func calendarSourceRequestError(err error) error { if errors.Is(err, errCalendarSourceBlocked) { return errCalendarSourceBlocked } if errors.Is(err, errCalendarSourceDNS) { return errCalendarSourceDNS } var cert *tls.CertificateVerificationError var unknown x509.UnknownAuthorityError if errors.As(err, &cert) || errors.As(err, &unknown) { return errors.New("external calendar TLS certificate verification failed") } var network net.Error if errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &network) && network.Timeout()) { return errors.New("external calendar request timed out") } return errors.New("external calendar connection failed (no reachable address, TLS handshake or redirect failure)") } type calendarSyncPayload struct { Domain string `json:"domain"` Calendar string `json:"calendar"` Force bool `json:"force,omitempty"` } func validCalendarSourceURL(raw string) bool { u, e := url.Parse(raw) if e != nil || len(raw) > 4096 || u.Scheme != "https" || u.Hostname() == "" || u.User != nil || u.Fragment != "" || (u.Port() != "" && u.Port() != "443") { return false } host := strings.TrimSuffix(strings.ToLower(u.Hostname()), ".") if host == "localhost" || strings.HasSuffix(host, ".localhost") { return false } if ip, err := netip.ParseAddr(host); err == nil && !publicCalendarIP(ip) { return false } return true } func publicCalendarIP(ip netip.Addr) bool { ip = ip.Unmap() if !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() { return false } for _, cidr := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32", "64:ff9b::/96", "64:ff9b:1::/48", "2002::/16"} { if netip.MustParsePrefix(cidr).Contains(ip) { return false } } return true } // DNS is checked at dial time and the approved address is pinned for this // connection. No proxy environment or alternate-port bypass is permitted. func calendarSourceClient() *http.Client { tr := &http.Transport{TLSHandshakeTimeout: 10 * time.Second, ResponseHeaderTimeout: 25 * time.Second, DisableKeepAlives: true} tr.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) { host, port, e := net.SplitHostPort(address) if e != nil || port != "443" { return nil, errCalendarSourceBlocked } ips, e := net.DefaultResolver.LookupNetIP(ctx, "ip", host) if e != nil || len(ips) == 0 { return nil, errCalendarSourceDNS } for _, ip := range ips { if !publicCalendarIP(ip) { return nil, errCalendarSourceBlocked } } // Try all validated addresses rather than failing on an unreachable // first IPv6/IPv4 address. Never re-resolve after the safety check. for _, ip := range ips { var conn net.Conn conn, e = (&net.Dialer{Timeout: 2 * time.Second}).DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port)) if e == nil { return conn, nil } if ctx.Err() != nil { return nil, ctx.Err() } } return nil, e } return &http.Client{Transport: tr, Timeout: 45 * time.Second, CheckRedirect: func(req *http.Request, via []*http.Request) error { if len(via) >= 3 || !validCalendarSourceURL(req.URL.String()) { return errors.New("calendar source redirect blocked") } req.Header.Del("Referer") return nil }} } func fetchCalendarSource(ctx context.Context, client *http.Client, c calendar.Calendar) ([]calendar.Event, string, string, bool, error) { if c.Source == nil || !validCalendarSourceURL(c.Source.URL) { return nil, "", "", false, errors.New("invalid calendar source URL") } req, e := http.NewRequestWithContext(ctx, "GET", c.Source.URL, nil) if e != nil { return nil, "", "", false, errors.New("invalid calendar source URL") } req.Header.Set("Accept", "text/calendar") req.Header.Set("User-Agent", "PagesWiki-Calendar/1") if c.Source.ETag != "" { req.Header.Set("If-None-Match", c.Source.ETag) } if c.Source.Modified != "" { req.Header.Set("If-Modified-Since", c.Source.Modified) } response, e := client.Do(req) if e != nil { return nil, "", "", false, calendarSourceRequestError(e) } defer response.Body.Close() if response.StatusCode == 304 { if c.Source.Success == 0 { return nil, "", "", false, errors.New("unexpected 304 before initial synchronization") } return nil, "", "", true, nil } if response.StatusCode != 200 { return nil, "", "", false, errors.New("external calendar returned HTTP " + http.StatusText(response.StatusCode)) } body, e := io.ReadAll(io.LimitReader(response.Body, calendar.MaxFeed+1)) if e != nil { return nil, "", "", false, errors.New("external calendar body could not be read") } now := time.Now().UTC().Truncate(24 * time.Hour) events, e := calendar.ParseExternalFeed(body, now.AddDate(0, 0, -90), now.AddDate(0, 0, 275)) if e != nil { e = fmt.Errorf("ICS validation stage failed: %w", e) } return events, response.Header.Get("ETag"), response.Header.Get("Last-Modified"), false, e } func (s *Server) ensureCalendarSweep() error { var exists bool if e := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM event_definitions WHERE uuid=?)`, calendarSweepType).Scan(&exists); e != nil { return e } if exists { return nil } return s.store.SaveEventDefinition(&store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1}) } func (s *Server) enqueueCalendarSource(domain, id string, force bool) (bool, error) { b, _ := json.Marshal(calendarSyncPayload{Domain: domain, Calendar: id, Force: force}) return s.store.EnqueueEventWithOptions("", calendarSyncType, domain+":"+id, string(b), time.Now(), -10, 1) } func (s *Server) sweepCalendarSources() error { repo, e := s.calendars() if e != nil { return e } domains, e := s.store.ListDomains() if e != nil { return e } for _, d := range domains { if !d.Enabled || !s.systemExtensionEnabled(d.UUID, "organizer.calendar") { continue } cs, e := repo.Sources(d.UUID) if e != nil { return e } for _, c := range cs { if c.Source.Enabled && c.Source.Next <= time.Now().UnixMilli() { if _, e = s.enqueueCalendarSource(d.UUID, c.ID, false); e != nil { return e } } } } return nil } func (s *Server) runCalendarSource(job *store.EventJob) error { var p calendarSyncPayload if e := json.Unmarshal([]byte(job.Payload), &p); e != nil { return e } if !s.systemExtensionEnabled(p.Domain, "organizer.calendar") { return nil } repo, e := s.calendars() if e != nil { return e } c, e := repo.SourceCalendar(p.Domain, p.Calendar) if e != nil { return e } if !c.Source.Enabled || (!p.Force && c.Source.Next > time.Now().UnixMilli()) { return nil } var active bool e = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? AND m.state='active' AND p.state='active' AND d.enabled=1)`, p.Domain, c.Owner).Scan(&active) if e != nil { return e } if !active { return errors.New("calendar source owner is inactive") } ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second) defer cancel() client := calendarSourceClient() if c.Source.Credential != "" { user, password, err := s.store.OpenCalendarCredential(p.Domain, c.ID, c.Source.Credential) if err != nil { return errors.New("calendar credential unavailable") } client.Transport = calendarAuthTransport{base: client.Transport, origin: c.Source.URL, user: user, password: password} } var events []calendar.Event var etag, modified string var unchanged bool var syncErr error var resources map[string]calendar.RemoteResource if c.Source.Kind == "caldav" { events, resources, syncErr = fetchCalDAVSource(ctx, client, repo, p.Domain, c) } else { events, etag, modified, unchanged, syncErr = fetchCalendarSource(ctx, client, c) } if syncErr != nil { // Deliberately omit source URL, credentials, UID, summary and body. log.Printf("calendar source synchronization failed domain=%s calendar=%s protocol=%s stage=fetch-validate: %v", p.Domain, p.Calendar, c.Source.Kind, syncErr) } if s.store.EventCancellationRequested(job.UUID) { return errors.New("calendar synchronization cancelled") } if e = repo.ApplySource(ctx, p.Domain, c, events, etag, modified, unchanged, syncErr, resources); e != nil { return e } return syncErr } func (s *Server) handleCalendarSource(w http.ResponseWriter, r *http.Request, repo *calendar.Repository, domain, id, actor string, active bool) { if !active { calendarError(w, calendar.ErrForbidden) return } c, e := repo.Get(domain, id, actor) if e != nil { calendarError(w, e) return } if c.Owner != actor { calendarError(w, calendar.ErrForbidden) return } switch r.Method { case "PUT": var body struct { Kind string `json:"kind"` Username string `json:"username"` Password string `json:"password"` ClearCredential bool `json:"clear_credential"` URL string `json:"url"` IntervalSeconds int64 `json:"interval_seconds"` Enabled bool `json:"enabled"` Version string `json:"version"` } if !decodeCalendarJSON(w, r, &body) { return } body.URL = strings.TrimSpace(body.URL) if !validCalendarSourceURL(body.URL) { writeEventJSON(w, 422, map[string]string{"error": "Use a public HTTPS ICS URL (port 443, no URL username/password)."}) return } if body.Kind == "" { body.Kind = "ics" } credential := "" if c.Source != nil && c.Source.URL == body.URL && !body.ClearCredential { credential = c.Source.Credential } if body.Username != "" || body.Password != "" { if body.Username == "" || body.Password == "" || strings.Contains(body.Username, ":") || len(body.Username) > 256 || len(body.Password) > 4096 { writeEventJSON(w, 422, map[string]string{"error": "Supply both username and password (username must not contain a colon)."}) return } credential, e = s.store.SealCalendarCredential(domain, id, body.Username, body.Password) if e != nil { calendarError(w, e) return } } c, e = repo.SetSource(r.Context(), domain, id, actor, body.Version, calendar.Source{Kind: body.Kind, Credential: credential, URL: body.URL, IntervalSeconds: body.IntervalSeconds, Enabled: body.Enabled}) if e != nil { calendarError(w, e) return } if body.Enabled { if _, e = s.enqueueCalendarSource(domain, id, true); e != nil { calendarError(w, e) return } s.wakeEventScheduler() } c.Months = nil c.Source.HasCredential = c.Source.Credential != "" c.Source.Credential = "" c.Source.Resources = nil writeEventJSON(w, 200, c) case "POST": if c.Source == nil || !c.Source.Enabled { writeEventJSON(w, 409, map[string]string{"error": "Enable a source first"}) return } queued, e := s.enqueueCalendarSource(domain, id, true) if e != nil { calendarError(w, e) return } s.wakeEventScheduler() writeEventJSON(w, 202, map[string]bool{"queued": queued}) default: w.Header().Set("Allow", "PUT, POST") w.WriteHeader(405) } }