lightning-terminal/db/sqlc/queries/sessions.sql
Viktor Torstensson b61f62d7d9
multi: preserve macaroon recipe order in SQL
Store explicit positions for session macaroon caveats and permissions
in the SQL schema and read them back in position order. Also remove the
migration-time sorting workaround in session comparison, so migration
validation now checks the actual persisted order instead of masking it.

This is needed because session caveat order is not just presentation
data. LiT adds caveats to the baked macaroon in slice order, and the
macaroon library updates the signature hash chain for each added caveat.
Reordering caveats can therefore change the resulting macaroon bytes and
signature.

The previous schema split caveats and permissions into child tables
without any position column, and the SQL reads had no ORDER BY. The KV
store preserves slice order, but SQL had no explicit way to reproduce
that order after migration or on later reads. The migration code’s old
sorting step was only making validation deterministic; it did not
preserve the original recipe order.

Permissions are canonicalized by lnd when baking, so their order is less
semantically important for the final macaroon. They still get positions
here so the stored recipe remains faithful to the original session data
and both child tables behave consistently.

Why it was needed:

- caveats needed explicit order preservation because they are appended
  and signed in order.
- The old SQL schema did not store order, and the read queries did not
  request one.
- Adding position makes the SQL representation faithful to the KV/TLV
  recipe instead of relying on incidental row order.

- Adding it to permissions too keeps the stored recipe lossless and
  consistent, even though lnd. canonicalizes permissions before baking.

NOTE: This commit explicitly edits the previous migration instead of
adding a new one. This is ok as SQL dbs are not yet supported in
production, so there are no live deployments to worry about.
2026-06-08 17:21:03 +02:00

126 lines
2.7 KiB
SQL

-- name: InsertSession :one
INSERT INTO sessions (
alias, label, state, type, expiry, created_at,
server_address, dev_server, macaroon_root_key, pairing_secret,
local_private_key, local_public_key, remote_public_key, privacy, group_id, account_id
) VALUES (
$1, $2, $3, $4, $5, $6, $7,
$8, $9, $10, $11, $12,
$13, $14, $15, $16
) RETURNING id;
-- name: SetSessionGroupID :exec
UPDATE sessions
SET group_id = $1
WHERE id = $2;
-- name: DeleteSessionsWithState :exec
DELETE FROM sessions
WHERE state = $1;
-- name: DeleteSession :exec
DELETE FROM sessions
WHERE id = $1;
-- name: GetSessionByLocalPublicKey :one
SELECT * FROM sessions
WHERE local_public_key = $1;
-- name: GetSessionsInGroup :many
SELECT * FROM sessions
WHERE group_id = $1;
-- name: GetSessionAliasesInGroup :many
SELECT alias FROM sessions
WHERE group_id = $1;
-- name: GetSessionByID :one
SELECT * FROM sessions
WHERE id = $1;
-- name: GetSessionIDByAlias :one
SELECT id FROM sessions
WHERE alias = $1;
-- name: GetAliasBySessionID :one
SELECT alias FROM sessions
WHERE id = $1;
-- name: GetSessionByAlias :one
SELECT * FROM sessions
WHERE alias = $1;
-- name: ListSessions :many
SELECT * FROM sessions
ORDER BY created_at;
-- name: ListSessionsByType :many
SELECT * FROM sessions
WHERE type = $1
ORDER BY created_at;
-- name: ListSessionsByState :many
SELECT * FROM sessions
WHERE state = $1
ORDER BY created_at;
-- name: SetSessionRevokedAt :exec
UPDATE sessions
SET revoked_at = $1
WHERE id = $2;
-- name: UpdateSessionState :exec
UPDATE sessions
SET state = $1
WHERE id = $2;
-- name: SetSessionRemotePublicKey :exec
UPDATE sessions
SET remote_public_key = $1
WHERE id = $2;
-- name: InsertSessionMacaroonPermission :exec
INSERT INTO session_macaroon_permissions (
session_id, entity, action, position
) VALUES (
$1, $2, $3, $4
);
-- name: GetSessionMacaroonPermissions :many
SELECT * FROM session_macaroon_permissions
WHERE session_id = $1
ORDER BY position ASC;
-- name: InsertSessionMacaroonCaveat :exec
INSERT INTO session_macaroon_caveats (
session_id, caveat_id, verification_id, location, position
) VALUES (
$1, $2, $3, $4, $5
);
-- name: GetSessionMacaroonCaveats :many
SELECT * FROM session_macaroon_caveats
WHERE session_id = $1
ORDER BY position ASC;
-- name: InsertSessionFeatureConfig :exec
INSERT INTO session_feature_configs (
session_id, feature_name, config
) VALUES (
$1, $2, $3
);
-- name: GetSessionFeatureConfigs :many
SELECT * FROM session_feature_configs
WHERE session_id = $1;
-- name: InsertSessionPrivacyFlag :exec
INSERT INTO session_privacy_flags (
session_id, flag
) VALUES (
$1, $2
);
-- name: GetSessionPrivacyFlags :many
SELECT * FROM session_privacy_flags
WHERE session_id = $1;