@@ -106,6 +106,52 @@ func Test_NewGenerateJWTCommand(t *testing.T) {
106106 })
107107}
108108
109+ func Test_GenerateJWTCommand_FilePermissions (t * testing.T ) {
110+ t .Parallel ()
111+
112+ t .Run ("new file should have 0600 permissions" , func (t * testing.T ) {
113+ tempDir := t .TempDir ()
114+ outputPath := filepath .Join (tempDir , "jwt.hex" )
115+
116+ cmd := jwt .NewGenerateJWTCommand ()
117+ cmd .SetArgs ([]string {"--output-path" , outputPath })
118+ require .NoError (t , cmd .Execute ())
119+
120+ info , err := os .Stat (outputPath )
121+ require .NoError (t , err )
122+ require .Equal (t , os .FileMode (0600 ), info .Mode ().Perm (),
123+ "new JWT file should have 0600 permissions" )
124+ })
125+
126+ t .Run ("pre-existing file with permissive permissions should be fixed" , func (t * testing.T ) {
127+ tempDir := t .TempDir ()
128+ outputPath := filepath .Join (tempDir , "jwt.hex" )
129+
130+ // Create file with world-readable permissions
131+ err := os .WriteFile (outputPath , []byte ("old_content" ), 0755 )
132+ require .NoError (t , err )
133+
134+ // Verify it has permissive permissions
135+ info , err := os .Stat (outputPath )
136+ require .NoError (t , err )
137+ require .Equal (t , os .FileMode (0755 ), info .Mode ().Perm ())
138+
139+ // Run the generate command
140+ cmd := jwt .NewGenerateJWTCommand ()
141+ cmd .SetArgs ([]string {"--output-path" , outputPath })
142+ require .NoError (t , cmd .Execute ())
143+
144+ // Verify permissions are now restricted
145+ info , err = os .Stat (outputPath )
146+ require .NoError (t , err )
147+ require .Equal (t , os .FileMode (0600 ), info .Mode ().Perm (),
148+ "pre-existing JWT file should have permissions fixed to 0600" )
149+
150+ // Also verify the content is valid
151+ checkAuthFileIntegrity (t , outputPath )
152+ })
153+ }
154+
109155func checkAuthFileIntegrity (tb testing.TB , fPath string ) {
110156 tb .Helper ()
111157 fs := afero .NewOsFs ()
0 commit comments