REGEXVAULTv2.0
Dev & Systems/File Paths
Verified Safe

POSIX File Permission Octal Regex for Go

/^([0-7]?)([0-7]{3})$/

What this pattern does

This page provides a lightweight, single-purpose regular expression for matching posix file permission octal, ported and verified for Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. 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
// POSIX File Permission Octal
// ReDoS-safe | RegexVault — Dev & Systems > File Paths

package validation

import "regexp"

var posixFilePermissionOctalRe = regexp.MustCompile(`^([0-7]?)([0-7]{3})$`)

func ValidatePosixFilePermissionOctal(s string) bool {
    return posixFilePermissionOctalRe.MatchString(s)
}

// Example
// fmt.Println(ValidatePosixFilePermissionOctal("755")) // true

Test Cases

Matches (Valid)
Rejects (Invalid)
755888
64499
77776
0008888
0755a644
47550855
2755
1777

When to use this pattern

This pattern is drawn from the Dev & Systems > File Paths 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

Setting 777 on sensitive files is a security risk. The sticky bit on directories (1777) prevents users from deleting each other's files even with directory write permission.

Technical Notes

Optional leading digit: 4=setuid, 2=setgid, 1=sticky. Three digits: owner/group/other (read=4, write=2, execute=1). 1777 = /tmp permissions (sticky). 4755 = setuid root (dangerous). Group 1 = special bits, group 2 = rwx permissions.

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