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
35 changes: 18 additions & 17 deletions happyview-modules/lua/endpoints/getActorReactions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ local function record_view(row)
}
end

-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local ACTOR_REACTION_NULL = json.decode("null")
local ACTOR_REACTION_LIMIT = 100
local ACTOR_REACTION_COLLECTIONS = {
Expand All @@ -67,25 +84,9 @@ local function actor_reaction_sort_key()
", 'timestamptz') THEN (" .. created .. ")::timestamptz ELSE COALESCE(indexed_at::timestamptz, created_at::timestamptz) END"
end

local function valid_actor_reaction_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function valid_actor_reaction_subject_uri(value)
local valid, collection = valid_record_uri(value)
return valid and valid_actor_reaction_nsid(collection)
return valid and valid_nsid(collection)
end

local function feed_actor_reaction_subjects(value)
Expand Down
34 changes: 17 additions & 17 deletions happyview-modules/lua/endpoints/getEntityFollows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,27 @@ local function follow_batch_targets(actor, key, value, validate, description, en
return targets, unique
end

-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function get_entity_follows()
keys_only(params, { actor = true, entities = true })
local actor = scalar(params, "actor")
if not actor or not valid_did(actor) then invalid("actor must be a valid DID") end
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end

for _, segment in ipairs(segments) do
if #segment > 63 or segment:find("[^A-Za-z0-9%-]")
or segment:sub(1, 1) == "-" or segment:sub(-1) == "-" then
return false
end
end

if not segments[1]:sub(1, 1):match("^[A-Za-z]$") then return false end
return segments[#segments]:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function valid_entity_uri(value)
if #value > 8192 then return false end
local valid, collection = valid_record_uri(value)
Expand Down
35 changes: 18 additions & 17 deletions happyview-modules/lua/endpoints/getReactionCounts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ local function valid_record_uri(value)
return true, collection, authority
end

-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local LIKE = "app.certified.feed.like"
local REPOST = "app.certified.feed.repost"

Expand All @@ -55,22 +72,6 @@ local function reaction_count_valid_did_percent_escapes(authority)
return true
end

local function reaction_count_valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function reaction_count_subjects()
local value = params.subjects
if value == nil then invalid("subjects is required") end
Expand Down Expand Up @@ -104,7 +105,7 @@ local function reaction_count_subjects()
for _, subject in ipairs(supplied) do
local valid, collection, authority = valid_record_uri(subject)
if not valid or not reaction_count_valid_did_percent_escapes(authority)
or not reaction_count_valid_nsid(collection) then
or not valid_nsid(collection) then
invalid("each subjects value must be a full AT-URI with a valid DID authority and collection NSID")
end
if not seen[subject] then
Expand Down
33 changes: 17 additions & 16 deletions happyview-modules/lua/endpoints/listAcknowledgements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,7 @@ local function hydrate_acknowledgement_authors(views)
end
end

local generic_valid_datetime = valid_datetime

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)
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)
end

-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
Expand All @@ -162,6 +147,22 @@ 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 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)
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)
end

local function acknowledgement_array(key, format)
local value = params[key]
if value == nil then return nil end
Expand Down
35 changes: 18 additions & 17 deletions happyview-modules/lua/endpoints/listBadgeAwards.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,8 @@ local function hydrate_actor_views(actors, run_query)
end
end

local AWARD = "app.certified.badge.award"
local DEFINITION = "app.certified.badge.definition"
local RESPONSE = "app.certified.badge.response"
local RESPONSE_STATUSES = { accepted = true, rejected = true, unanswered = true }

local function valid_badge_award_uri(value)
local valid, collection = valid_record_uri(value)
return valid and collection == AWARD
end

local function valid_badge_definition_uri(value)
local valid, collection = valid_record_uri(value)
return valid and collection == DEFINITION
end

local function valid_badge_subject_nsid(value)
-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
Expand All @@ -154,6 +140,21 @@ local function valid_badge_subject_nsid(value)
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local AWARD = "app.certified.badge.award"
local DEFINITION = "app.certified.badge.definition"
local RESPONSE = "app.certified.badge.response"
local RESPONSE_STATUSES = { accepted = true, rejected = true, unanswered = true }

local function valid_badge_award_uri(value)
local valid, collection = valid_record_uri(value)
return valid and collection == AWARD
end

local function valid_badge_definition_uri(value)
local valid, collection = valid_record_uri(value)
return valid and collection == DEFINITION
end

local function query(sql, values)
if db.backend() ~= "postgres" then
error("BadgeAwardQueryFailed: badge award API requires PostgreSQL", 0)
Expand Down Expand Up @@ -205,7 +206,7 @@ local function subject_values()
local is_did = valid_did(item)
local valid_uri, collection
if not is_did then valid_uri, collection = valid_record_uri(item) end
if not is_did and (not valid_uri or not valid_badge_subject_nsid(collection)) then
if not is_did and (not valid_uri or not valid_nsid(collection)) then
invalid("subjects[" .. index .. "] must be a valid DID or full record AT-URI with a valid collection NSID")
end
if not seen[item] then
Expand Down
8 changes: 2 additions & 6 deletions happyview-modules/lua/endpoints/listEvaluations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,19 @@ local function hydrate_evaluation_views(views)
end
end

-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end

local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end

for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then
return false
end
or not last:match("^[A-Za-z0-9]$") then return false end
end

local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end
Expand Down
7 changes: 2 additions & 5 deletions happyview-modules/lua/endpoints/listMeasurements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,19 @@ local function hydrate_measurement_views(views)
end
end

-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end

for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then
return false
end
or not last:match("^[A-Za-z0-9]$") then return false end
end

local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end
Expand Down
30 changes: 13 additions & 17 deletions happyview-modules/lua/endpoints/listReceipts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,26 @@ local function hydrate_funding_receipt_views(views)
end
end

local function valid_funding_nsid(value)
if type(value) ~= "string" or #value < 5 or #value > 317 or value:find("[^A-Za-z0-9.%-]") then
return false
end
-- An atproto NSID: at least three dot-separated segments, at most 317 characters.
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^.]+") do segments[#segments + 1] = segment end
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end

for index, segment in ipairs(segments) do
if #segment > 63 or segment:find("^[%-]") or segment:find("[%-]$") then return false end
if index == 1 then
if not segment:match("^[A-Za-z]") then return false end
elseif index == #segments then
if not segment:match("^[A-Za-z][A-Za-z0-9]*$") then return false end
elseif not segment:match("^[A-Za-z0-9]") then
return false
end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
return true
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function valid_funding_record_uri(value)
local valid, collection = valid_record_uri(value)
if not valid or not valid_funding_nsid(collection) then return false, collection end
if not valid or not valid_nsid(collection) then return false, collection end
return true, collection
end

Expand Down
16 changes: 0 additions & 16 deletions happyview-modules/lua/shared/acknowledgementList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ local function valid_cursor_timestamp(value)
return acknowledgement_valid_datetime(value)
end

local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end
local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end
for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then return false end
end
local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function acknowledgement_array(key, format)
local value = params[key]
if value == nil then return nil end
Expand Down
21 changes: 0 additions & 21 deletions happyview-modules/lua/shared/evaluationList.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
local function valid_nsid(value)
if type(value) ~= "string" or #value > 317 then return false end

local segments = {}
for segment in value:gmatch("[^%.]+") do segments[#segments + 1] = segment end
if #segments < 3 or table.concat(segments, ".") ~= value then return false end

for index = 1, #segments - 1 do
local segment = segments[index]
if #segment > 63 or segment:find("[^A-Za-z0-9%-]") then return false end
local first, last = segment:sub(1, 1), segment:sub(-1)
if not first:match(index == 1 and "^[A-Za-z]$" or "^[A-Za-z0-9]$")
or not last:match("^[A-Za-z0-9]$") then
return false
end
end

local name = segments[#segments]
return #name <= 63 and name:match("^[A-Za-z][A-Za-z0-9]*$") ~= nil
end

local function evaluation_array(key, format)
local value = params[key]
if value == nil then return nil end
Expand Down
Loading
Loading