Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/reject-year-zero-cursor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypercerts-org/happyview-modules': patch
---

A list request whose `cursor` or `before` timestamp is in year 0000 now returns an `InvalidRequest` error. Several endpoints used to answer it with an HTTP 500. `listVocabTags` now reports this as "cursor is malformed".
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/getActorLikes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/getActorReposts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/getLikes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/getReposts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
109 changes: 53 additions & 56 deletions happyview-modules/lua/endpoints/listAcknowledgements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down Expand Up @@ -147,20 +148,59 @@ local function valid_nsid(value)
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local generic_valid_datetime = valid_datetime
local function parse_sort_direction(params)
local direction = scalar(params, "sortDirection") or "desc"
if direction ~= "asc" and direction ~= "desc" then invalid("sortDirection must be 'asc' or 'desc'") end
return direction
end

local function acknowledgement_valid_datetime(value)
if type(value) ~= "string" then return false end
if value:match("^(%d%d%d%d)") == "0000" then return false end
return generic_valid_datetime(value)
local function cursor_encode(value)
local encoded = json.encode(value)
return (encoded:gsub(".", function(char) return string.format("%02x", string.byte(char)) end))
end

-- Decodes a hex-encoded {v, d, t, u} list cursor. Rejects malformed tokens, other
-- sort directions and unknown keys; validate_value(value) -> boolean runs the
-- endpoint-specific check on t and u. Not for endpoints that skip the 8192-character
-- cap or accept extra keys.
local function decode_cursor(token, direction, validate_value)
if token == nil then return nil end
if #token == 0 or #token > 8192 or #token % 2 ~= 0 or token:find("[^0-9a-f]") then
invalid("cursor is malformed")
end
local decoded = token:gsub("..", function(pair) return string.char(tonumber(pair, 16)) end)
local ok, value = pcall(json.decode, decoded)
if not ok or type(value) ~= "table" or value.v ~= 1 or value.d ~= direction
or type(value.t) ~= "string" or type(value.u) ~= "string" then
invalid("cursor is malformed or belongs to another sortDirection")
end
for key in pairs(value) do
if key ~= "v" and key ~= "d" and key ~= "t" and key ~= "u" then invalid("cursor is malformed") end
end
if not validate_value(value) then invalid("cursor is malformed") end
return value
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, ", ") .. ")"
end

local function valid_cursor_timestamp(value)
if type(value) ~= "string" or #value ~= 27
or not value:match("^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d%.%d%d%d%d%d%dZ$") then
return false
end
return acknowledgement_valid_datetime(value)
return valid_datetime(value)
end

local function acknowledgement_array(key, format)
Expand Down Expand Up @@ -205,39 +245,11 @@ local function acknowledgement_array(key, format)
return unique
end

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

local function cursor_encode(value)
local encoded = json.encode(value)
return (encoded:gsub(".", function(char) return string.format("%02x", string.byte(char)) end))
end

local function decode_acknowledgement_cursor(token, direction)
if token == nil then return nil end
if #token == 0 or #token > 8192 or #token % 2 ~= 0 or token:find("[^0-9a-f]") then
invalid("cursor is malformed")
end
local decoded = token:gsub("..", function(pair) return string.char(tonumber(pair, 16)) end)
local ok, value = pcall(json.decode, decoded)
if not ok or type(value) ~= "table" or value.v ~= 1 or value.d ~= direction
or type(value.t) ~= "string" or type(value.u) ~= "string" then
invalid("cursor is malformed or belongs to another sortDirection")
end
for key in pairs(value) do
if key ~= "v" and key ~= "d" and key ~= "t" and key ~= "u" then invalid("cursor is malformed") end
end
local valid, collection = valid_record_uri(value.u)
if not valid_cursor_timestamp(value.t) or not valid or collection ~= ACKNOWLEDGEMENT then
invalid("cursor is malformed")
end
return value
return decode_cursor(token, direction, function(value)
local valid, collection = valid_record_uri(value.u)
return valid_cursor_timestamp(value.t) and valid and collection == ACKNOWLEDGEMENT
end)
end

local function acknowledgement_sort_expression()
Expand All @@ -252,22 +264,8 @@ end

local function acknowledgement_list_query(authors, subjects, limit, cursor, direction)
local where, values = { "acknowledgement.collection = $1" }, { ACKNOWLEDGEMENT }
if authors then
if #authors == 0 then
where[#where + 1] = "FALSE"
else
where[#where + 1] = "acknowledgement.did IN (" ..
table.concat(bind_acknowledgement_values(values, authors), ", ") .. ")"
end
end
if subjects then
if #subjects == 0 then
where[#where + 1] = "FALSE"
else
where[#where + 1] = "acknowledgement.record::jsonb->'subject'->>'uri' IN (" ..
table.concat(bind_acknowledgement_values(values, subjects), ", ") .. ")"
end
end
if authors then add_in(where, values, "acknowledgement.did", authors) end
if subjects then add_in(where, values, "acknowledgement.record::jsonb->'subject'->>'uri'", subjects) end

local page_values, page_where = {}, {}
for _, value in ipairs(values) do page_values[#page_values + 1] = value end
Expand Down Expand Up @@ -303,8 +301,7 @@ local function list_acknowledgements()
})
local authors = acknowledgement_array("authors", "did")
local subjects = acknowledgement_array("subjects", "at-uri")
local direction = scalar(params, "sortDirection") or "desc"
if direction ~= "asc" and direction ~= "desc" then invalid("sortDirection must be 'asc' or 'desc'") end
local direction = parse_sort_direction(params)
local limit = parse_list_limit(params)
local cursor = decode_acknowledgement_cursor(scalar(params, "cursor"), direction)
local rows = acknowledgement_list_query(authors, subjects, limit, cursor, direction)
Expand Down
41 changes: 12 additions & 29 deletions happyview-modules/lua/endpoints/listActivities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand All @@ -267,37 +268,19 @@ local function parse_list_limit(params)
return limit
end

local CREATED_AT_SORT_COLUMNS = {
profile = { record = "record", indexed_at = "indexed_at", created_at = "created_at" },
organization = {
record = "organization.record",
indexed_at = "organization.indexed_at",
created_at = "organization.created_at",
},
activity = {
record = "activity.record",
indexed_at = "activity.indexed_at",
created_at = "activity.created_at",
},
collection = {
record = "collection.record",
indexed_at = "collection.indexed_at",
created_at = "collection.created_at",
},
}
local CREATED_AT_SORT_PATTERN = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]+)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])$"

local function created_at_sort_expression(source)
local columns = CREATED_AT_SORT_COLUMNS[source]
if not columns then
error("CreatedAtSortExpressionError: source must be 'profile', 'organization', 'activity', or 'collection'", 0)
end

local created = columns.record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. columns.record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
-- Sort key for a happyview_records row: the record's own createdAt when it is a
-- valid timestamp, otherwise COALESCE(indexed_at, created_at). `alias` is the
-- table alias; nil selects unqualified columns.
local function created_at_sort(alias)
local prefix = alias and (alias .. ".") or ""
local record, indexed_at, created_at = prefix .. "record", prefix .. "indexed_at", prefix .. "created_at"
local created = record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
" ~ '" .. CREATED_AT_SORT_PATTERN .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created ..
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. columns.indexed_at ..
"::timestamptz, " .. columns.created_at .. "::timestamptz) END"
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. indexed_at ..
"::timestamptz, " .. created_at .. "::timestamptz) END"
end

local function activity_scalar(values, key)
Expand Down Expand Up @@ -455,7 +438,7 @@ local function activity_query(authors, has_organization_record, contributors, in
local sql = "SELECT activity.uri, activity.did, activity.cid, activity.indexed_at::text AS indexed_at, " ..
"activity.record::text AS record, to_char(sorted.sort_at AT TIME ZONE 'UTC', " ..
"'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') AS sort_timestamp " ..
"FROM happyview_records AS activity CROSS JOIN LATERAL (SELECT " .. created_at_sort_expression("activity") .. " AS sort_at) AS sorted " ..
"FROM happyview_records AS activity CROSS JOIN LATERAL (SELECT " .. created_at_sort("activity") .. " AS sort_at) AS sorted " ..
"WHERE " .. table.concat(where, " AND ") .. " ORDER BY sorted.sort_at " .. ordering ..
", activity.uri " .. ordering .. " LIMIT $" .. #values
local rows = query(sql, values)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/listActorFollowers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/listActorFollowing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/listAttachments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/listBadgeAwards.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/listBadgeDefinitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
1 change: 1 addition & 0 deletions happyview-modules/lua/endpoints/listBadgeResponses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand Down
41 changes: 12 additions & 29 deletions happyview-modules/lua/endpoints/listCollections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ local function valid_datetime(value)
"^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)(.*)$")
if not year then return false end
year, month, day = tonumber(year), tonumber(month), tonumber(day)
if year < 1 then return false end
hour, minute, second = tonumber(hour), tonumber(minute), tonumber(second)
if month < 1 or month > 12 or hour > 23 or minute > 59 or second > 59 then return false end
local leap = year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
Expand All @@ -275,37 +276,19 @@ local function valid_datetime(value)
return true
end

local CREATED_AT_SORT_COLUMNS = {
profile = { record = "record", indexed_at = "indexed_at", created_at = "created_at" },
organization = {
record = "organization.record",
indexed_at = "organization.indexed_at",
created_at = "organization.created_at",
},
activity = {
record = "activity.record",
indexed_at = "activity.indexed_at",
created_at = "activity.created_at",
},
collection = {
record = "collection.record",
indexed_at = "collection.indexed_at",
created_at = "collection.created_at",
},
}
local CREATED_AT_SORT_PATTERN = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]+)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])$"

local function created_at_sort_expression(source)
local columns = CREATED_AT_SORT_COLUMNS[source]
if not columns then
error("CreatedAtSortExpressionError: source must be 'profile', 'organization', 'activity', or 'collection'", 0)
end

local created = columns.record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. columns.record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
-- Sort key for a happyview_records row: the record's own createdAt when it is a
-- valid timestamp, otherwise COALESCE(indexed_at, created_at). `alias` is the
-- table alias; nil selects unqualified columns.
local function created_at_sort(alias)
local prefix = alias and (alias .. ".") or ""
local record, indexed_at, created_at = prefix .. "record", prefix .. "indexed_at", prefix .. "created_at"
local created = record .. "::jsonb->>'createdAt'"
return "CASE WHEN jsonb_typeof(" .. record .. "::jsonb->'createdAt') = 'string' AND " .. created ..
" ~ '" .. CREATED_AT_SORT_PATTERN .. "' AND " .. created .. " !~ '-00:00$' AND pg_input_is_valid(" .. created ..
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. columns.indexed_at ..
"::timestamptz, " .. columns.created_at .. "::timestamptz) END"
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(" .. indexed_at ..
"::timestamptz, " .. created_at .. "::timestamptz) END"
end

local function collection_array(key, kind)
Expand Down Expand Up @@ -487,7 +470,7 @@ end
local function collection_list_sql(where, values, limit, direction)
values[#values + 1] = limit + 1
local ordering = direction == "asc" and "ASC" or "DESC"
local sort_key = created_at_sort_expression("collection")
local sort_key = created_at_sort("collection")
return "SELECT collection.uri, collection.did, collection.cid, collection.indexed_at::text AS indexed_at, " ..
"collection.record::text AS record, to_char(sorted.sort_at AT TIME ZONE 'UTC', " ..
"'YYYY-MM-DD\"T\"HH24:MI:SS.US\"Z\"') AS sort_timestamp " ..
Expand Down
Loading
Loading