Merge pull request #604 from NebulousLabs/fix-hns-app-links

fix hns links redirects
This commit is contained in:
Karol Wypchło 2021-01-28 13:22:23 +01:00 committed by GitHub
commit 879fb33d3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 8 deletions

View File

@ -176,6 +176,9 @@ server {
-- example response: '{"skylink":"sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"}'
local hnsres_json = json.decode(hnsres_res.body)
-- define local variable containing rest of the skylink if provided
local skylink_rest
if hnsres_json.skylink then
-- try to match the skylink with sia:// prefix
skylink, skylink_rest = string.match(hnsres_json.skylink, "sia://([^/?]+)(.*)")
@ -212,7 +215,7 @@ server {
end
ngx.var.skylink = skylink
if request_uri_rest == "/" and skylink_rest ~= "" and skylink_rest ~= "/" then
if request_uri_rest == "/" and skylink_rest ~= nil and skylink_rest ~= "" and skylink_rest ~= "/" then
ngx.var.rest = skylink_rest
else
ngx.var.rest = request_uri_rest

View File

@ -1024,6 +1024,20 @@ function fileEndpointCheck(done) {
skylinkVerification(done, linkInfo);
}
// check whether hns/note-to-self would properly redirect to note-to-self/
function hnsEndpointDirectoryRedirect(done) {
const expected = {
name: "hns endpoint directory redirect",
skylink: "hns/note-to-self",
statusCode: 307,
headers: {
location: "note-to-self/",
},
};
skylinkVerification(done, expected, { redirects: 0 });
}
function parseHeaderString(header) {
try {
return JSON.parse(header);
@ -1033,26 +1047,36 @@ function parseHeaderString(header) {
}
// skylinkVerification verifies a skylink against provided information.
function skylinkVerification(done, { name, skylink, bodyHash, headers }) {
function skylinkVerification(done, { name, skylink, bodyHash, headers, statusCode }, { redirects } = {}) {
const time = process.hrtime();
// Create the query for the skylink
const query = `${process.env.PORTAL_URL}/${skylink}?nocache=true`;
const query = `${process.env.PORTAL_URL}/${skylink}`;
// Get the Skylink
superagent
.get(query)
.set("cookie", "nocache=true")
.redirects(redirects)
.ok((res) => (redirects === undefined ? res.ok : res.status < 400))
.responseType("blob")
.then(
(response) => {
const entry = { name, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) };
const info = {};
// Check if the response body is valid by checking against the known hash
const currentBodyHash = hash(response.body);
if (currentBodyHash !== bodyHash) {
if (statusCode && statusCode !== response.statusCode) {
entry.up = false;
info.bodyHash = { expected: bodyHash, current: currentBodyHash };
info.statusCode = { expected: statusCode, current: response.statusCode };
}
// Check if the response body is valid by checking against the known hash
if (bodyHash) {
const currentBodyHash = hash(response.body);
if (currentBodyHash !== bodyHash) {
entry.up = false;
info.bodyHash = { expected: bodyHash, current: currentBodyHash };
}
}
if (headers) {
@ -1065,7 +1089,7 @@ function skylinkVerification(done, { name, skylink, bodyHash, headers }) {
if (typeof currentHeader === "object") {
info.headers[headerName] = ensureValidJSON(detailedDiff(expectedHeader, currentHeader));
} else {
info.headers[headerName] = currentHeader;
info.headers[headerName] = { expected: expectedHeader, current: currentHeader };
}
}
});
@ -1118,4 +1142,5 @@ module.exports = [
// uniswapHNSRedirectCheck,
uniswapHNSResolverCheck,
uniswapHNSResolverRedirectCheck,
hnsEndpointDirectoryRedirect,
];