Detect Mixed Scripts With unicode.In RangeTables

Inspect identifier runes against unicode.Latin, unicode.Cyrillic, and custom Range16 tables via unicode.In to block mixed-script spoofing attempts.

GoSecurity Boundaries

Code

snippet
package main

import (
	"fmt"
	"unicode"
)

// Custom Range16 table (entries must be sorted ascending by Lo)
var slugExtra = &unicode.RangeTable{
	R16: []unicode.Range16{
		{Lo: '-', Hi: '-', Stride: 1},
		{Lo: '0', Hi: '9', Stride: 1},
		{Lo: '_', Hi: '_', Stride: 1},
	},
	LatinOffset: 3,
}

func auditSlug(s string) string {
	var hasLatin, hasCyrillic, hasDisallowed bool
	for _, r := range s {
		if unicode.In(r, unicode.Latin) {
			hasLatin = true
		} else if unicode.In(r, unicode.Cyrillic) {
			hasCyrillic = true
		} else if !unicode.In(r, slugExtra) {
			hasDisallowed = true
		}
	}
	if hasLatin && hasCyrillic {
		return "blocked:mixed-latin-cyrillic"
	}
	if hasCyrillic || hasDisallowed {
		return "blocked:outside-allowlist"
	}
	return "allowed"
}

func main() {
	samples := []string{
		"api-gateway_v2",
		"café-menu",
		"p\u0430ypal-billing", // U+0430 is Cyrillic 'а'
		"\u0442\u0435\u0441\u0442",
		"docs/internal",
	}
	for _, s := range samples {
		fmt.Printf("%-20q -> %s\n", s, auditSlug(s))
	}
}

Execution summary — Detect Mixed Scripts With unicode.In RangeTables

Detect Mixed Scripts With unicode.In RangeTables is built around the functions auditSlug and main.

Iteration happens inside for _, r := range s — everything outside it runs once.

Points of care

  • No external package tool enters; every single operation targets core platform built-in APIs.

Putting it to use

Signature: func auditSlug(s string) string

main sequences auditSlug; executing the file runs that flow once.

Core lines

for _, s := range samples {
	fmt.Printf("%-20q -> %s\n", s, auditSlug(s))
}
for _, r := range s {
	if unicode.In(r, unicode.Latin) {
		hasLatin = true
	} else if unicode.In(r, unicode.Cyrillic) {
		hasCyrillic = true
	} else if !unicode.In(r, slugExtra) {
		hasDisallowed = true
	}
}
return "blocked:mixed-latin-cyrillic"

Interactive Tools for This Topic

FAQ

How do you run Detect Mixed Scripts With unicode.In RangeTables?
Run it as a complete program — compile or interpret the file directly; there is nothing to install first.