Skip to content

Commit 478c98d

Browse files
authored
fix: signature dose not work as expected, if upload new pkg to old dify (#311)
- Updated the method in the interface to remove the parameter, simplifying its usage. - Introduced a new function to provide a default verification structure. - Added a file to store verification data, improving the plugin signing process. - Enhanced tests in to validate the verification process, ensuring proper handling of success and failure scenarios. - Refactored related code to accommodate the new verification structure and improve overall maintainability.
1 parent 6b7172d commit 478c98d

File tree

9 files changed

+94
-37
lines changed

9 files changed

+94
-37
lines changed

cmd/commandline/signature/signature_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,29 @@ func TestSignAndVerify(t *testing.T) {
172172
} else if !tt.expectSuccess && err == nil {
173173
t.Errorf("Expected verification to fail, but it succeeded")
174174
}
175+
176+
// check the verification
177+
pluginData, err := os.ReadFile(dummyPluginPath)
178+
assert.NoError(t, err)
179+
180+
decoder, err := decoder.NewZipPluginDecoderWithThirdPartySignatureVerificationConfig(
181+
pluginData,
182+
&decoder.ThirdPartySignatureVerificationConfig{
183+
Enabled: true,
184+
PublicKeyPaths: []string{tt.verifyKeyPath},
185+
},
186+
)
187+
assert.NoError(t, err)
188+
189+
if tt.expectSuccess {
190+
verification, err := decoder.Verification()
191+
assert.NoError(t, err)
192+
assert.Equal(t, tt.verification.AuthorizedCategory, verification.AuthorizedCategory)
193+
} else {
194+
verification, err := decoder.Verification()
195+
assert.Error(t, err)
196+
assert.Nil(t, verification)
197+
}
175198
})
176199
}
177200
}
@@ -273,7 +296,7 @@ func TestVerifyPluginWithoutVerificationField(t *testing.T) {
273296
)
274297
assert.NoError(t, err)
275298

276-
verification, err := decoder.Verification(false)
299+
verification, err := decoder.Verification()
277300
assert.NoError(t, err)
278301
assert.Nil(t, verification)
279302

internal/service/plugin_decoder.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ func UploadPluginPkg(
6161
}
6262
}
6363

64-
verification, _ := decoderInstance.Verification(false)
64+
verification, _ := decoderInstance.Verification()
6565
if verification == nil && decoderInstance.Verified() {
66-
verification = &decoder.Verification{
67-
AuthorizedCategory: decoder.AUTHORIZED_CATEGORY_LANGGENIUS,
68-
}
66+
verification = decoder.DefaultVerification()
6967
}
7068

7169
return entities.NewSuccessResponse(map[string]any{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package consts
2+
3+
const (
4+
VERIFICATION_FILE = ".verification.dify.json"
5+
)

pkg/plugin_packager/decoder/decoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type PluginDecoder interface {
4545

4646
// Verification returns the verification of the plugin, if available
4747
// Error will only returns if the plugin is not verified
48-
Verification(ignoreVerifySignature bool) (*Verification, error)
48+
Verification() (*Verification, error)
4949

5050
// CreateTime returns the creation time of the plugin as a Unix timestamp
5151
CreateTime() (int64, error)

pkg/plugin_packager/decoder/entities.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ const (
1111
type Verification struct {
1212
AuthorizedCategory AuthorizedCategory `json:"authorized_category"`
1313
}
14+
15+
func DefaultVerification() *Verification {
16+
return &Verification{
17+
AuthorizedCategory: AUTHORIZED_CATEGORY_LANGGENIUS,
18+
}
19+
}

pkg/plugin_packager/decoder/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (d *FSPluginDecoder) CreateTime() (int64, error) {
169169
return 0, nil
170170
}
171171

172-
func (d *FSPluginDecoder) Verification(ignoreVerifySignature bool) (*Verification, error) {
172+
func (d *FSPluginDecoder) Verification() (*Verification, error) {
173173
return nil, nil
174174
}
175175

pkg/plugin_packager/decoder/verifier.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/langgenius/dify-plugin-daemon/internal/core/license/public_key"
1414
"github.com/langgenius/dify-plugin-daemon/internal/utils/encryption"
15-
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
1615
)
1716

1817
// VerifyPlugin is a function that verifies the signature of a plugin
@@ -96,22 +95,9 @@ func VerifyPluginWithPublicKeys(decoder PluginDecoder, publicKeys []*rsa.PublicK
9695
return err
9796
}
9897

99-
// get the verification, at this point, verification is used to verify checksum
100-
// just ignore verifying signature
101-
verification, err := decoder.Verification(true)
102-
if err != nil {
103-
return err
104-
}
105-
10698
// write the time into data
10799
data.Write([]byte(strconv.FormatInt(createdAt, 10)))
108100

109-
if verification != nil {
110-
// marshal
111-
verificationBytes := parser.MarshalJsonBytes(verification)
112-
data.Write(verificationBytes)
113-
}
114-
115101
sigBytes, err := base64.StdEncoding.DecodeString(signature)
116102
if err != nil {
117103
return err

pkg/plugin_packager/decoder/zip.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
1717
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
18+
"github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/consts"
1819
)
1920

2021
type ZipPluginDecoder struct {
@@ -184,9 +185,8 @@ func (z *ZipPluginDecoder) decode() error {
184185
}
185186

186187
signatureData, err := parser.UnmarshalJson[struct {
187-
Signature string `json:"signature"`
188-
Time int64 `json:"time"`
189-
Verification *Verification `json:"verification"`
188+
Signature string `json:"signature"`
189+
Time int64 `json:"time"`
190190
}](z.reader.Comment)
191191

192192
if err != nil {
@@ -195,11 +195,31 @@ func (z *ZipPluginDecoder) decode() error {
195195

196196
pluginSig := signatureData.Signature
197197
pluginTime := signatureData.Time
198-
pluginVerification := signatureData.Verification
198+
199+
var verification *Verification
200+
201+
// try to read the verification file
202+
verificationData, err := z.ReadFile(consts.VERIFICATION_FILE)
203+
if err != nil {
204+
if !errors.Is(err, os.ErrNotExist) {
205+
return err
206+
}
207+
208+
// if the verification file is not found, set the verification to nil
209+
verification = nil
210+
} else {
211+
// unmarshal the verification data
212+
verificationData, err := parser.UnmarshalJsonBytes[Verification](verificationData)
213+
if err != nil {
214+
return err
215+
}
216+
217+
verification = &verificationData
218+
}
199219

200220
z.sig = pluginSig
201221
z.createTime = pluginTime
202-
z.verification = pluginVerification
222+
z.verification = verification
203223

204224
return nil
205225
}
@@ -238,8 +258,8 @@ func (z *ZipPluginDecoder) CreateTime() (int64, error) {
238258
return z.createTime, nil
239259
}
240260

241-
func (z *ZipPluginDecoder) Verification(ignoreVerifySignature bool) (*Verification, error) {
242-
if !ignoreVerifySignature && !z.Verified() {
261+
func (z *ZipPluginDecoder) Verification() (*Verification, error) {
262+
if !z.Verified() {
243263
return nil, errors.New("plugin is not verified")
244264
}
245265

pkg/plugin_packager/signer/withkey/sign_with_key.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/langgenius/dify-plugin-daemon/internal/utils/encryption"
1616
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
17+
"github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/consts"
1718
"github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/decoder"
1819
)
1920

@@ -73,6 +74,31 @@ func SignPluginWithPrivateKey(
7374
return nil, err
7475
}
7576

77+
// write the verification into data
78+
// NOTE: .verification.dify.json is a special file that contains the verification information
79+
// and it will be placed at the end of the zip file, checksum is calculated using it also
80+
verificationBytes := parser.MarshalJsonBytes(verification)
81+
82+
// write verification into the zip file
83+
fileWriter, err := zipWriter.Create(consts.VERIFICATION_FILE)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
if _, err := fileWriter.Write(verificationBytes); err != nil {
89+
return nil, err
90+
}
91+
92+
// hash the verification
93+
hash := sha256.New()
94+
hash.Write(verificationBytes)
95+
hashed := hash.Sum(nil)
96+
97+
// write the hash into data
98+
if _, err := data.Write(hashed); err != nil {
99+
return nil, err
100+
}
101+
76102
// get current time
77103
ct := time.Now().Unix()
78104

@@ -82,12 +108,6 @@ func SignPluginWithPrivateKey(
82108
// write the time into data
83109
data.Write([]byte(timeString))
84110

85-
// json marshal the verification
86-
verificationBytes := parser.MarshalJsonBytes(verification)
87-
88-
// write the verification into data
89-
data.Write(verificationBytes)
90-
91111
// sign the data
92112
signature, err := encryption.RSASign(privateKey, data.Bytes())
93113
if err != nil {
@@ -96,9 +116,8 @@ func SignPluginWithPrivateKey(
96116

97117
// write the signature into the comment field of the zip file
98118
comments := parser.MarshalJson(map[string]any{
99-
"signature": base64.StdEncoding.EncodeToString(signature),
100-
"time": ct,
101-
"verification": verification,
119+
"signature": base64.StdEncoding.EncodeToString(signature),
120+
"time": ct,
102121
})
103122

104123
// write signature

0 commit comments

Comments
 (0)