ProgramGuru

Secure Go Programming

Start with Secure Go Programming

Learn secure go programming through an executable example, then connect the small demonstration to production code.

What you’ll learn

  • Explain the main purpose of secure go programming.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Secure programs keep untrusted input separate from code and privileged operations. Common controls include parameterized SQL, contextual HTML escaping, careful file path handling, bounded input and least-privilege credentials.

html/template escapes user-provided content when rendered in ordinary HTML text contexts.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for securityUse encoders appropriate to each output context.Untrusted textStep 1Template contextStep 2Escape dangerous markupStep 3Render safelyStep 4Use encoders appropriate to each output context.
Use encoders appropriate to each output context.

Tools, syntax and practical notes

Never concatenate untrusted input into SQL statements. Use parameters supported by the database driver; validate authorization independently of HTML escaping.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"bytes"
	"fmt"
	"html/template"
)

func main() {
	page := template.Must(template.New("page").Parse("<p>{{.}}</p>"))
	var result bytes.Buffer
	if err := page.Execute(&result, "<script>alert(1)</script>"); err != nil {
		panic(err)
	}
	fmt.Println(result.String())
}

Output

<p>&lt;script&gt;alert(1)&lt;/script&gt;</p>

Trace what the program does

The template writes the supplied text as escaped HTML rather than making the script tag executable markup. The output includes HTML character entities.

Avoid common mistakes

  • Avoid converting user strings to template.HTML; doing so bypasses escaping.
  • Template escaping is not a substitute for CSRF protection, authentication or input limits.

Try it yourself

Create a template that renders a user name and compare the output for plain text and characters such as < and &.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which Go package is designed for contextual HTML template escaping?
Choose one answer. You can try again.

Answer and explanation

html/template

Correct. The template writes the supplied text as escaped HTML rather than making the script tag executable markup.

What you should remember

You have practiced Secure Go Programming in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Context and Request Lifecycle Management →