Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/nestedpkg/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186404
  max_deposit = "5000000ugnot"

nestedpkg.gno

// Package nestedpkg provides helpers for package-path based access control.
// It is useful for upgrade patterns relying on namespaces.
//
// SECURITY: every exported helper takes `rlm realm` and reads both
// `rlm.PkgPath()` and `rlm.Previous().PkgPath()` to make an
// authorization decision. To close Class-2 designation forgery (a
// hostile realm stashes a captured realm value and passes it back to
// spoof identity), every helper gates on `rlm.IsCurrent()` first. The
// Is* predicates return false on stale rlm (fail-closed); the Assert*
// helpers panic. See docs/resources/gno-security.md.
package nestedpkg

import "strings"

// IsCallerSubPath checks if the caller realm is located in a subfolder of the current realm.
func IsCallerSubPath(_ int, rlm realm) bool {
	if !rlm.IsCurrent() {
		return false
	}
	var (
		curPath  = rlm.PkgPath() + "/"
		prevPath = rlm.Previous().PkgPath() + "/"
	)
	return strings.HasPrefix(prevPath, curPath)
}

// AssertCallerIsSubPath panics if IsCallerSubPath returns false.
func AssertCallerIsSubPath(_ int, rlm realm) {
	if !rlm.IsCurrent() {
		panic("unauthorized: rlm is not the caller's live cur")
	}
	var (
		curPath  = rlm.PkgPath() + "/"
		prevPath = rlm.Previous().PkgPath() + "/"
	)
	if !strings.HasPrefix(prevPath, curPath) {
		panic("call restricted to nested packages. current realm is " + curPath + ", previous realm is " + prevPath)
	}
}

// IsCallerParentPath checks if the caller realm is located in a parent location of the current realm.
func IsCallerParentPath(_ int, rlm realm) bool {
	if !rlm.IsCurrent() {
		return false
	}
	var (
		curPath  = rlm.PkgPath() + "/"
		prevPath = rlm.Previous().PkgPath() + "/"
	)
	return strings.HasPrefix(curPath, prevPath)
}

// AssertCallerIsParentPath panics if IsCallerParentPath returns false.
func AssertCallerIsParentPath(_ int, rlm realm) {
	if !rlm.IsCurrent() {
		panic("unauthorized: rlm is not the caller's live cur")
	}
	var (
		curPath  = rlm.PkgPath() + "/"
		prevPath = rlm.Previous().PkgPath() + "/"
	)
	if !strings.HasPrefix(curPath, prevPath) {
		panic("call restricted to parent packages. current realm is " + curPath + ", previous realm is " + prevPath)
	}
}

// IsSameNamespace checks if the caller realm and the current realm are in the same namespace.
func IsSameNamespace(_ int, rlm realm) bool {
	if !rlm.IsCurrent() {
		return false
	}
	var (
		curNs  = nsFromPath(rlm.PkgPath()) + "/"
		prevNs = nsFromPath(rlm.Previous().PkgPath()) + "/"
	)
	return curNs == prevNs
}

// AssertIsSameNamespace panics if IsSameNamespace returns false.
func AssertIsSameNamespace(_ int, rlm realm) {
	if !rlm.IsCurrent() {
		panic("unauthorized: rlm is not the caller's live cur")
	}
	var (
		curNs  = nsFromPath(rlm.PkgPath()) + "/"
		prevNs = nsFromPath(rlm.Previous().PkgPath()) + "/"
	)
	if curNs != prevNs {
		panic("call restricted to packages from the same namespace. current realm is " + curNs + ", previous realm is " + prevNs)
	}
}

// nsFromPath extracts the namespace from a package path.
func nsFromPath(pkgpath string) string {
	parts := strings.Split(pkgpath, "/")

	// Specifically for gno.land, potential paths are in the form of DOMAIN/r/NAMESPACE/...
	// XXX: Consider extra checks.
	// XXX: Support non gno.land domains, where p/ and r/ won't be enforced.
	if len(parts) >= 3 {
		return parts[2]
	}
	return ""
}

// XXX: Consider adding IsCallerDirectlySubPath
// XXX: Consider adding IsCallerDirectlyParentPath

nestedpkg_test.gno

package nestedpkg

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

func TestNsFromPath(t *testing.T) {
	cases := []struct {
		path string
		want string
	}{
		{"gno.land/r/nym-moultest123/home", "moul"},
		{"gno.land/p/nym-moultest123/svg/v0", "moul"},
		{"gno.land/r/demo/boards", "demo"},
		{"gno.land/r/moul", "moul"},
		// Fewer than three segments has no namespace slot.
		{"gno.land/r", ""},
		{"gno.land", ""},
		{"", ""},
		// The helper is domain-agnostic: it just takes the third segment.
		{"example.com/r/alice/thing", "alice"},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, nsFromPath(c.path), "nsFromPath("+c.path+")")
	}
}

// What these tests can and cannot reach, because it is not obvious:
//
//   - testing.SetRealm sets the realm the helpers read as **current**
//     (rlm.PkgPath()); this test package is what they see as **previous**
//     (rlm.Previous().PkgPath()).
//   - SetRealm's argument must be an `r/` path. PkgPath() panics with
//     "should only be called for Realms" on a `p/` path.
//
// This package is `p/moul/nestedpkg/v0`, so previous is always a `p/` path
// while current is always an `r/` path. A `p/` path can therefore never be a
// prefix of the current one or vice versa, which makes the *matching* branches
// of IsCallerSubPath/IsCallerParentPath unreachable from here — exercising
// those needs a realm-hosted caller (an `r/` test package). The non-matching
// branches and the whole namespace comparison are covered below.
const selfNS = "moul"

func TestIsCallerSubPathRejectsUnrelated(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
	uassert.False(t, IsCallerSubPath(0, cur), "an unrelated realm is not an ancestor of the caller")
}

func TestIsCallerParentPathRejectsUnrelated(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
	uassert.False(t, IsCallerParentPath(0, cur), "an unrelated realm is not a descendant of the caller")
}

func TestIsSameNamespace(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/" + selfNS + "/anything"))
	uassert.True(t, IsSameNamespace(0, cur), "same namespace across p/ and r/")

	testing.SetRealm(testing.NewCodeRealm("gno.land/r/someoneelse/thing"))
	uassert.False(t, IsSameNamespace(0, cur), "different namespace")
}

func TestAssertCallerIsSubPathPanics(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
	uassert.PanicsWithMessage(t, cur,
		"call restricted to nested packages. current realm is gno.land/r/other/thing/, previous realm is gno.land/p/nym-moultest123/nestedpkg/v0/",
		func() { AssertCallerIsSubPath(0, cur) })
}

func TestAssertCallerIsParentPathPanics(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/other/thing"))
	uassert.PanicsWithMessage(t, cur,
		"call restricted to parent packages. current realm is gno.land/r/other/thing/, previous realm is gno.land/p/nym-moultest123/nestedpkg/v0/",
		func() { AssertCallerIsParentPath(0, cur) })
}

func TestAssertIsSameNamespaceAllowsAndPanics(cur realm, t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/" + selfNS + "/anything"))
	AssertIsSameNamespace(0, cur) // same namespace: must not panic

	testing.SetRealm(testing.NewCodeRealm("gno.land/r/someoneelse/thing"))
	uassert.PanicsWithMessage(t, cur,
		"call restricted to packages from the same namespace. current realm is someoneelse/, previous realm is "+selfNS+"/",
		func() { AssertIsSameNamespace(0, cur) })
}