359 lines
14 KiB
Go
359 lines
14 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
const testCard = "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:urn:test:dav-contact\r\nFN:田中\r\nEMAIL;TYPE=work:tanaka@example.invalid\r\nX-PRIVATE:keep\r\nEND:VCARD\r\n"
|
|
|
|
func TestContactDAVConcurrentWritesAndStandaloneExtension(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
for _, id := range []string{"organizer.tasks", "organizer.calendar"} {
|
|
if e := s.setSystemExtensionEnabled(domain, id, false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
cred, pass, err := s.store.CreateDAVCredential(ctx, domain, actor, "phone", true, 1, "contacts")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(cred.ID+":"+pass))
|
|
path := contactDAV + actor + "/contacts/one.vcf"
|
|
if w := cardRequest(s, auth, "PUT", path, testCard, map[string]string{"If-None-Match": "*"}); w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if w := cardRequest(s, "Bearer "+token, "GET", "/_/api/v1/caldav-credentials", "", nil); w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
start := make(chan struct{})
|
|
results := make(chan int, 2)
|
|
for _, name := range []string{"First", "Second"} {
|
|
go func(name string) {
|
|
<-start
|
|
w := cardRequest(s, auth, "PUT", path, strings.Replace(testCard, "FN:田中", "FN:"+name, 1), map[string]string{"If-Match": `"1"`})
|
|
results <- w.Code
|
|
}(name)
|
|
}
|
|
close(start)
|
|
a, b := <-results, <-results
|
|
if !(a == 204 && b == 412 || a == 412 && b == 204) {
|
|
t.Fatal("concurrent overwrite", a, b)
|
|
}
|
|
// Removing the contacts marker must never reinterpret this secret as tasks.
|
|
if _, err = s.store.DB.Exec(`DELETE FROM organizer_contact_credentials WHERE credential_id=?`, cred.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, _, err = s.store.AuthenticateDAV(ctx, domain, cred.ID, pass, ""); err != store.ErrAccessDenied {
|
|
t.Fatal("scope downgrade", err)
|
|
}
|
|
}
|
|
|
|
func TestContactDAVInventoryAndLimits(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
for i := 0; i < 201; i++ {
|
|
_, e := s.store.SaveContact(ctx, domain, actor, store.Contact{UUID: uuid.NewString(), Name: fmt.Sprintf("Person %03d", i)}, false)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
data, err := s.store.ContactDAVInventory(ctx, domain, actor)
|
|
if err != nil || data.More || len(data.Items) != 201 {
|
|
t.Fatal(len(data.Items), err)
|
|
}
|
|
for _, c := range data.Items {
|
|
if c.VCard != "" || c.Size < 1 || c.Version != 1 {
|
|
t.Fatal("inventory must contain metadata only", c)
|
|
}
|
|
}
|
|
book := contactDAV + actor + "/contacts/"
|
|
auth := "Bearer " + token
|
|
w := cardRequest(s, auth, "PROPFIND", book, "", nil)
|
|
if w.Code != 207 || strings.Count(w.Body.String(), "<d:response>") != 202 || strings.Contains(w.Body.String(), "BEGIN:VCARD") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
w = cardRequest(s, auth, "REPORT", book, cardSync("", ""), nil)
|
|
if w.Code != 207 || !strings.Contains(w.Body.String(), "507") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
next := cardRequest(s, auth, "REPORT", book, cardSync(responseSync(t, w), ""), nil)
|
|
if next.Code != 207 || strings.Count(next.Body.String(), "<d:response>") != 1 {
|
|
t.Fatal(next.Code, next.Body)
|
|
}
|
|
query := `<a:addressbook-query xmlns:a="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:"><d:prop><d:getetag/></d:prop></a:addressbook-query>`
|
|
w = cardRequest(s, auth, "REPORT", book, query, nil)
|
|
if w.Code != 507 {
|
|
t.Fatal("must not silently truncate a full query", w.Code)
|
|
}
|
|
for _, tc := range []struct {
|
|
raw, media string
|
|
want int
|
|
}{
|
|
{strings.Replace(testCard, "UID:urn:test:dav-contact\r\n", "", 1), "text/vcard", 403},
|
|
{testCard, "text/vcard; version=3.0", 415},
|
|
{testCard, "text/vcard; charset=shift_jis", 415},
|
|
} {
|
|
w = cardRequest(s, auth, "PUT", book+"invalid.vcf", tc.raw, map[string]string{"If-None-Match": "*", "Content-Type": tc.media})
|
|
if w.Code != tc.want {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestContactDAVUnicodeAndUnknownProperties(t *testing.T) {
|
|
card := store.Contact{VCard: strings.Replace(testCard, "X-PRIVATE:keep", "X-PRIVATE:CAFÉ", 1)}
|
|
for _, tc := range []struct {
|
|
collation, query string
|
|
want bool
|
|
}{
|
|
{"i;unicode-casemap", "cafe\u0301", true},
|
|
{"i;unicode-casemap", "CAFÉ", true},
|
|
{"i;octet", "café", false},
|
|
{"i;octet", "CAFÉ", true},
|
|
} {
|
|
var filter davNode
|
|
raw := `<a:filter xmlns:a="urn:ietf:params:xml:ns:carddav"><a:prop-filter name="X-PRIVATE"><a:text-match collation="` + tc.collation + `" match-type="equals">` + tc.query + `</a:text-match></a:prop-filter></a:filter>`
|
|
if e := xml.Unmarshal([]byte(raw), &filter); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if !validContactFilter(filter) || matchContactFilter(card, filter) != tc.want {
|
|
t.Fatal(tc)
|
|
}
|
|
}
|
|
}
|
|
|
|
func cardRequest(s *Server, auth, method, path, body string, headers map[string]string) *httptest.ResponseRecorder {
|
|
if headers == nil {
|
|
headers = map[string]string{}
|
|
}
|
|
if _, ok := headers["Content-Type"]; !ok {
|
|
headers["Content-Type"] = "text/vcard"
|
|
}
|
|
return davTaskRequest(s, auth, method, path, body, headers)
|
|
}
|
|
func cardSync(token string, limit string) string {
|
|
return `<d:sync-collection xmlns:d="DAV:"><d:sync-token>` + xmlText(token) + `</d:sync-token><d:sync-level>1</d:sync-level><d:prop><d:getetag/></d:prop>` + limit + `</d:sync-collection>`
|
|
}
|
|
func responseSync(t *testing.T, w *httptest.ResponseRecorder) string {
|
|
t.Helper()
|
|
var value struct {
|
|
Token string `xml:"sync-token"`
|
|
}
|
|
if e := xml.Unmarshal(w.Body.Bytes(), &value); e != nil || value.Token == "" {
|
|
t.Fatal(e, w.Body)
|
|
}
|
|
return value.Token
|
|
}
|
|
func TestContactDAVRoundTripAndScopes(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
book := contactDAV + actor + "/contacts/"
|
|
cred, password, err := s.store.CreateDAVCredential(ctx, domain, actor, "phone", true, 90, "contacts")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(cred.ID+":"+password))
|
|
request := func(method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
w := cardRequest(s, auth, method, path, body, h)
|
|
if w.Code != want {
|
|
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
|
|
}
|
|
return w
|
|
}
|
|
options := request("OPTIONS", contactDAV, "", nil, 204)
|
|
if options.Header().Get("DAV") != "1, addressbook-access" {
|
|
t.Fatal(options.Header())
|
|
}
|
|
home := request("PROPFIND", contactDAV, `<d:propfind xmlns:d="DAV:" xmlns:a="urn:ietf:params:xml:ns:carddav"><d:prop><d:current-user-principal/><a:addressbook-home-set/></d:prop></d:propfind>`, nil, 207)
|
|
if !strings.Contains(home.Body.String(), contactDAV+actor+"/") {
|
|
t.Fatal(home.Body)
|
|
}
|
|
discovery := request("PROPFIND", contactDAV+actor+"/", "", nil, 207)
|
|
if !strings.Contains(discovery.Body.String(), book) {
|
|
t.Fatal(discovery.Body)
|
|
}
|
|
empty := request("REPORT", book, cardSync("", ""), nil, 207)
|
|
emptyToken := responseSync(t, empty)
|
|
path := book + "client-contact.vcf"
|
|
request("PUT", path, testCard, map[string]string{"If-None-Match": "*"}, 201)
|
|
get := request("GET", path, "", nil, 200)
|
|
tag := get.Header().Get("ETag")
|
|
request("GET", path, "", map[string]string{"If-None-Match": tag}, 304)
|
|
request("PUT", path, testCard, map[string]string{"If-None-Match": "*"}, 412)
|
|
request("PUT", path, testCard, nil, 428)
|
|
request("PUT", path, testCard, map[string]string{"If-Match": `"999"`}, 412)
|
|
request("PUT", book+"duplicate.vcf", testCard, map[string]string{"If-None-Match": "*"}, 403)
|
|
c, err := s.store.ContactDAVResource(ctx, domain, actor, "client-contact.vcf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.Name = "Webで変更"
|
|
c, err = s.store.SaveContact(ctx, domain, actor, c, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
delta := request("REPORT", book, cardSync(emptyToken, ""), nil, 207)
|
|
if !strings.Contains(delta.Body.String(), path) || strings.Contains(delta.Body.String(), "Webで変更") {
|
|
t.Fatal(delta.Body)
|
|
}
|
|
synced := responseSync(t, delta)
|
|
multi := `<a:addressbook-multiget xmlns:a="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:"><d:prop><d:getetag/><a:address-data/></d:prop><d:href>` + path + `</d:href><d:href>` + book + `missing.vcf</d:href></a:addressbook-multiget>`
|
|
got := request("REPORT", book, multi, nil, 207)
|
|
if !strings.Contains(got.Body.String(), "Webで変更") || !strings.Contains(got.Body.String(), "X-PRIVATE:keep") || !strings.Contains(got.Body.String(), "404 Not Found") {
|
|
t.Fatal(got.Body)
|
|
}
|
|
request("DELETE", path, "", map[string]string{"If-Match": store.ContactETag(c)}, 204)
|
|
request("GET", path, "", nil, 404)
|
|
delta = request("REPORT", book, cardSync(synced, ""), nil, 207)
|
|
if !strings.Contains(delta.Body.String(), path) || !strings.Contains(delta.Body.String(), "404 Not Found") {
|
|
t.Fatal(delta.Body)
|
|
}
|
|
request("REPORT", book, cardSync("urn:foreign:123", ""), nil, 403)
|
|
request("PUT", path, testCard, map[string]string{"If-None-Match": "*"}, 201)
|
|
c, err = s.store.ContactDAVResource(ctx, domain, actor, "client-contact.vcf")
|
|
if err != nil || c.Deleted {
|
|
t.Fatal(c, err)
|
|
}
|
|
if w := cardRequest(s, auth, "GET", contactsAPI+"items", "", nil); w.Code != 401 {
|
|
t.Fatal("DAV credential leaked to API", w.Code)
|
|
}
|
|
if w := cardRequest(s, auth, "PROPFIND", taskDAVRoot, "", nil); w.Code != 401 {
|
|
t.Fatal("contact credential grants tasks", w.Code)
|
|
}
|
|
// The legacy 'all' scope remains calendar+tasks, not newly added contacts.
|
|
all, pass, e := s.store.CreateDAVCredential(ctx, domain, actor, "old", true, 1, "all")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
otherAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(all.ID+":"+pass))
|
|
if w := cardRequest(s, otherAuth, "PROPFIND", contactDAV, "", nil); w.Code != 401 {
|
|
t.Fatal("old authority expanded", w.Code)
|
|
}
|
|
request("PROPFIND", contactDAV+uuid.NewString()+"/contacts/", "", nil, 404)
|
|
if e = s.store.RevokeTaskDAVCredential(ctx, domain, actor, cred.ID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
request("GET", path, "", nil, 401)
|
|
if _, e = s.store.SaveContactDAV(ctx, domain, actor, cred.ID, "client-contact.vcf", testCard, c.Version, false); e != store.ErrAccessDenied {
|
|
t.Fatal("revoked writer", e)
|
|
}
|
|
// A normal session can still edit its own address book.
|
|
if w := cardRequest(s, "Bearer "+token, "GET", path, "", nil); w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|
|
|
|
func TestContactDAVPaginationHistoryAndBoundaries(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
auth := "Bearer " + token
|
|
book := contactDAV + actor + "/contacts/"
|
|
var ids []string
|
|
for i := 0; i < 3; i++ {
|
|
id := uuid.NewString()
|
|
ids = append(ids, id)
|
|
_, e := s.store.SaveContact(ctx, domain, actor, store.Contact{UUID: id, Name: "Person"}, false)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
first := cardRequest(s, auth, "REPORT", book, cardSync("", `<d:limit><d:nresults>1</d:nresults></d:limit>`), nil)
|
|
if first.Code != 207 || !strings.Contains(first.Body.String(), "507 Insufficient Storage") {
|
|
t.Fatal(first.Code, first.Body)
|
|
}
|
|
second := cardRequest(s, auth, "REPORT", book, cardSync(responseSync(t, first), ""), nil)
|
|
if second.Code != 207 || strings.Contains(second.Body.String(), "507 Insufficient Storage") {
|
|
t.Fatal(second.Code, second.Body)
|
|
}
|
|
var r struct {
|
|
Responses []struct {
|
|
Href string `xml:"href"`
|
|
} `xml:"response"`
|
|
}
|
|
if e := xml.Unmarshal(second.Body.Bytes(), &r); e != nil || len(r.Responses) != 2 {
|
|
t.Fatal(e, second.Body)
|
|
}
|
|
c, e := s.store.Contact(ctx, domain, actor, ids[0])
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c.Name = "Changed"
|
|
c, e = s.store.SaveContact(ctx, domain, actor, c, false)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
h, e := s.store.ContactHistory(ctx, domain, actor, c.UUID, 0)
|
|
if e != nil || len(h) != 2 {
|
|
t.Fatal(h, e)
|
|
}
|
|
restored, e := s.store.RestoreContactRevision(ctx, domain, actor, c.UUID, 1, c.Version)
|
|
if e != nil || restored.Name != "Person" || restored.Version != 3 {
|
|
t.Fatal(restored, e)
|
|
}
|
|
if _, e = s.store.RestoreContactRevision(ctx, domain, actor, c.UUID, 1, c.Version); e != store.ErrTaskConflict {
|
|
t.Fatal("stale restore", e)
|
|
}
|
|
raw, e := s.store.ExportContacts(ctx, domain, actor)
|
|
if e != nil || strings.Count(raw, "BEGIN:VCARD") != 3 {
|
|
t.Fatal(e, raw)
|
|
}
|
|
history := cardRequest(s, auth, "GET", contactsAPI+"items/"+c.UUID+"/history", "", nil)
|
|
if history.Code != 200 {
|
|
t.Fatal(history.Code, history.Body)
|
|
}
|
|
preview := cardRequest(s, auth, "GET", contactsAPI+"items/"+c.UUID+"/history/1", "", nil)
|
|
if preview.Code != 200 || !strings.Contains(preview.Body.String(), "Person") {
|
|
t.Fatal(preview.Code, preview.Body)
|
|
}
|
|
restoreBody, _ := json.Marshal(map[string]int64{"version": restored.Version, "revision": 1})
|
|
restore := cardRequest(s, auth, "POST", contactsAPI+"items/"+c.UUID+"/restore", string(restoreBody), map[string]string{"If-Match": store.ContactETag(restored)})
|
|
if restore.Code != 200 {
|
|
t.Fatal(restore.Code, restore.Body)
|
|
}
|
|
query := `<a:addressbook-query xmlns:a="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:"><d:prop><d:getetag/></d:prop><a:filter><a:prop-filter name="FN"><a:text-match match-type="equals">Person</a:text-match></a:prop-filter></a:filter></a:addressbook-query>`
|
|
found := cardRequest(s, auth, "REPORT", book, query, nil)
|
|
if found.Code != 207 {
|
|
t.Fatal(found.Code, found.Body)
|
|
}
|
|
for _, body := range []string{strings.Replace(query, `name="FN"`, `name="BAD NAME"`, 1), strings.Replace(query, `match-type="equals"`, `collation="unsupported"`, 1)} {
|
|
w := cardRequest(s, auth, "REPORT", book, body, nil)
|
|
if w.Code != 403 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|
|
xmlBomb := `<!DOCTYPE x [<!ENTITY x "bomb">]><d:propfind xmlns:d="DAV:"/>`
|
|
if w := cardRequest(s, auth, "PROPFIND", book, xmlBomb, nil); w.Code != 400 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
foreign := `<a:addressbook-multiget xmlns:a="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:"><d:prop><d:getetag/></d:prop><d:href>https://evil.invalid` + book + `a.vcf</d:href></a:addressbook-multiget>`
|
|
if w := cardRequest(s, auth, "REPORT", book, foreign, nil); w.Code != 400 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
read, pass, e := s.store.CreateDAVCredential(ctx, domain, actor, "readonly", false, 1, "contacts")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
readonly := "Basic " + base64.StdEncoding.EncodeToString([]byte(read.ID+":"+pass))
|
|
if w := cardRequest(s, readonly, "PUT", book+"new.vcf", testCard, map[string]string{"If-None-Match": "*"}); w.Code != 403 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if e = s.setSystemExtensionEnabled(domain, "organizer.contacts", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if w := cardRequest(s, auth, "PROPFIND", book, "", nil); w.Code != 404 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
}
|