Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/hypercerts-api/references/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4669,7 +4669,7 @@
"description": "Opaque cursor from the previous page, bound to sortDirection.",
"schema": {
"type": "string",
"maxLength": 32768
"maxLength": 8192
}
}
],
Expand Down
6 changes: 6 additions & 0 deletions .changeset/list-features-cursor-limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hypercerts-org/happyview-modules': patch
'@hypercerts-org/api': patch
---

`listFeatures` now documents the same 8192-character cursor limit as the other list endpoints (it said 32768). Cursors the API returns are far shorter, so paging is unaffected.
5 changes: 5 additions & 0 deletions .changeset/shared-list-input-checks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypercerts-org/happyview-modules': patch
---

List endpoints now check paging cursors and repeated parameters the same way everywhere. A cursor that is empty or longer than 8192 characters is rejected with `cursor is malformed`; cursors the API returns are always shorter, so normal paging is unaffected. Repeated filters such as `authors` that arrive in an unexpected shape now fail with `<name> must use repeated query values` on every endpoint; HappyView already rejects those spellings before this check, so requests behave as before.
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
- `pnpm install:api` sends admin requests to a HappyView instance and uploads assets. Do not run it unless the user explicitly approves the target and token in the current conversation.
- HTTP runtime tests use a task-owned local Compose project and disposable database. Do not target or tear down unrelated services; note that the Compose bridge network permits egress and the configured proxy is not network-level isolation.
- Every new query endpoint needs a case in `happyview-modules/tooling/smoke-cases.js`. Add it to `happyview-modules/tooling/e2e.js` only if it has paging walks, count invariants or batch behaviour. When an endpoint's behavior changes, update the checks that cover it.
- Reuse and extend the canonical Lua helpers in `happyview-modules/lua/shared/` instead of writing a new copy in a handler or another shared file. Before writing a helper, search `happyview-modules/lua/shared/` and `happyview-modules/tooling/shared-helpers.registry.json` for an existing one; the lint only catches some copies. Families: DID, NSID, record URI/key, subject URI, datetime, handle/resolver, list cursor, list limit/includeTotal/sortDirection, query parameter helpers (`invalid`, `keys_only`, `scalar`), record view, actor view and batch request-target. Repeated-parameter (array) parsing has no shared helper yet.
- Reuse and extend the canonical Lua helpers in `happyview-modules/lua/shared/` instead of writing a new copy in a handler or another shared file. Before writing a helper, search `happyview-modules/lua/shared/` and `happyview-modules/tooling/shared-helpers.registry.json` for an existing one; the lint only catches some copies. Families: DID, NSID, record URI/key, subject URI, datetime, handle/resolver, list cursor, list limit/includeTotal/sortDirection, query parameter helpers (`invalid`, `keys_only`, `scalar`), record view, actor view and batch request-target. Repeated-parameter (array) parsing (`query_string_values` reads and bounds the list, `unique_query_values` validates and de-duplicates; the endpoint keeps its own item messages).
- `pnpm lint` (so `pnpm check` and CI) runs `happyview-modules/tooling/lint-shared-helpers.js` on `lua/shared` and `lua/src`. Every top-level function defined in `lua/shared` is protected automatically: defining the same name in another shared file or in `lua/src` fails lint and names the file that owns it. Nothing to register for a new shared helper, but check the names already there first. `shared-helpers.registry.json` (paths relative to `happyview-modules/`) holds only lookalike name patterns and inline code patterns per family (`id`, `description`, `homes` = files where the patterns legitimately appear, `lookalikes`/`inline`) and the `allowlist`. To keep an existing copy, add `{file, family, symbol, reason}` to `allowlist` (`family` is `shared-helper` for a name defined in `lua/shared`; `symbol` is the function name or inline rule id; add `count` if it occurs more than once). The failure message prints a ready-made entry. An entry that no longer matches fails lint; delete it. The current allowlist is a baseline, not approval for new copies.
2 changes: 1 addition & 1 deletion docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4909,7 +4909,7 @@
"description": "Opaque cursor from the previous page, bound to sortDirection.",
"schema": {
"type": "string",
"maxLength": 32768
"maxLength": 8192
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"cursor": {
"type": "string",
"maxLength": 32768,
"maxLength": 8192,
"description": "Opaque cursor from the previous page, bound to sortDirection."
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"cursor": {
"type": "string",
"maxLength": 32768,
"maxLength": 8192,
"description": "Opaque cursor from the previous page, bound to sortDirection."
}
}
Expand Down
2 changes: 2 additions & 0 deletions happyview-modules/lua/endpoints/getActorReactions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ local function actor_reaction_sort_key()
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(indexed_at::timestamptz, created_at::timestamptz) END"
end

-- Not query_string_values: subjects is required, keeps duplicates in request order, and
-- reports the range as "subjects must contain 1 through 100 AT-URIs".
local function feed_actor_reaction_subjects(actor, value)
if value == nil then invalid("subjects is required") end

Expand Down
2 changes: 2 additions & 0 deletions happyview-modules/lua/endpoints/getReactionCounts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ function ref_counts.run(failures, sql, values)
return rows
end

-- Not query_string_values: subjects is required, keeps duplicates in request order, stops
-- counting at 101 entries, and reports the range as "subjects must contain 1 through 100 AT-URIs".
local function reaction_count_subjects()
local value = params.subjects
if value == nil then invalid("subjects is required") end
Expand Down
101 changes: 61 additions & 40 deletions happyview-modules/lua/endpoints/listAcknowledgements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,49 @@ local function scalar_or_boolean(values, key)
end
-- luacheck: pop

-- Reads a repeated query parameter (?key=a&key=b) as a list of strings, or nil when absent.
-- A single value counts as a list of one. Rejects non-string entries, keyed or sparse
-- tables and more than 100 values. Item rules stay with the caller so each endpoint keeps
-- its own error messages; see unique_query_values.
local function query_string_values(params, key)
local value = params[key]
if value == nil then return nil end
if type(value) == "string" then return { value } end
if type(value) ~= "table" then
invalid(key .. " must be a string or repeated string parameter")
end
local count = 0
for index in pairs(value) do
if type(index) ~= "number" or index < 1 or index % 1 ~= 0 then
invalid(key .. " must use repeated query values")
end
count = count + 1
end
if count ~= #value then invalid(key .. " must use repeated query values") end
local values = {}
for index = 1, count do
if type(value[index]) ~= "string" then invalid(key .. " entries must be strings") end
values[index] = value[index]
end
if #values > 100 then invalid(key .. " accepts at most 100 values") end
return values
end

-- Runs check(item, index) on every value in order, then returns the distinct results in
-- first-seen order. check raises its own error for a bad item and may return a normalized
-- replacement; returning nothing keeps the item.
local function unique_query_values(values, check)
local unique, seen = {}, {}
for index, item in ipairs(values) do
local value = check and check(item, index) or item
if not seen[value] then
seen[value] = true
unique[#unique + 1] = value
end
end
return unique
end

local function valid_record_key(value)
return #value >= 1 and #value <= 512 and value ~= "." and value ~= ".."
and not value:find("[^%w_~%.:%-]")
Expand Down Expand Up @@ -236,18 +279,23 @@ local function parse_sort_direction(params)
return direction
end

-- Appends each item to binds and returns its "$n" placeholders, in order.
local function bind_values(binds, items)
local placeholders = {}
for _, item in ipairs(items) do
binds[#binds + 1] = item
placeholders[#placeholders + 1] = "$" .. #binds
end
return placeholders
end

-- Adds `column IN ($n, ...)` to where, binding items; an empty list matches nothing.
local function add_in(where, binds, column, items)
if #items == 0 then
where[#where + 1] = "FALSE"
return
end
local marks = {}
for _, item in ipairs(items) do
binds[#binds + 1] = item
marks[#marks + 1] = "$" .. #binds
end
where[#where + 1] = column .. " IN (" .. table.concat(marks, ", ") .. ")"
where[#where + 1] = column .. " IN (" .. table.concat(bind_values(binds, items), ", ") .. ")"
end

local function valid_cursor_timestamp(value)
Expand All @@ -258,32 +306,10 @@ local function valid_cursor_timestamp(value)
return valid_datetime(value)
end

local function acknowledgement_array(key, format)
local value = params[key]
if value == nil then return nil end
local supplied = {}
if type(value) == "string" then
supplied[1] = value
elseif type(value) == "table" then
local count = 0
for index in pairs(value) do
if type(index) ~= "number" or index < 1 or index % 1 ~= 0 then
invalid(key .. " must use repeated query values")
end
count = count + 1
end
if count ~= #value then invalid(key .. " must use repeated query values") end
for index = 1, count do
if type(value[index]) ~= "string" then invalid(key .. " entries must be strings") end
supplied[#supplied + 1] = value[index]
end
else
invalid(key .. " must be a string or repeated string parameter")
end
if #supplied > 100 then invalid(key .. " accepts at most 100 values") end

local unique, seen = {}, {}
for _, item in ipairs(supplied) do
local function acknowledgement_filter_values(key, format)
local supplied = query_string_values(params, key)
if not supplied then return nil end
return unique_query_values(supplied, function(item)
if format == "did" and not valid_did(item) then
invalid("each " .. key .. " value must be a valid DID; resolve handles to DIDs first")
elseif format == "at-uri" then
Expand All @@ -292,12 +318,7 @@ local function acknowledgement_array(key, format)
invalid("each " .. key .. " value must be a full AT-URI with a DID authority and valid collection NSID")
end
end
if not seen[item] then
seen[item] = true
unique[#unique + 1] = item
end
end
return unique
end)
end

local function acknowledgement_sort_expression()
Expand Down Expand Up @@ -347,8 +368,8 @@ local function list_acknowledgements()
limit = true,
cursor = true,
})
local authors = acknowledgement_array("authors", "did")
local subjects = acknowledgement_array("subjects", "at-uri")
local authors = acknowledgement_filter_values("authors", "did")
local subjects = acknowledgement_filter_values("subjects", "at-uri")
local direction = parse_sort_direction(params)
local limit = parse_list_limit(params)
local cursor = decode_record_cursor(scalar(params, "cursor"), direction, ACKNOWLEDGEMENT, valid_cursor_timestamp)
Expand Down
114 changes: 63 additions & 51 deletions happyview-modules/lua/endpoints/listActivities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,49 @@ local function scalar_or_boolean(values, key)
end
-- luacheck: pop

-- Reads a repeated query parameter (?key=a&key=b) as a list of strings, or nil when absent.
-- A single value counts as a list of one. Rejects non-string entries, keyed or sparse
-- tables and more than 100 values. Item rules stay with the caller so each endpoint keeps
-- its own error messages; see unique_query_values.
local function query_string_values(params, key)
local value = params[key]
if value == nil then return nil end
if type(value) == "string" then return { value } end
if type(value) ~= "table" then
invalid(key .. " must be a string or repeated string parameter")
end
local count = 0
for index in pairs(value) do
if type(index) ~= "number" or index < 1 or index % 1 ~= 0 then
invalid(key .. " must use repeated query values")
end
count = count + 1
end
if count ~= #value then invalid(key .. " must use repeated query values") end
local values = {}
for index = 1, count do
if type(value[index]) ~= "string" then invalid(key .. " entries must be strings") end
values[index] = value[index]
end
if #values > 100 then invalid(key .. " accepts at most 100 values") end
return values
end

-- Runs check(item, index) on every value in order, then returns the distinct results in
-- first-seen order. check raises its own error for a bad item and may return a normalized
-- replacement; returning nothing keeps the item.
local function unique_query_values(values, check)
local unique, seen = {}, {}
for index, item in ipairs(values) do
local value = check and check(item, index) or item
if not seen[value] then
seen[value] = true
unique[#unique + 1] = value
end
end
return unique
end

local ACTIVITY = "org.hypercerts.claim.activity"

local function query(sql, values)
Expand Down Expand Up @@ -356,18 +399,23 @@ local function parse_sort_direction(params)
return direction
end

-- Appends each item to binds and returns its "$n" placeholders, in order.
local function bind_values(binds, items)
local placeholders = {}
for _, item in ipairs(items) do
binds[#binds + 1] = item
placeholders[#placeholders + 1] = "$" .. #binds
end
return placeholders
end

-- Adds `column IN ($n, ...)` to where, binding items; an empty list matches nothing.
local function add_in(where, binds, column, items)
if #items == 0 then
where[#where + 1] = "FALSE"
return
end
local marks = {}
for _, item in ipairs(items) do
binds[#binds + 1] = item
marks[#marks + 1] = "$" .. #binds
end
where[#where + 1] = column .. " IN (" .. table.concat(marks, ", ") .. ")"
where[#where + 1] = column .. " IN (" .. table.concat(bind_values(binds, items), ", ") .. ")"
end

local function parse_include_total(values)
Expand All @@ -380,32 +428,10 @@ local function parse_include_total(values)
invalid("includeTotal must be true or false")
end

local function activity_array(key, format)
local value = params[key]
if value == nil then return nil end
local supplied = {}
if type(value) == "string" then
supplied[1] = value
elseif type(value) == "table" then
local count = 0
for index in pairs(value) do
if type(index) ~= "number" or index < 1 or index % 1 ~= 0 then
invalid(key .. " must use repeated query values")
end
count = count + 1
end
if count ~= #value then invalid(key .. " must use repeated query values") end
for index = 1, count do
if type(value[index]) ~= "string" then invalid(key .. " entries must be strings") end
supplied[#supplied + 1] = value[index]
end
else
invalid(key .. " must be a string or repeated string parameter")
end
if #supplied > 100 then invalid(key .. " accepts at most 100 values") end

local unique, seen = {}, {}
for _, item in ipairs(supplied) do
local function activity_filter_values(key, format)
local supplied = query_string_values(params, key)
if not supplied then return nil end
return unique_query_values(supplied, function(item)
if format == "did" and not valid_did(item) then
invalid("each " .. key .. " value must be a valid DID; resolve handles to DIDs first")
elseif format == "activityUri" then
Expand All @@ -414,21 +440,7 @@ local function activity_array(key, format)
invalid("each uris value must be a full org.hypercerts.claim.activity AT-URI with a DID authority")
end
end
if not seen[item] then
seen[item] = true
unique[#unique + 1] = item
end
end
return unique
end

local function bind_values(values, items)
local placeholders = {}
for _, item in ipairs(items) do
values[#values + 1] = item
placeholders[#placeholders + 1] = "$" .. #values
end
return placeholders
end)
end

local function activity_contributor_match(values, dids)
Expand Down Expand Up @@ -543,7 +555,7 @@ local function activity_list_response(search_enabled)
if search_enabled then allowed.search = true else allowed.includeTotal = true end
keys_only(params, allowed, "unknown query parameter: ")

local authors = activity_array("authors", "did")
local authors = activity_filter_values("authors", "did")
local has_organization_record = scalar_or_boolean(params, "hasOrganizationRecord")
if has_organization_record ~= nil then
if has_organization_record == "true" then
Expand All @@ -554,9 +566,9 @@ local function activity_list_response(search_enabled)
invalid("hasOrganizationRecord must be true or false")
end
end
local contributors = activity_array("contributors", "did")
local involved_actors = activity_array("involvedActors", "did")
local uris = activity_array("uris", "activityUri")
local contributors = activity_filter_values("contributors", "did")
local involved_actors = activity_filter_values("involvedActors", "did")
local uris = activity_filter_values("uris", "activityUri")
local search
if search_enabled then
search = scalar(params, "search")
Expand Down
Loading
Loading