From 5385064202bad4b5a8a0a63e51668f5847adc350 Mon Sep 17 00:00:00 2001 From: ckilian867 <98434879+ckilian867@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:04:08 -0700 Subject: [PATCH] [Proto] Require space between 'service' and service name in regex matching (#1845) Prior to this commit, the regex for determining whether a file had services did not require a space between the word "service" and the service name. This could lead to false alarms, like "message serviceABC". In this commit, we update the regex to only match if there is at least one space between service and the service name. --- language/proto/fileinfo.go | 2 +- language/proto/fileinfo_test.go | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/language/proto/fileinfo.go b/language/proto/fileinfo.go index cbeb381a2..d7cbe230b 100644 --- a/language/proto/fileinfo.go +++ b/language/proto/fileinfo.go @@ -106,7 +106,7 @@ func buildProtoRegexp() *regexp.Regexp { importStmt := `\bimport\s*(?:public|weak)?\s*(?P` + strLit + `)\s*;` packageStmt := `\bpackage\s*(?P` + fullIdent + `)\s*;` optionStmt := `\boption\s*(?P` + fullIdent + `)\s*=\s*(?P` + strLit + `)\s*;` - serviceStmt := `(?Pservice\s*` + ident + `\s*{)` + serviceStmt := `(?Pservice\s+` + ident + `\s*{)` comment := `//[^\n]*` protoReSrc := strings.Join([]string{importStmt, packageStmt, optionStmt, serviceStmt, comment}, "|") return regexp.MustCompile(protoReSrc) diff --git a/language/proto/fileinfo_test.go b/language/proto/fileinfo_test.go index 5438f004c..e78c40b2a 100644 --- a/language/proto/fileinfo_test.go +++ b/language/proto/fileinfo_test.go @@ -110,10 +110,35 @@ import "second.proto";`, want: FileInfo{ HasServices: true, }, - }, { + }, + { + desc: "service multiple spaces", + name: "service.proto", + proto: `service ChatService {}`, + want: FileInfo{ + HasServices: true, + }, + }, + { + desc: "service no space for bracket after service name", + name: "service.proto", + proto: `service ChatService{}`, + want: FileInfo{ + HasServices: true, + }, + }, + { + desc: "service no space before service name not matched", + name: "service.proto", + proto: `serviceChatService {}`, + want: FileInfo{ + HasServices: false, + }, + }, + { desc: "service as name", name: "service.proto", - proto: `message ServiceAccount { string service = 1; }`, + proto: `message serviceAccount { string service = 1; }`, want: FileInfo{ HasServices: false, },