Skip to content

Commit

Permalink
Merge pull request trustbloc#1757 from bstasyszyn/escape-doc-for-mongo
Browse files Browse the repository at this point in the history
fix: Escape "." characters in document fields before storing in Mongo
  • Loading branch information
bstasyszyn authored Sep 19, 2024
2 parents 8ea8e87 + 349bff4 commit 83dd445
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/storage/mongodb/ldstore/context_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
ldcontext "github.com/trustbloc/did-go/doc/ld/context"
ldstore "github.com/trustbloc/did-go/doc/ld/store"
"github.com/trustbloc/kms-go/spi/storage"
"github.com/trustbloc/vcs/pkg/storage/mongodb/internal"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -212,7 +213,6 @@ func (s *ContextStore) Import(documents []ldcontext.Document) error {

for _, doc := range targetDocs {
_, err := collection.InsertOne(ctxWithTimeout, doc.bsonDoc)

if err != nil {
if mongo.IsDuplicateKeyError(err) {
s.cache.Add(doc.remoteDoc.ContextURL, doc.remoteDoc)
Expand Down Expand Up @@ -317,9 +317,14 @@ func getJSONLDRemoteDocumentBytes(d ldcontext.Document) ([]byte, error) {
return nil, fmt.Errorf("document from reader: %w", err)
}

mongoDoc, err := internal.PrepareDataForBSONStorage(content)
if err != nil {
return nil, fmt.Errorf("prepare data for bson storage: %w", err)
}

rd := &jsonld.RemoteDocument{
DocumentURL: d.DocumentURL,
Document: content,
Document: mongoDoc,
ContextURL: d.URL,
}

Expand All @@ -337,6 +342,11 @@ func mapToBSONRemoteDocument(rd *jsonld.RemoteDocument) (*bsonRemoteDocument, er
return nil, err
}

content, err = internal.PrepareDataForBSONStorage(content)
if err != nil {
return nil, err
}

return &bsonRemoteDocument{
DocumentURL: rd.DocumentURL,
Document: content,
Expand Down
3 changes: 3 additions & 0 deletions pkg/storage/mongodb/ldstore/context_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
},
"...": {
"@id": "https://www.iana.org/assignments/jwt#..."
}
}
}`
Expand Down

0 comments on commit 83dd445

Please sign in to comment.