Module:Sensitive IP addresses/API: Difference between revisions

Content deleted Content added
start writing the query function
m Protected "Module:Sensitive IP addresses/API": High-risk Lua module: used in the MediaWiki interface, e.g. MediaWiki:Blockiptext via Template:Sensitive IP addresses ([Edit=Require administrator access] (indefinite...
 
(16 intermediate revisions by the same user not shown)
Line 7:
local IPv4Collection = mIP.IPv4Collection
local IPv6Collection = mIP.IPv6Collection
 
local libraryUtil = require('libraryUtil')
-- Lazily load the jf-JSON module
local checkType = libraryUtil.checkType
local JSON
 
-------------------------------------------------------------------------------
-- Helper functions
-------------------------------------------------------------------------------
 
local function deepCopy(val)
-- Make a deep copy of a value, but don't worry about self-references or
-- metatables as mw.clone does. If a table in val has a self-reference,
-- you will get an infinite loop, so don't do that.
if type(val) == 'table' then
local ret = {}
for k, v in pairs(val) do
ret[k] = deepCopy(v)
end
return ret
else
return val
end
end
 
local function deepCopyInto(source, dest)
-- Do a deep copy of a source table into a destination table, ignoring
-- self-references and metatables. If a table in source has a self-reference
-- you will get an infinite loop.
for k, v in pairs(source) do
if type(v) == 'table' then
dest[k] = {}
deepCopyInto(v, dest[k])
else
dest[k] = v
end
end
end
 
local function removeDuplicates(t)
-- Return a copy of an array with duplicate values removed.
local keys, ret = {}, {}
for i, v in ipairs(t) do
if not keys[v] then
table.insert(ret, v)
keys[v] = true
end
end
return ret
end
 
-------------------------------------------------------------------------------
Line 30 ⟶ 76:
-- Private methods
local function addRanges(self, key, collectionConstructor, ranges)
if ranges and #ranges > 0[1] then
self[key] = collectionConstructor()
for i, range in ipairs(ranges) do
Line 52 ⟶ 98:
 
function SensitiveEntity:matchesIPOrRange(str)
-- Returns true, matchObj, queryObj if there is a match for the IP address
checkType('matchesIPOrRange', 1, str, 'string')
-- string or CIDR range str in the sensitive entity. Returns false
-- otherwise. matchObj is the Subnet object that was matched, and queryObj
-- is the IPAddress or Subnet object corresponding to the input string.
 
-- Get the IPAddress or Subnet object for str
local isIP, isSubnet, obj
isIP, obj = pcall(IPAddress.new, str)
if isIP and not ipobj then
isIP = false
end
 
if not isIP then
isSubnet, obj = pcall(Subnet.new, str)
Line 73 ⟶ 123:
local function isInCollection(collection, obj, isIP)
if isIP then
returnif collection and collection:containsIP(obj) or falsethen
local isMatch, matchObj = collection:containsIP(obj)
return isMatch, matchObj, obj
else
return false
end
else
returnif collection and collection:overlapsSubnet(obj) or falsethen
local isMatch, matchObj = collection:overlapsSubnet(obj)
return isMatch, matchObj, obj
else
return false
end
end
end
Line 110 ⟶ 170:
-- {
-- entities = {'all'}
-- }
--
-- Query all entities and format the result as a JSON string:
-- {
-- entities = {'all'},
-- format = 'json'
-- }
--
Line 184 ⟶ 250:
local function query(options)
-- Make entity objects
local entities, entityIdsentityIndexes = {}, {}
local data = mw.loadData('Module:Sensitive IP addresses/list')
for i, entityData in ipairs(data) do
entities[entityData.id] = SensitiveEntity.new(entityData)
entityIndexes[entityData.id] = i -- Keep track of the original order
table.insert(entityIds, entityData.id)
end
 
local function makeError(code, info, format)
local ret = {['error'] = {
return {
code = code,
info = info,
['*'] = 'See https://en.wikipedia.org/wiki/Module:Sensitive_IP_addresses/API for API usage',
}}
if format == 'json' then
return mw.text.jsonEncode(ret)
else
return ret
end
end
 
-- Construct result
local result = {}
matches = {},
['matched-ranges'] = {},
entities = {},
['entity-ids'] = {}
}
 
if type(options) ~= 'table' then
Line 209 ⟶ 285:
type(options)
)
)
elseif not options.test and not options.entities then
return makeError(
'sipa-blank-options',
"the options table didn't contain a 'test' or an 'entities' key",
options.format
)
end
Line 216 ⟶ 298:
return makeError(
'sipa-test-type-error',
string.format(
"'test' options key was type %s (expected table)",
type(options.test))
),
options.format
)
end
Line 228 ⟶ 314:
i,
type(testString)
),
options.format
)
end
end
end
end
 
for k, entity in pairs(entities) do
-- Try to match the range with the current sensitive entity.
local success, isMatch, matchObj, queryObj = pcall(
entity.matchesIPOrRange,
entity,
testString
)
if not success then
-- The string was invalid.
return makeError(
'sipa-invalid-test-string',
string.format(
"test string #%d '%s' was not a valid IP address or CIDR string",
i,
testString
),
options.format
)
end
if isMatch then
-- The string was a sensitive IP address or subnet.
 
-- Add match data
--------------------------------------------------------------------------------
local match = {}
-- Q&D demo of loading data from [[Module:Sensitive IP addresses/list]]
-- Quick and dirty hack to find if queryObj is an IPAddress object.
-- into a structure that could be used to determine whether a particular
local isIP = queryObj.getNextIP ~= nil and queryObj.isInSubnet ~= nil
-- IP or subnet overlaps a sensitive range.
if isIP then
-- If used, this would be greatly refactored and possibly split to
match.type = 'ip'
-- [[Module:Sensitive IP addresses/data]].
match.ip = tostring(queryObj)
--
else
-- Usage in a sandbox:
match.type = 'range'
-- {{#invoke:Sensitive IP addresses|main}}
match.range = tostring(queryObj)
end
match['ip-version'] = queryObj:getVersion()
match['matches-range'] = matchObj:getCIDR()
match['entity-id'] = entity.data.id
table.insert(result.matches, match)
 
-- Add the matched range data.
result['matched-ranges'][match['matches-range']] = {
range = match['matches-range'],
['ip-version'] = match['ip-version'],
['entity-id'] = match['entity-id'],
}
 
-- Add the entity data for the entity we matched.
local function main()
result.entities[match['entity-id']] = deepCopy(
-- Test Module:IP.
entities[match['entity-id']].data
----------------------------------------------------------------------------
)
-- An IP collection in Module:IP should hold both IPv4 and IPv6 lists and
 
-- it would use the appropriate list depending on the object queried?
-- Add the entity ID for the entity we matched.
-- That would make this code more straight forward.
table.insert(result['entity-ids'], match['entity-id'])
----------------------------------------------------------------------------
end
-- Support stuff
----------------------------------------------------------------------------
local modcode = require('Module:IP')
local IPAddress = modcode.IPAddress
local Subnet = modcode.Subnet
local IPv4Collection = modcode.IPv4Collection
local IPv6Collection = modcode.IPv6Collection
local Collection = {}
Collection.__index = Collection
do
function Collection:add(item)
if item ~= nil then
self.n = self.n + 1
self[self.n] = item
end
end
function Collection:join(sep)
return table.concat(self, sep)
end
function Collection:sort(comp)
table.sort(self, comp)
end
function Collection.new()
return setmetatable({n = 0}, Collection)
end
end
 
local function getObject(ipStr)
-- Add entity data requested explicitly.
-- Parse a string and return an appropriate object:
if options.entities then
-- IPv4 or IPv6 IP or subnet, or nil.
if type(options.entities) ~= 'table' then
-- TODO This should be in Module:IP (see IPCollection:_store).
return makeError(
local maker
'sipa-entities-type-error',
if ipStr:find('/', 1, true) then
string.format(
maker = Subnet.new
"'entities' options key was type %s (expected table)",
else
type(options.test)
maker = IPAddress.new
),
options.format
)
end
 
local success, obj = pcall(maker, ipStr)
-- Check the type of all the entity strings, and check if 'all' has
if success then
-- been specified.
return obj
local isAll = false
end
for i, entityString in ipairs(options.entities) do
return nil
if type(entityString) ~= 'string' then
end
return makeError(
local function preBlock(text)
'sipa-entity-string-type-error',
-- Pre tags returned by a module do not act like wikitext <pre>...</pre>.
string.format(
return '<pre>\n' ..
"type error in item #%d in the 'entities' array (expected string, received %s)",
mw.text.nowiki(text) ..
i,
(text:sub(-1) == '\n' and '' or '\n') ..
type(entityString)
'</pre>\n'
),
end
options.format
----------------------------------------------------------------------------
)
-- Load sensitive IP information
----------------------------------------------------------------------------
local function loadList(modname)
-- Return a table to query an IP/subnet wrt sensitive ranges.
local data = {
subnetToInfo = {},
v4Collection = IPv4Collection.new(),
v6Collection = IPv6Collection.new(),
}
local sensitiveList = mw.loadData(modname)
for i, info in ipairs(sensitiveList) do
for _, r in ipairs({
{key = 'ipv4Ranges', list = data.v4Collection},
{key = 'ipv6Ranges', list = data.v6Collection},
}) do
local rangeStrings = info[r.key]
if rangeStrings then
for _, str in ipairs(rangeStrings) do
local subnet = Subnet.new(str)
r.list:addSubnet(subnet)
data.subnetToInfo[subnet] = info
end
end
end
if entityString == 'all' then
end
isAll = true
return data
end
----------------------------------------------------------------------------
-- Run test using Module:IP
----------------------------------------------------------------------------
local data = loadList('Module:Sensitive IP addresses/list')
local results = Collection.new()
results:add('IP ranges equivalent to collection')
for _, col in ipairs({data.v4Collection, data.v6Collection}) do
for _, range in ipairs(col:getRanges()) do
if range[1] == range[2] then
results:add(' ' .. range[1])
else
results:add(' ' .. range[1] .. ' – ' .. range[2])
end
end
 
end
if isAll then
for _, ipStr in ipairs({
-- Add all the entity data.
-- Each of the following is tested against the sensitive list.
-- As the final result will contain all the entity data, we can
'143.228.19.123',
-- just create the entities and entity-ids subtables from scratch
'2620:0:E21:9F2::',
-- without worrying about what any existing values might be.
'131.132.224.0/19',
result.entities = {}
'198.35.27.255',
result['entity-ids'] = {}
'2620:0:860::1',
for i, entityData in ipairs(data) do
'1.2.3.4',
result.entities[entityData.id] = deepCopy(entityData)
'11.12.13.192/26',
result['entity-ids'][i] = entityData.id
'2001:db8::abcd',
'2001:db8::/72',
}) do
local obj = getObject(ipStr)
if obj then
local isPresent, clashObj
local col = obj:getVersion() == 'IPv4' and
data.v4Collection or data.v6Collection
if obj.getNextIP then -- dirty trick to check if obj is an IP
isPresent, clashObj = col:containsIP(obj)
else
isPresent, clashObj = col:overlapsSubnet(obj)
end
else
results:add('')
-- Add data for the entities specified.
results:add('IP or range under test: ' .. ipStr)
-- Insert the entity and entity-id subtables if they aren't already
if isPresent then
-- present.
local info = data.subnetToInfo[clashObj]
for i, entityString in ipairs(options.entities) do
if info then
if entities[entityString] then
results:add(' sensitive: ' .. clashObj)
result.entities[entityString] = deepCopy(
results:add(' name: ' .. (info.name or '?'))
entities[entityString].data
results:add(' id: ' .. (info.id or '?'))
)
results:add(' description: ' .. (info.description or '?'))
table.insert(result['entity-ids'], entityString)
results:add(' reason: ' .. (info.reason or '?'))
else
-- Should not occur!
results:add(' info not found!')
end
else
results:add(' not sensitive')
end
result['entity-ids'] = removeDuplicates(result['entity-ids'])
else
table.sort(result['entity-ids'], function(s1, s2)
-- Report problem?
return entityIndexes[s1] < entityIndexes[s2]
end)
end
end
 
return preBlock(results:join('\n'))
-- Add any missing reason fields from entities.
for id, entityData in pairs(result.entities) do
entityData.reason = entityData.reason or 'political'
end
 
-- Wrap the result in an outer layer like the MediaWiki Action API does.
result = {sensitiveips = result}
 
if options.format == 'json' then
-- Load jf-JSON
JSON = JSON or require('Module:jf-JSON')
JSON.strictTypes = true -- Necessary for correct blank-object encoding
-- Decode a skeleton result JSON string. This ensures that blank objects
-- are re-encoded as blank objects and not as blank arrays.
local jsonResult = JSON:decode([[{"sensitiveips": {
"matches": [],
"matched-ranges": {},
"entities": {},
"entity-ids": []
}}]])
for i, key in ipairs{'matches', 'matched-ranges', 'entities', 'entity-ids'} do
deepCopyInto(result.sensitiveips[key], jsonResult.sensitiveips[key])
end
return JSON:encode(jsonResult)
elseif options.format == nil or options.format == 'lua' then
return result
elseif type(options.format) ~= 'string' then
return makeError(
'sipa-format-type-error',
string.format(
"'format' options key was type %s (expected string or nil)",
type(options.format)
)
)
else
return makeError(
'sipa-invalid-format',
string.format(
"invalid format '%s' (expected 'json' or 'lua')",
type(options.format)
)
)
end
end
 
Line 396 ⟶ 489:
 
local p = {}
p.main = main
 
function p.isValidSensitivityReason_isValidSensitivityReason(s)
-- Return true if s is a valid sensitivity reason; otherwise return false.
return s ~= nil and SensitiveEntity.reasons[s] ~= nil
checkType('isValidSensitivityReason', 1, s, 'string')
return SensitiveEntity.reasons[s] ~= nil
end
 
function p.getSensitivityReasons_getSensitivityReasons(separator, conjunction)
-- Return an arraystring of valid sensitivity reasons, ordered alphabetically.
-- The reasons are separated by an optional separator; if conjunction is
local ret = {}
-- specified it is used instead of the last separator, as in
-- mw.text.listToText.
 
-- Get an array of valid sensitivity reasons.
local reasons = {}
for reason in pairs(SensitiveEntity.reasons) do
retreasons[#retreasons + 1] = reason
end
table.sort(retreasons)
return ret
end
 
-- Convert arguments if we are being called from wikitext.
function p.query()
if type(separator) == 'table' and type(separator.getParent) == 'function' then
-- separator is a frame object
local frame = separator
separator = frame.args[1]
conjunction = frame.args[2]
end
 
-- Return a formatted string
return mw.text.listToText(reasons, separator, conjunction)
end
 
-- Export the API query function
p.query = query
 
return p