Repository: googleapis/api-linter
Pull request #355 (2019-11-18 23:10:07)

[AIP-192] Rule to forbid raw HTML.


Patch:
diff --git a/docs/rules/0192/has-comments.md b/docs/rules/0192/has-comments.md
index a874553..40e988e 100644
--- a/docs/rules/0192/has-comments.md
+++ b/docs/rules/0192/has-comments.md
@@ -8,7 +8,7 @@ redirect_from:
   - /0192/has-comments
 ---
 
-# Proto3 syntax
+# Omnipresent comments
 
 This rule enforces that every descriptor in every proto file has a _public_
 leading comment, as mandated in [AIP-192][].
diff --git a/docs/rules/0192/no-html.md b/docs/rules/0192/no-html.md
new file mode 100644
index 0000000..619e29e
--- /dev/null
+++ b/docs/rules/0192/no-html.md
@@ -0,0 +1,73 @@
+---
+rule:
+  aip: 192
+  name: [core, '0192', no-html]
+  summary: Comments must not use raw HTML
+permalink: /192/no-html
+redirect_from:
+  - /0192/no-html
+---
+
+# No HTML in comments
+
+This rule enforces that every descriptor in every proto file does not use raw
+HTML in comments, as mandated in [AIP-192][].
+
+## Details
+
+This rule looks at each descriptor in each proto file (exempting oneofs and the
+file itself) and tries to find HTML tags, and complains if it finds any.
+
+**Note:** This lint rule uses a regular expression to look for HTML, which is a
+[famous anti-pattern][]. We do it anyway to avoid taking a large dependency for
+one rule. Therefore, this rule allows many false negatives to avoid any false
+positives; that is, it will intentionally let more complex HTML through in
+order to prevent cases where it complains and is wrong.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// A representation of a book.
+message Book {
+  // (-- This comment should use Markdown, not HTML.) --)
+  // The name of the book.
+  // Format: <code>publishers/{publisher}/books/{book}</code>
+  string name = 1;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+// A representation of a book.
+message Book {
+  // The name of the book.
+  // Format: `publishers/{publisher}/books/{book}`
+  string name = 1;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the descriptor
+(and revel in the irony). Remember to also include an [aip.dev/not-precedent][]
+comment explaining why.
+
+```proto
+// A representation of a book.
+message Book {
+  // (-- api-linter: core::0192::no-html=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  // The name of the book.
+  // Format: <code>publishers/{publisher}/books/{book}</code>
+  string name = 1;
+}
+```
+
+[aip-192]: https://aip.dev/192
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
+[famous anti-pattern]: https://stackoverflow.com/questions/1732348/
diff --git a/lint/rule.go b/lint/rule.go
index 70038c5..042d8f9 100644
--- a/lint/rule.go
+++ b/lint/rule.go
@@ -248,6 +248,10 @@ type DescriptorRule struct {
 	// only a subset of methods are available to it.
 	LintDescriptor func(desc.Descriptor) []Problem
 
+	// OnlyIf accepts a Descriptor and determines whether this rule
+	// is applicable.
+	OnlyIf func(desc.Descriptor) bool
+
 	noPositional struct{}
 }
 
@@ -265,25 +269,37 @@ func (r *DescriptorRule) Lint(fd *desc.FileDescriptor) []Problem {
 
 	// Iterate over all services and methods.
 	for _, service := range fd.GetServices() {
-		problems = append(problems, r.LintDescriptor(service)...)
+		if r.OnlyIf == nil || r.OnlyIf(service) {
+			problems = append(problems, r.LintDescriptor(service)...)
+		}
 		for _, method := range service.GetMethods() {
-			problems = append(problems, r.LintDescriptor(method)...)
+			if r.OnlyIf == nil || r.OnlyIf(method) {
+				problems = append(problems, r.LintDescriptor(method)...)
+			}
 		}
 	}
 
 	// Iterate over all messages, and all fields within each message.
 	for _, message := range getAllMessages(fd) {
-		problems = append(problems, r.LintDescriptor(message)...)
+		if r.OnlyIf == nil || r.OnlyIf(message) {
+			problems = append(problems, r.LintDescriptor(message)...)
+		}
 		for _, field := range message.GetFields() {
-			problems = append(problems, r.LintDescriptor(field)...)
+			if r.OnlyIf == nil || r.OnlyIf(field) {
+				problems = append(problems, r.LintDescriptor(field)...)
+			}
 		}
 	}
 
 	// Iterate over all enums and enum values.
 	for _, enum := range getAllEnums(fd) {
-		problems = append(problems, r.LintDescriptor(enum)...)
+		if r.OnlyIf == nil || r.OnlyIf(enum) {
+			problems = append(problems, r.LintDescriptor(enum)...)
+		}
 		for _, value := range enum.GetValues() {
-			problems = append(problems, r.LintDescriptor(value)...)
+			if r.OnlyIf == nil || r.OnlyIf(value) {
+				problems = append(problems, r.LintDescriptor(value)...)
+			}
 		}
 	}
 
diff --git a/rules/aip0192/aip0192.go b/rules/aip0192/aip0192.go
index 691b886..b3cad12 100644
--- a/rules/aip0192/aip0192.go
+++ b/rules/aip0192/aip0192.go
@@ -26,6 +26,7 @@ func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		192,
 		hasComments,
+		noHTML,
 		onlyLeadingComments,
 	)
 }
diff --git a/rules/aip0192/no_html.go b/rules/aip0192/no_html.go
new file mode 100644
index 0000000..76533ec
--- /dev/null
+++ b/rules/aip0192/no_html.go
@@ -0,0 +1,51 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0192
+
+import (
+	"regexp"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var noHTML = &lint.DescriptorRule{
+	Name: lint.NewRuleName(192, "no-html"),
+	OnlyIf: func(d desc.Descriptor) bool {
+		return d.GetSourceInfo() != nil
+	},
+	LintDescriptor: func(d desc.Descriptor) []lint.Problem {
+		for _, comment := range separateInternalComments(d.GetSourceInfo().GetLeadingComments()).External {
+			if htmlTag.MatchString(comment) == true {
+				return []lint.Problem{{
+					Message:    "Comments must not include raw HTML.",
+					Descriptor: d,
+				}}
+			}
+		}
+		return nil
+	},
+}
+
+// Yes, yes, I know: https://stackoverflow.com/questions/1732348/
+// Even Jon Skeet cannot parse HTML using regular expressions.
+//
+// That said, we really only want to pick up "basic HTML smell", and are
+// disinterested in actually doing any manipulation, and we can be at least
+// a little tolerant of false positives/negatives.
+//
+// Therefore, in this case, a regex seems better than taking a dependency
+// just for this.
+var htmlTag = regexp.MustCompile(`</?[a-zA-Z]+( /)?>`)


Test patch:
diff --git a/lint/rule_test.go b/lint/rule_test.go
index e60c959..f82a5f2 100644
--- a/lint/rule_test.go
+++ b/lint/rule_test.go
@@ -272,7 +272,9 @@ func TestEnumRuleNested(t *testing.T) {
 func TestDescriptorRule(t *testing.T) {
 	// Create a file with one of everything in it.
 	book := builder.NewMessage("Book").AddNestedEnum(
-		builder.NewEnum("Format").AddValue(builder.NewEnumValue("FORMAT_UNSPECIFIED")),
+		builder.NewEnum("Format").AddValue(
+			builder.NewEnumValue("FORMAT_UNSPECIFIED"),
+		).AddValue(builder.NewEnumValue("PAPERBACK")),
 	).AddField(builder.NewField("name", builder.FieldTypeString())).AddNestedMessage(
 		builder.NewMessage("Author"),
 	)
@@ -293,6 +295,9 @@ func TestDescriptorRule(t *testing.T) {
 	visited := make(map[string]desc.Descriptor)
 	rule := &DescriptorRule{
 		Name: RuleName("test"),
+		OnlyIf: func(d desc.Descriptor) bool {
+			return d.GetName() != "FORMAT_UNSPECIFIED"
+		},
 		LintDescriptor: func(d desc.Descriptor) []Problem {
 			visited[d.GetName()] = d
 			return nil
@@ -305,7 +310,7 @@ func TestDescriptorRule(t *testing.T) {
 	// Verify that each descriptor was visited.
 	// We do not care what order they were visited in.
 	wantDescriptors := []string{
-		"Author", "Book", "ConjureBook", "Format", "FORMAT_UNSPECIFIED",
+		"Author", "Book", "ConjureBook", "Format", "PAPERBACK",
 		"name", "Library", "State",
 	}
 	if got, want := rule.GetName(), "test"; string(got) != want {
diff --git a/rules/aip0192/no_html_test.go b/rules/aip0192/no_html_test.go
new file mode 100644
index 0000000..71461d8
--- /dev/null
+++ b/rules/aip0192/no_html_test.go
@@ -0,0 +1,50 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0192
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestNoHTML(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		comment  string
+		problems testutils.Problems
+	}{
+		{"Valid", "It is **great!**", testutils.Problems{}},
+		{"ValidMath", "x < 10", testutils.Problems{}},
+		{"ValidMoreMath", "x < 10 > y", testutils.Problems{}},
+		{"InvalidBold", "It is <b>great!</b>", testutils.Problems{{Message: "raw HTML"}}},
+		{"InvalidCode", "This is <code>code font</code>.", testutils.Problems{{Message: "raw HTML"}}},
+		{"InvalidBreak", "This spans<br />two lines.", testutils.Problems{{Message: "raw HTML"}}},
+		{"InternionalFalseNegativeComplexTag", `<img src="https://foo.bar/mickey" />`, testutils.Problems{}},
+		{"IntentionalFalseNegativeInnerSpace", "Something < b > bold < /b >", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3String(t, strings.ReplaceAll(`
+				// A foo. {{.Comment}}
+				message Foo {}
+			`, "{{.Comment}}", test.comment))
+			message := f.GetMessageTypes()[0]
+			if diff := test.problems.SetDescriptor(message).Diff(noHTML.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}