Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed wkd.lookup in case of HTML being returned in fetch response (closes #3) #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion src/wkd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

// inspired by `is-html`
// <https://github.com/sindresorhus/is-html/blob/bc57478683406b11aac25c4a7df78b66c42cc27c/index.js#L1-L11>
const basic = /\s?<!doctype html>|(<html\b[^>]*>|<body\b[^>]*>|<x-[^>]+>)+/i;

/**
* This class implements a client for the Web Key Directory (WKD) protocol
* in order to lookup keys on designated servers.
Expand Down Expand Up @@ -69,7 +73,16 @@ class WKD {
}
}

return new Uint8Array(await response.arrayBuffer());
const uint8Array = new Uint8Array(await response.arrayBuffer())

// inspired by `is-html`
// <https://github.com/sindresorhus/is-html/blob/bc57478683406b11aac25c4a7df78b66c42cc27c/index.js#L1-L11>
const str = new TextDecoder().decode(uint8Array);
if (str && basic.test(str.trim().slice(0, 1000))) {
throw new Error('Invalid WKD lookup result (HTML content detected)');
}

return uint8Array;
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/wkd.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ describe('WKD unit tests', function() {
expect(error.message).to.equal('Direct WKD lookup failed: Not Found')
});
});

it('by email address should not work on invalid website', function() {
return wkd.lookup({
email: '[email protected]'
}).then(function() {
throw new Error('Lookup should throw an error');
}).catch(function(error) {
expect(error.message).to.equal('Invalid WKD lookup result (text/html Content-Type header)');
});
});
});

});