Skip to content

Commit 534e30f

Browse files
authored
fix: prevent using invalid name for type/packet/message (ignite#849)
* Forbidden type name * Rename type to component * Create component file * Lint
1 parent 05d9906 commit 534e30f

File tree

4 files changed

+102
-74
lines changed

4 files changed

+102
-74
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package scaffolder
2+
3+
import (
4+
"go/token"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
// isMsgServerDefined checks if the module uses the MsgServer convention for transactions
10+
// this is checked by verifying the existence of the tx.proto file
11+
func isMsgServerDefined(appPath, moduleName string) (bool, error) {
12+
txProto, err := filepath.Abs(filepath.Join(appPath, "proto", moduleName, "tx.proto"))
13+
if err != nil {
14+
return false, err
15+
}
16+
17+
if _, err := os.Stat(txProto); os.IsNotExist(err) {
18+
return false, nil
19+
}
20+
return true, err
21+
}
22+
23+
// isForbiddenComponentName returns true if the name is forbidden as a component name
24+
func isForbiddenComponentName(name string) bool {
25+
switch name {
26+
case
27+
"logger",
28+
"keeper",
29+
"query",
30+
"genesis",
31+
"types",
32+
"tx":
33+
return true
34+
}
35+
36+
return isGoReservedWord(name)
37+
}
38+
39+
func isGoReservedWord(name string) bool {
40+
// Check keyword or literal
41+
if token.Lookup(name).IsKeyword() {
42+
return true
43+
}
44+
45+
// Check with builtin identifier
46+
switch name {
47+
case
48+
"panic",
49+
"recover",
50+
"append",
51+
"bool",
52+
"byte",
53+
"cap",
54+
"close",
55+
"complex",
56+
"complex64",
57+
"complex128",
58+
"uint16",
59+
"copy",
60+
"false",
61+
"float32",
62+
"float64",
63+
"imag",
64+
"int",
65+
"int8",
66+
"int16",
67+
"uint32",
68+
"int32",
69+
"int64",
70+
"iota",
71+
"len",
72+
"make",
73+
"new",
74+
"nil",
75+
"uint64",
76+
"print",
77+
"println",
78+
"real",
79+
"string",
80+
"true",
81+
"uint",
82+
"uint8",
83+
"uintptr":
84+
return true
85+
}
86+
return false
87+
}

starport/services/scaffolder/message.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func (s *Scaffolder) AddMessage(moduleName string, msgName string, msgDesc strin
3737
return fmt.Errorf("the module %s doesn't exist", moduleName)
3838
}
3939

40-
// Ensure the msg name is not a Go reserved name, it would generate an incorrect code
41-
if isGoReservedWord(msgName) {
42-
return fmt.Errorf("%s can't be used as a type name", msgName)
40+
// Ensure the name is valid, otherwise it would generate an incorrect code
41+
if isForbiddenComponentName(msgName) {
42+
return fmt.Errorf("%s can't be used as a packet name", msgName)
4343
}
4444

4545
// Check msg is not already created

starport/services/scaffolder/packet.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func (s *Scaffolder) AddPacket(moduleName string, packetName string, packetField
5454
return fmt.Errorf("the module %s doesn't implement IBC module interface", moduleName)
5555
}
5656

57+
// Ensure the name is valid, otherwise it would generate an incorrect code
58+
if isForbiddenComponentName(packetName) {
59+
return fmt.Errorf("%s can't be used as a packet name", packetName)
60+
}
61+
5762
// Check packet doesn't exist
5863
ok, err = isPacketCreated(s.path, moduleName, packetName)
5964
if err != nil {

starport/services/scaffolder/type.go

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type AddTypeOption struct {
3131
}
3232

3333
// AddType adds a new type stype to scaffolded app by using optional type fields.
34-
func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, stype string, fields ...string) error {
34+
func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, typeName string, fields ...string) error {
3535
version, err := s.version()
3636
if err != nil {
3737
return err
@@ -54,18 +54,18 @@ func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, st
5454
return fmt.Errorf("the module %s doesn't exist", moduleName)
5555
}
5656

57-
// Ensure the type name is not a Go reserved name, it would generate an incorrect code
58-
if isGoReservedWord(stype) {
59-
return fmt.Errorf("%s can't be used as a type name", stype)
57+
// Ensure the type name is valid, otherwise it would generate an incorrect code
58+
if isForbiddenComponentName(typeName) {
59+
return fmt.Errorf("%s can't be used as a type name", typeName)
6060
}
6161

6262
// Check type is not already created
63-
ok, err = isTypeCreated(s.path, moduleName, stype)
63+
ok, err = isTypeCreated(s.path, moduleName, typeName)
6464
if err != nil {
6565
return err
6666
}
6767
if ok {
68-
return fmt.Errorf("%s type is already added", stype)
68+
return fmt.Errorf("%s type is already added", typeName)
6969
}
7070

7171
// Parse provided field
@@ -81,7 +81,7 @@ func (s *Scaffolder) AddType(addTypeOptions AddTypeOption, moduleName string, st
8181
ModulePath: path.RawPath,
8282
ModuleName: moduleName,
8383
OwnerName: owner(path.RawPath),
84-
TypeName: stype,
84+
TypeName: typeName,
8585
Fields: tFields,
8686
Legacy: addTypeOptions.Legacy,
8787
}
@@ -208,20 +208,6 @@ func isTypeCreated(appPath, moduleName, typeName string) (isCreated bool, err er
208208
return
209209
}
210210

211-
// isMsgServerDefined checks if the module uses the MsgServer convention for transactions
212-
// this is checked by verifying the existence of the tx.proto file
213-
func isMsgServerDefined(appPath, moduleName string) (bool, error) {
214-
txProto, err := filepath.Abs(filepath.Join(appPath, "proto", moduleName, "tx.proto"))
215-
if err != nil {
216-
return false, err
217-
}
218-
219-
if _, err := os.Stat(txProto); os.IsNotExist(err) {
220-
return false, nil
221-
}
222-
return true, err
223-
}
224-
225211
// isForbiddenTypeField returns true if the name is forbidden as a field name
226212
func isForbiddenTypeField(name string) bool {
227213
switch name {
@@ -234,53 +220,3 @@ func isForbiddenTypeField(name string) bool {
234220

235221
return isGoReservedWord(name)
236222
}
237-
238-
func isGoReservedWord(name string) bool {
239-
// Check keyword or literal
240-
if token.Lookup(name).IsKeyword() {
241-
return true
242-
}
243-
244-
// Check with builtin identifier
245-
switch name {
246-
case
247-
"panic",
248-
"recover",
249-
"append",
250-
"bool",
251-
"byte",
252-
"cap",
253-
"close",
254-
"complex",
255-
"complex64",
256-
"complex128",
257-
"uint16",
258-
"copy",
259-
"false",
260-
"float32",
261-
"float64",
262-
"imag",
263-
"int",
264-
"int8",
265-
"int16",
266-
"uint32",
267-
"int32",
268-
"int64",
269-
"iota",
270-
"len",
271-
"make",
272-
"new",
273-
"nil",
274-
"uint64",
275-
"print",
276-
"println",
277-
"real",
278-
"string",
279-
"true",
280-
"uint",
281-
"uint8",
282-
"uintptr":
283-
return true
284-
}
285-
return false
286-
}

0 commit comments

Comments
 (0)