nats-io/nats-server · error

invalid public key

Error message

invalid public key

What it means

DirJWTStore.load guard: pathForKey returned empty because nkeys.IsValidPublicKey failed for the lookup key, so the store refuses to build a file path for a malformed public key.

Source

Thrown at server/dirstore.go:402

	if !nkeys.IsValidPublicKey(publicKey) {
		return _EMPTY_
	}
	fileName := fmt.Sprintf("%s%s", publicKey, fileExtension)
	if store.shard {
		last := publicKey[len(publicKey)-2:]
		return filepath.Join(store.directory, last, fileName)
	} else {
		return filepath.Join(store.directory, fileName)
	}
}

// Load checks the memory store and returns the matching JWT or an error
// Assumes lock is NOT held
func (store *DirJWTStore) load(publicKey string) (string, error) {
	store.Lock()
	defer store.Unlock()
	if path := store.pathForKey(publicKey); path == _EMPTY_ {
		return _EMPTY_, fmt.Errorf("invalid public key")
	} else if data, err := os.ReadFile(path); err != nil {
		return _EMPTY_, err
	} else {
		if store.expiration != nil {
			store.expiration.updateTrack(publicKey)
		}
		return string(data), nil
	}
}

// write that keeps hash of all jwt in sync
// Assumes the lock is held. Does return true or an error never both.
func (store *DirJWTStore) write(path string, publicKey string, theJWT string) (bool, error) {
	if len(theJWT) == 0 {
		return false, fmt.Errorf("invalid JWT")
	}
	var newHash *[sha256.Size]byte
	if store.expiration != nil {

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Verify the public key string being looked up
  2. Use a valid account/operator nkey
  3. Regenerate credentials if the key is corrupt
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at server/dirstore.go:402 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/abadecad2c5b1900. Report an issue: GitHub.