Skip to content

Commit bd77d2e

Browse files
committed
Validate appID before encrypting
We've all done it. It's too easy to use the wrong key to encrypt with, whether you use the app name, the folder name, or get the hyphens wrong. Cropped up again recently with tidbyt#1071. If you're running pixlet encrypt from within the community repo, this will add a check that the provided key matches the ID field from some existing manifest. If not, you get an error. Thought about @jmanske's suggestions but I don't think pixlet can assume there'll be exactly one manifest in the commit to check against. You might be updating multiple apps, in which case, which should you check? Or you might be fixing a bad encryption, and not have any local edits. App names are generally unique enough that I think this works.
1 parent a703555 commit bd77d2e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

cmd/encrypt.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ package cmd
33
import (
44
"fmt"
55
"log"
6+
"os"
7+
"path/filepath"
68

79
"github.com/spf13/cobra"
810
"go.starlark.net/starlark"
911

12+
"tidbyt.dev/pixlet/manifest"
1013
"tidbyt.dev/pixlet/runtime"
14+
"tidbyt.dev/pixlet/tools/repo"
1115
)
1216

1317
const PublicKeysetJSON = `{
@@ -40,6 +44,10 @@ func encrypt(cmd *cobra.Command, args []string) {
4044
}
4145

4246
appID := args[0]
47+
if err := validateAppID(args[0]); err != nil {
48+
log.Fatalf("Cannot encrypt with appID '%s': %v", appID, err)
49+
}
50+
4351
encrypted := make([]string, len(args)-1)
4452

4553
for i, val := range args[1:] {
@@ -54,3 +62,40 @@ func encrypt(cmd *cobra.Command, args []string) {
5462
fmt.Println(starlark.String(val).String())
5563
}
5664
}
65+
66+
func validateAppID(appID string) error {
67+
cwd, err := os.Getwd()
68+
if err != nil {
69+
return fmt.Errorf("something went wrong with your local filesystem: %v", err)
70+
}
71+
if !repo.IsInRepo(cwd, "community") {
72+
log.Printf("Skipping validation of appID since command is not being run from inside the community repo.")
73+
return nil // Can only apply check if running from the community repo
74+
}
75+
root, err := repo.RepoRoot(cwd)
76+
if err != nil {
77+
return fmt.Errorf("something went wrong with your community repo: %v", err)
78+
}
79+
entries, err := os.ReadDir(filepath.Join(root, "apps"))
80+
if err != nil {
81+
return fmt.Errorf("something went wrong listing existing apps: %v", err)
82+
}
83+
for _, e := range entries {
84+
if !e.IsDir() {
85+
continue
86+
}
87+
f, err := os.Open(filepath.Join(root, "apps", e.Name(), manifest.ManifestFileName))
88+
if err != nil {
89+
log.Printf("Skipping %s/%s/%s/%s: %v", root, "apps", e.Name(), manifest.ManifestFileName, err)
90+
continue
91+
}
92+
m, err := manifest.LoadManifest(f)
93+
if err != nil {
94+
return fmt.Errorf("something went wrong loading manifest for %s: %v", e.Name(), err)
95+
}
96+
if m.ID == appID {
97+
return nil
98+
}
99+
}
100+
return fmt.Errorf("does not match manifest ID for any app in the community repo")
101+
}

0 commit comments

Comments
 (0)