knobe

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

knobe-go

A KNOBE verifier for Go: an independent verifier for KNOBE Protocol v1 .knobe.md files, written as a sibling of the reference verifier lens.py in KnobeOne/knobe-protocol.

A match proves integrity, not truth. A valid proves conformance, not truth. Inspect before trusting.

What it does and does not do

knobe-go reads a .knobe.md file and reports three independent results from spec §7. status (integrity) is verified, verified-body-modified, failed or unreadable. body_verified (the optional body hash) is yes, modified or omitted. conformance (schema compliance) is valid, warnings or invalid. To get these it applies the §5 canonical hash rule and the §6 body normalization, and it rejects as unreadable any payload with duplicate keys, keys that collide under NFC, a non-object payload, or an unsupported spec_version. Its reports and exit codes match lens.py field for field, and every payload-derived string is escaped before it is printed. It does not seal, create or transform KNOBE files. It does not check that content is accurate, that attribution is honest, or that a file is fit for any use: a verified result only proves the payload is byte-identical to what was sealed. It runs entirely locally and never makes a network request.

Install and use

Requires Go 1.22 or later.

go install github.com/KnobeDev/knobe-go/cmd/knobe-verify@latest
knobe-verify path/to/file.knobe.md
knobe-verify --json path/to/file.knobe.md

The human report and the --json document have the same layout and keys as python3 lens.py and python3 lens.py --json.

Exit code Meaning
0 verified or verified-body-modified, and conformance valid or warnings
1 Integrity holds but conformance is invalid, or status is failed
2 unreadable, or a usage or file error

As a library:

import knobe "github.com/KnobeDev/knobe-go"

r := knobe.VerifyFile("file.knobe.md") // or knobe.VerifyBytes(data)
fmt.Println(r.Status, r.BodyVerified, r.Conformance, r.ConformanceIssues)

Test vector results

These are the results against the published vectors at knobe-protocol commit c7ada1f. Every one matches the expected result in that directory's README. The first nine are the conformance bar.

file status body_verified conformance exit
minimal-valid.knobe.md verified omitted valid 0
full-valid.knobe.md verified yes valid 0
body-modified.knobe.md verified-body-modified modified valid 0
payload-modified.knobe.md failed omitted valid 1
unreadable.knobe.md unreadable omitted (null) invalid 2
unicode-valid.knobe.md verified yes valid 0
numeric-violation.knobe.md verified yes invalid 1
omitted-body-hash.knobe.md verified omitted valid 0
multi-block.knobe.md verified omitted warnings 0
adversarial/bad-attribution-sources-type.knobe.md verified omitted invalid 1
adversarial/bad-body-hash-format.knobe.md verified-body-modified modified invalid 1
adversarial/bad-created-date.knobe.md verified omitted invalid 1
adversarial/control-character-title.knobe.md verified omitted valid 0
adversarial/duplicate-key.knobe.md unreadable omitted (null) invalid 2
adversarial/multi-block-warning.knobe.md verified omitted warnings 0
adversarial/nfc-key-collision.knobe.md unreadable omitted (null) invalid 2
adversarial/no-frontmatter-valid-payload.knobe.md verified omitted invalid 1
adversarial/payload-array.knobe.md unreadable omitted (null) invalid 2
adversarial/unsupported-spec-version.knobe.md unreadable omitted (null) invalid 2
interpretation/astral-unicode-literal.knobe.md verified omitted valid 0
interpretation/attribution-missing.knobe.md verified omitted invalid 1
interpretation/base64-unpadded.knobe.md unreadable omitted (null) invalid 2
interpretation/begin-marker-mid-line.knobe.md verified yes valid 0
interpretation/boolean-null-values.knobe.md verified omitted valid 0
interpretation/spec-version-frontmatter-mismatch.knobe.md verified omitted warnings 0

For an unreadable file, --json prints body_verified as null, as lens.py does. ERRATA E11 accepts either null or omitted.

The control-character title prints escaped, never raw:

title:             ALERT\x1b[31mINJECTED\x1b[0m\x0astatus:            verified (SPOOFED)

How it is tested

go test needs a checkout of knobe-protocol at ../knobe-protocol. Set KNOBE_PROTOCOL_DIR to use a different location. The differential tests also need python3, and they skip themselves when it is missing.

Test What it checks
vectors_test.go All 25 published vectors (core, adversarial, interpretation) against the expected results in their READMEs, plus exit codes and the ERRATA E5/E9 issue text
parity_test.go Runs lens.py --json side by side with this verifier over every published .knobe.md (75 files) and 123 crafted hostile files. It compares state, conformance, body_verified, computed and stored hash, block count and exit code, the fields upstream's knobe-core.selftest.mjs compares. The hostile files are sealed by an independent Python oracle (testdata/pyhash.py), never by the code under test.
unit_test.go The JSON parser, Python-compatible float repr and string helpers, Base64 rules, canonical form, body normalization, block and frontmatter parsing, and output escaping

On top of the tests, the human-readable output and the complete --json output are byte-identical to lens.py on all 75 published files.

CI runs against upstream main, so an upstream change that breaks parity shows up immediately.

Behavior beyond lens.py

knobe-go reports unreadable (exit 2) in a few cases where lens.py itself crashes with a Python traceback. It never crashes on any input.

Input lens.py knobe-go
A float that overflows to infinity, such as 1e400 Traceback from json.dumps unreadable
Nesting deeper than 996 levels Traceback (RecursionError) unreadable
A non-string, non-hashable spec_version, such as a list Traceback (TypeError) unreadable (unsupported spec_version)

knobe-verify reads at most 64 MiB (knobe.MaxFileSize) and reports a larger file as unreadable. Parsing takes about 30 bytes of memory per input byte, and lens.py has no limit. Library callers of VerifyBytes should bound their input the same way.

The human report escapes more than lens.py does. It also escapes control characters in stored_hash and in conformance issue text, which lens.py prints raw, so a crafted key or hash cannot carry terminal escape sequences through them. It also escapes Unicode format characters, such as bidi overrides and isolates (U+202E, U+2066), zero-width characters and U+FEFF, as \uNNNN, so a crafted title cannot reorder or hide text in the report. The --json output is unchanged and matches lens.py.

Provenance

knobe-go is maintained by Joshua Hori (KnobeDev). It is not an official or reference KNOBE implementation, and the KNOBE steward did not build, review or endorse it.

It was written with AI assistance (Claude) at the maintainer's direction. As the steward's governance notes ask, the reliance on the reference engines is disclosed here: lens.py was consulted throughout, along with the maintainer's Rust port, knobe-rs, which follows the same reference. Every ambiguous case was settled by observing lens.py and is pinned by the parity tests above.

License

Apache-2.0, matching the upstream verifier code. This is an independent implementation: it shares no code with upstream but follows the behavior of lens.py. See NOTICE.

Documentation

Index

Constants

View Source
const (
	BeginMarker = "-----BEGIN KNOBE B64-----"
	EndMarker   = "-----END KNOBE B64-----"
)
View Source
const MaxFileSize = 64 << 20

MaxFileSize bounds what VerifyFile reads. Parsing costs roughly 30 bytes of memory per input byte, so an unbounded hostile file could exhaust a constrained host. Larger files are reported unreadable.

Variables

This section is empty.

Functions

func BodyHash

func BodyHash(text string) string

BodyHash is the §6 body hash.

func NormalizeBody

func NormalizeBody(text string) string

NormalizeBody applies the §6 body normalization.

func PayloadHash

func PayloadHash(payload Value) (string, error)

PayloadHash is the §5 payload hash of a payload object; its own payload_hash member is ignored.

func SafeString

func SafeString(s string, limit int) string

SafeString is lens.py safe() for a string: escape C0/C1 controls and DEL as \xNN so a crafted string cannot spoof the report or inject terminal escape sequences, then truncate to limit code points (limit <= 0: no limit). Stricter than lens.py, it also escapes bidi overrides, isolates and zero-width characters (\uNNNN), which could reorder or hide report text.

Types

type BodyVerified

type BodyVerified string

BodyVerified is the optional body-hash result (spec §6, §7).

const (
	BodyYes      BodyVerified = "yes"
	BodyModified BodyVerified = "modified"
	BodyOmitted  BodyVerified = "omitted"
)

type Conformance

type Conformance string

Conformance is the schema/spec compliance result (spec §7).

const (
	Valid    Conformance = "valid"
	Warnings Conformance = "warnings"
	Invalid  Conformance = "invalid"
)

type Kind

type Kind int

Kind is the JSON type of a Value.

const (
	Null Kind = iota
	Bool
	Int   // integer lexeme kept exactly as written (arbitrary precision, like Python int)
	Float // float64, parsed with correct rounding like Python float()
	String
	Array
	Object
)

type Member

type Member struct {
	Key string
	Val Value
}

Member is one key/value pair of an Object.

type ParseError

type ParseError struct {
	DuplicateKey string
	IsDuplicate  bool
	NonJSONConst bool
	Msg          string
	Offset       int
}

ParseError kinds, so the verifier can word its unreadable reason as lens.py does.

func (*ParseError) Error

func (e *ParseError) Error() string

type ParseResult

type ParseResult struct {
	Value         Value
	LoneSurrogate bool
}

ParseResult carries the parsed value plus a flag for unpaired UTF-16 surrogate escapes. Python accepts those while parsing but cannot UTF-8-encode them, so the verifier rejects the payload after its structural checks, as lens.py does.

func ParseJSON

func ParseJSON(text string) (ParseResult, error)

ParseJSON parses one strict RFC 8259 document. Duplicate keys at any depth and NaN/Infinity are errors. Only JSON whitespace may surround the value.

type Report

type Report struct {
	Status Status
	// Computed is the recomputed payload hash; empty when unreadable.
	Computed string
	// Stored is the payload_hash member as found (any JSON type; the empty
	// string if absent). Nil when unreadable.
	Stored *Value
	// Payload is the decoded payload, when one was decoded.
	Payload *Value
	// BodyVerified is empty when unreadable (machine output then says null).
	BodyVerified      BodyVerified
	Conformance       Conformance
	ConformanceIssues []string
	// BlockCount is the number of payload blocks found; the last is evaluated.
	BlockCount int
	// Reason says why the file is unreadable; empty otherwise.
	Reason string
}

Report is a verification result with lens.py's fields and semantics.

func VerifyBytes

func VerifyBytes(data []byte) Report

VerifyBytes verifies the contents of a .knobe.md file. Callers passing untrusted input should bound its size, as VerifyFile does.

func VerifyFile

func VerifyFile(path string) Report

VerifyFile reads and verifies a .knobe.md file of at most MaxFileSize bytes. It never panics on file contents; read errors are reported as unreadable.

func (Report) BlockUsed

func (r Report) BlockUsed() int

BlockUsed is the 1-based index of the evaluated block: always the last (spec §3.3).

func (Report) ExitCode

func (r Report) ExitCode() int

ExitCode is lens.py's: 0 when integrity holds and conformance is valid or warnings; 1 when the payload failed or conformance is invalid; 2 when the file is unreadable.

func (Report) JSON

func (r Report) JSON() string

JSON renders the report exactly as `python3 lens.py --json` does: the same keys in the same order, two-space indent, literal UTF-8.

func (Report) MultipleBlocks

func (r Report) MultipleBlocks() bool

MultipleBlocks reports whether more than one payload block was present.

func (Report) WriteHuman

func (r Report) WriteHuman(w io.Writer)

WriteHuman writes the human-readable report in lens.py's layout. Every payload-derived string is escaped first, so control characters and terminal escape sequences in a hostile file are shown, never executed.

type Status

type Status string

Status is the integrity result (spec §7).

const (
	Verified             Status = "verified"
	VerifiedBodyModified Status = "verified-body-modified"
	Failed               Status = "failed"
	Unreadable           Status = "unreadable"
)

type Value

type Value struct {
	Kind Kind
	B    bool
	Num  string // Int lexeme
	F    float64
	S    string
	Arr  []Value
	Obj  []Member
}

Value is a parsed JSON value. Objects keep insertion order; the parser guarantees their keys are unique.

func (Value) Get

func (v Value) Get(key string) (Value, bool)

Get returns the value of key in an Object.

func (Value) Has

func (v Value) Has(key string) bool

Has reports whether an Object contains key.

func (Value) Str

func (v Value) Str() (string, bool)

Str returns the string value and whether v is a String.

Directories

Path Synopsis
cmd
knobe-verify command
Command knobe-verify checks a KNOBE Protocol v1 file and reports its integrity (status), body hash result (body_verified) and spec conformance, with the same report and exit codes as the reference verifier lens.py.
Command knobe-verify checks a KNOBE Protocol v1 file and reports its integrity (status), body hash result (body_verified) and spec conformance, with the same report and exit codes as the reference verifier lens.py.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL