fix(manifest): validate signing inputs
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
e73809cf8d
commit
dd71070a9d
3 changed files with 48 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ package scm
|
|||
import (
|
||||
filepath "dappco.re/go/core/scm/internal/ax/filepathx"
|
||||
os "dappco.re/go/core/scm/internal/ax/osx"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"dappco.re/go/core/io"
|
||||
|
|
@ -57,3 +58,18 @@ version: 2.0.0
|
|||
assert.Equal(t, "compile-custom", cm.Code)
|
||||
assert.Equal(t, "custom builder", cm.BuiltBy)
|
||||
}
|
||||
|
||||
func TestRunCompile_Bad_InvalidSignKey_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
coreDir := filepath.Join(dir, ".core")
|
||||
require.NoError(t, os.MkdirAll(coreDir, 0755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(coreDir, "manifest.yaml"), []byte(`
|
||||
code: compile-invalid-key
|
||||
name: Compile Invalid Key
|
||||
version: 1.0.0
|
||||
`), 0644))
|
||||
|
||||
err := runCompile(dir, hex.EncodeToString([]byte("short")), "core scm compile", "core.json")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid private key length")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,13 @@ func signable(m *Manifest) ([]byte, error) {
|
|||
// Sign computes the ed25519 signature and stores it in m.Sign (base64).
|
||||
// Usage: Sign(...)
|
||||
func Sign(m *Manifest, priv ed25519.PrivateKey) error {
|
||||
if m == nil {
|
||||
return coreerr.E("manifest.Sign", "nil manifest", nil)
|
||||
}
|
||||
if len(priv) != ed25519.PrivateKeySize {
|
||||
return coreerr.E("manifest.Sign", "invalid private key length", nil)
|
||||
}
|
||||
|
||||
msg, err := signable(m)
|
||||
if err != nil {
|
||||
return coreerr.E("manifest.Sign", "marshal failed", err)
|
||||
|
|
@ -32,6 +39,9 @@ func Sign(m *Manifest, priv ed25519.PrivateKey) error {
|
|||
// Verify checks the ed25519 signature in m.Sign against the public key.
|
||||
// Usage: Verify(...)
|
||||
func Verify(m *Manifest, pub ed25519.PublicKey) (bool, error) {
|
||||
if m == nil {
|
||||
return false, coreerr.E("manifest.Verify", "nil manifest", nil)
|
||||
}
|
||||
if m.Sign == "" {
|
||||
return false, coreerr.E("manifest.Verify", "no signature present", nil)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,3 +51,25 @@ func TestVerify_Bad_Unsigned_Good(t *testing.T) {
|
|||
assert.Error(t, err)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestSign_Bad_InvalidPrivateKey_Good(t *testing.T) {
|
||||
m := &Manifest{Code: "test-app", Version: "1.0.0"}
|
||||
|
||||
err := Sign(m, ed25519.PrivateKey([]byte("short")))
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid private key length")
|
||||
assert.Empty(t, m.Sign)
|
||||
}
|
||||
|
||||
func TestSign_Bad_NilManifest_Good(t *testing.T) {
|
||||
err := Sign(nil, ed25519.PrivateKey(make([]byte, ed25519.PrivateKeySize)))
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "nil manifest")
|
||||
}
|
||||
|
||||
func TestVerify_Bad_NilManifest_Good(t *testing.T) {
|
||||
ok, err := Verify(nil, ed25519.PublicKey(make([]byte, ed25519.PublicKeySize)))
|
||||
assert.Error(t, err)
|
||||
assert.False(t, ok)
|
||||
assert.Contains(t, err.Error(), "nil manifest")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue