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"}' -- example response: '{"skylink":"sia://XABvi7JtJbQSMAcDwnUnmp2FKDPjg8_tTTFP4BwMSxVdEg"}'
local hnsres_json = json.decode(hnsres_res.body) 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 if hnsres_json.skylink then
-- try to match the skylink with sia:// prefix -- try to match the skylink with sia:// prefix
skylink, skylink_rest = string.match(hnsres_json.skylink, "sia://([^/?]+)(.*)") skylink, skylink_rest = string.match(hnsres_json.skylink, "sia://([^/?]+)(.*)")
@ -212,7 +215,7 @@ server {
end end
ngx.var.skylink = skylink 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 ngx.var.rest = skylink_rest
else else
ngx.var.rest = request_uri_rest ngx.var.rest = request_uri_rest

View File

@ -1024,6 +1024,20 @@ function fileEndpointCheck(done) {
skylinkVerification(done, linkInfo); 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) { function parseHeaderString(header) {
try { try {
return JSON.parse(header); return JSON.parse(header);
@ -1033,27 +1047,37 @@ function parseHeaderString(header) {
} }
// skylinkVerification verifies a skylink against provided information. // 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(); const time = process.hrtime();
// Create the query for the skylink // 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 // Get the Skylink
superagent superagent
.get(query) .get(query)
.set("cookie", "nocache=true")
.redirects(redirects)
.ok((res) => (redirects === undefined ? res.ok : res.status < 400))
.responseType("blob") .responseType("blob")
.then( .then(
(response) => { (response) => {
const entry = { name, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) }; const entry = { name, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) };
const info = {}; const info = {};
if (statusCode && statusCode !== response.statusCode) {
entry.up = false;
info.statusCode = { expected: statusCode, current: response.statusCode };
}
// Check if the response body is valid by checking against the known hash // Check if the response body is valid by checking against the known hash
if (bodyHash) {
const currentBodyHash = hash(response.body); const currentBodyHash = hash(response.body);
if (currentBodyHash !== bodyHash) { if (currentBodyHash !== bodyHash) {
entry.up = false; entry.up = false;
info.bodyHash = { expected: bodyHash, current: currentBodyHash }; info.bodyHash = { expected: bodyHash, current: currentBodyHash };
} }
}
if (headers) { if (headers) {
Object.entries(headers).forEach(([headerName, expectedHeader]) => { Object.entries(headers).forEach(([headerName, expectedHeader]) => {
@ -1065,7 +1089,7 @@ function skylinkVerification(done, { name, skylink, bodyHash, headers }) {
if (typeof currentHeader === "object") { if (typeof currentHeader === "object") {
info.headers[headerName] = ensureValidJSON(detailedDiff(expectedHeader, currentHeader)); info.headers[headerName] = ensureValidJSON(detailedDiff(expectedHeader, currentHeader));
} else { } else {
info.headers[headerName] = currentHeader; info.headers[headerName] = { expected: expectedHeader, current: currentHeader };
} }
} }
}); });
@ -1118,4 +1142,5 @@ module.exports = [
// uniswapHNSRedirectCheck, // uniswapHNSRedirectCheck,
uniswapHNSResolverCheck, uniswapHNSResolverCheck,
uniswapHNSResolverRedirectCheck, uniswapHNSResolverRedirectCheck,
hnsEndpointDirectoryRedirect,
]; ];