diff --git a/services/collaboration/pkg/middleware/wopicontext.go b/services/collaboration/pkg/middleware/wopicontext.go index deecbf6ab8f..c6d3fa7365f 100644 --- a/services/collaboration/pkg/middleware/wopicontext.go +++ b/services/collaboration/pkg/middleware/wopicontext.go @@ -128,6 +128,20 @@ func WopiContextAuthMiddleware(cfg *config.Config, st microstore.Store, next htt claims.WopiContext.AccessToken = wopiContextAccessToken + // The view only token is encrypted the same way as the access token + // (see GenerateWopiToken). Decrypt it so downstream consumers get the + // plaintext reva token. It is only set for view only shares, so skip + // the decryption when it is empty. + if claims.WopiContext.ViewOnlyToken != "" { + wopiContextViewOnlyToken, err := DecryptAES([]byte(cfg.Wopi.Secret), claims.WopiContext.ViewOnlyToken) + if err != nil { + wopiLogger.Error().Err(err).Msg("failed to decrypt view only token") + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + claims.WopiContext.ViewOnlyToken = wopiContextViewOnlyToken + } + ctx = context.WithValue(ctx, wopiContextKey, claims.WopiContext) // authentication for the CS3 api ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, claims.WopiContext.AccessToken) @@ -201,6 +215,18 @@ func GenerateWopiToken(wopiContext WopiContext, cfg *config.Config, st microstor return "", 0, err } + // The view only token is a reva token that impersonates the resource owner. + // It must never be exposed in plaintext inside the (readable) WOPI JWT, so + // encrypt it the same way as the access token. It is only set for view only + // shares, so skip the encryption when it is empty. + if wopiContext.ViewOnlyToken != "" { + cryptedViewOnlyToken, err := EncryptAES([]byte(cfg.Wopi.Secret), wopiContext.ViewOnlyToken) + if err != nil { + return "", 0, err + } + wopiContext.ViewOnlyToken = cryptedViewOnlyToken + } + cs3Claims := &jwt.RegisteredClaims{} _, err = jwt.ParseWithClaims(wopiContext.AccessToken, cs3Claims, func(token *jwt.Token) (interface{}, error) { if token.Method != jwt.SigningMethodHS256 { diff --git a/services/collaboration/pkg/middleware/wopicontext_test.go b/services/collaboration/pkg/middleware/wopicontext_test.go index b90be6d9233..cb98ed7ca27 100644 --- a/services/collaboration/pkg/middleware/wopicontext_test.go +++ b/services/collaboration/pkg/middleware/wopicontext_test.go @@ -11,6 +11,7 @@ import ( appprovider "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" + "github.com/golang-jwt/jwt/v5" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" @@ -168,6 +169,60 @@ var _ = Describe("Wopi Context Middleware", func() { mw.ServeHTTP(resp, req) Expect(resp.Code).To(Equal(http.StatusOK)) }) + It("Should encrypt the ViewOnlyToken inside the WOPI token and restore it on the way in", func() { + // a known, recognizable plaintext so we can assert it is not readable in the JWT + const viewOnlyPlaintext = "my-secret-view-only-token" + + // capture the WopiContext the middleware hands to the next handler + var captured middleware.WopiContext + var capturedErr error + capturingNext := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + captured, capturedErr = middleware.WopiContextFromCtx(r.Context()) + w.WriteHeader(http.StatusOK) + }) + capturingMw := middleware.WopiContextAuthMiddleware(cfg, nil, capturingNext) + + token, err := tknMngr.MintToken(ctx, user, nil) + Expect(err).ToNot(HaveOccurred()) + + wopiContext := middleware.WopiContext{ + AccessToken: token, + ViewOnlyToken: viewOnlyPlaintext, + ViewMode: appprovider.ViewMode_VIEW_MODE_VIEW_ONLY, + FileReference: &providerv1beta1.Reference{ + ResourceId: rid, + Path: ".", + }, + } + wopiToken, ttl, err := middleware.GenerateWopiToken(wopiContext, cfg, nil) + Expect(err).ToNot(HaveOccurred()) + + // Security property: the plaintext view-only token must not be recoverable + // from the (readable) JWT. Parse the JWT and assert the ViewOnlyToken claim + // is present but is ciphertext, not the plaintext value. + claims := &middleware.Claims{} + _, err = jwt.ParseWithClaims(wopiToken, claims, func(t *jwt.Token) (interface{}, error) { + return []byte(cfg.Wopi.Secret), nil + }) + Expect(err).ToNot(HaveOccurred()) + Expect(claims.WopiContext.ViewOnlyToken).ToNot(BeEmpty()) + Expect(claims.WopiContext.ViewOnlyToken).ToNot(Equal(viewOnlyPlaintext)) + + // No-regression property: the middleware must decrypt the token back to the + // original plaintext so downstream consumers get a usable reva token. + req := httptest.NewRequest("GET", src.String(), nil).WithContext(ctx) + q := req.URL.Query() + q.Add("access_token", wopiToken) + q.Add("access_token_ttl", strconv.FormatInt(ttl, 10)) + req.URL.RawQuery = q.Encode() + resp := httptest.NewRecorder() + + capturingMw.ServeHTTP(resp, req) + + Expect(resp.Code).To(Equal(http.StatusOK)) + Expect(capturedErr).ToNot(HaveOccurred()) + Expect(captured.ViewOnlyToken).To(Equal(viewOnlyPlaintext)) + }) It("Should authorize successful with template reference", func() { req := httptest.NewRequest("GET", src.String(), nil).WithContext(ctx) token, err := tknMngr.MintToken(ctx, user, nil)