3535 initFriends []string
3636)
3737
38+ const (
39+ // MaxNameLength is the maximum allowed length for friend names
40+ MaxNameLength = 200
41+ // MaxEmailLength is the maximum allowed length for email addresses
42+ MaxEmailLength = 320 // RFC 5321 maximum
43+ // MaxPhoneLength is the maximum allowed length for phone numbers
44+ MaxPhoneLength = 50
45+ )
46+
3847func init () {
3948 rootCmd .AddCommand (initCmd )
4049 initCmd .Flags ().StringVar (& initFrom , "from" , "" , "Base new project on existing project (copies friends)" )
@@ -155,6 +164,9 @@ func runInit(cmd *cobra.Command, args []string) error {
155164 if nameStr == "" {
156165 return fmt .Errorf ("name is required" )
157166 }
167+ if len (nameStr ) > MaxNameLength {
168+ return fmt .Errorf ("name too long (max %d characters)" , MaxNameLength )
169+ }
158170 friends [i ].Name = nameStr
159171
160172 fmt .Print (" Email: " )
@@ -163,11 +175,18 @@ func runInit(cmd *cobra.Command, args []string) error {
163175 if emailStr == "" {
164176 return fmt .Errorf ("email is required" )
165177 }
178+ if len (emailStr ) > MaxEmailLength {
179+ return fmt .Errorf ("email too long (max %d characters)" , MaxEmailLength )
180+ }
166181 friends [i ].Email = emailStr
167182
168183 fmt .Print (" Phone (optional): " )
169184 phoneStr , _ := reader .ReadString ('\n' )
170- friends [i ].Phone = strings .TrimSpace (phoneStr )
185+ phoneStr = strings .TrimSpace (phoneStr )
186+ if len (phoneStr ) > MaxPhoneLength {
187+ return fmt .Errorf ("phone too long (max %d characters)" , MaxPhoneLength )
188+ }
189+ friends [i ].Phone = phoneStr
171190
172191 fmt .Println ()
173192 }
@@ -226,9 +245,18 @@ func parseFriendFlags(flags []string) ([]project.Friend, error) {
226245 if friends [i ].Name == "" {
227246 return nil , fmt .Errorf ("friend name cannot be empty" )
228247 }
248+ if len (friends [i ].Name ) > MaxNameLength {
249+ return nil , fmt .Errorf ("friend name too long (max %d characters)" , MaxNameLength )
250+ }
229251 if friends [i ].Email == "" {
230252 return nil , fmt .Errorf ("friend email cannot be empty" )
231253 }
254+ if len (friends [i ].Email ) > MaxEmailLength {
255+ return nil , fmt .Errorf ("friend email too long (max %d characters)" , MaxEmailLength )
256+ }
257+ if len (friends [i ].Phone ) > MaxPhoneLength {
258+ return nil , fmt .Errorf ("friend phone too long (max %d characters)" , MaxPhoneLength )
259+ }
232260 }
233261 return friends , nil
234262}
0 commit comments