REGEXVAULTv2.0
Security/Injection Patterns
Verified Safe

Path Traversal Pattern Regex for Go

/(?:\.\./|\.\.\\|%2e%2e%2f|%2e%2e/|\.%2e/|%2e\./|%252e%252e|\.\.%c0%af|\.\.%c1%9c|\.\./|%00|\.php\x00|\.asp\x00)/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching path traversal pattern, ported and verified for Go. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.

Go Implementation

Go
// Path Traversal Pattern
// ReDoS-safe | RegexVault — Security > Injection Patterns

package validation

import "regexp"

var pathTraversalPatternRe = regexp.MustCompile(`(?:\.\./|\.\.\\|%2e%2e%2f|%2e%2e/|\.%2e/|%2e\./|%252e%252e|\.\.%c0%af|\.\.%c1%9c|\.\./|%00|\.php\x00|\.asp\x00)`)

func ValidatePathTraversalPattern(s string) bool {
    return pathTraversalPatternRe.MatchString(s)
}

// Example
// fmt.Println(ValidatePathTraversalPattern("../../etc/passwd")) // true

Test Cases

Matches (Valid)
Rejects (Invalid)
../../etc/passwd/var/www/html/index.html
%2e%2e%2fuploads/profile.jpg
../../../C:\Users\Public\Documents
..%c0%af
%00injection

When to use this pattern

This pattern is drawn from the Security > Injection Patterns category and carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Path traversal defense must happen AFTER URL decoding. A filter that blocks ../ before decoding will miss %2e%2e%2f. Use the real path function (os.path.realpath in Python, path.resolve in Node.js) and verify it starts with the intended base directory.

Technical Notes

Path traversal (directory traversal) allows attackers to read files outside the intended directory. Double encoding (%252e = %) is used to bypass naive decoders. Null byte (%00) terminates strings in some C-based systems. Real defense: use path canonicalization and compare against the allowed base directory.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern