Skip to content

Commit

Permalink
fix: unmet constraints should return a 400
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister committed Nov 12, 2024
1 parent 8c03517 commit 8acfdb2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/html2md.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
} from './mdast-table-handler.js';
import formatPlugin from './markdownFormatPlugin.js';

export class ConstraintsError extends Error {}

const HELIX_META = {
viewport: true,
};
Expand Down Expand Up @@ -80,25 +82,25 @@ function toGridTable(title, data) {
/**
* @param {string} str
* @returns {string}
* @throws {Error}
* @throws {ConstraintsError} when it is not valid JSON
*/
function assertValidJSON(str) {
try {
return JSON.stringify(JSON.parse(str.trim()));
} catch {
throw Error('invalid json-ld');
throw new ConstraintsError('invalid json-ld');
}
}

/**
* @param {string} str
* @param {number} [limit]
* @returns {string}
* @throws {Error}
* @throws {ConstraintsError} when metadata size limit is exceeded
*/
function assertMetaSizeLimit(str, limit = 128_000) {
if (str && str.length > limit) {
throw Error('metadata size limit exceeded');
throw new ConstraintsError('metadata size limit exceeded');
}
return str;
}
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { cleanupHeaderValue } from '@adobe/helix-shared-utils';
import { MediaHandler } from '@adobe/helix-mediahandler';
import pkgJson from './package.cjs';
import { html2md } from './html2md.js';
import { ConstraintsError, html2md } from './html2md.js';
import { TooManyImagesError } from './mdast-process-images.js';

/* c8 ignore next 7 */
Expand Down Expand Up @@ -213,6 +213,9 @@ async function run(request, ctx) {
if (e instanceof TooManyImagesError) {
return error(`error fetching resource at ${sourceUrl}: ${e.message}`, 409);
}
if (e instanceof ConstraintsError) {
return error(`error fetching resource at ${sourceUrl}: ${e.message}`, 400);
}
/* c8 ignore next 3 */
log.debug(e.stack);
return error(`error fetching resource at ${sourceUrl}: ${e.message}`, 500);
Expand Down
32 changes: 32 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,38 @@ describe('Index Tests', () => {
});
}

it('returns 400 for invalid metadata', async () => {
const html = `<html>
<head>
<script type="application/ld+json">
{"@context":"http://schema.org",.
</script>
</head>
<body>
<main>
<div>
<h1>Hello, World.</h1>
</div>
</main>
</body>
</html>
`;
nock('https://www.example.com')
.get('/')
.reply(200, html);

const result = await main(reqUrl('/'), { log: console, env: {} });
assert.strictEqual(result.status, 400);
assert.strictEqual(await result.text(), '');
assert.deepStrictEqual(result.headers.plain(), {
'cache-control': 'no-store, private, must-revalidate',
'content-type': 'text/plain; charset=utf-8',
'x-error': 'error fetching resource at https://www.example.com/: invalid json-ld',
});
});

it('returns 409 for too many different images', async () => {
let html = '<html><body><main><div>';
for (let i = 0; i < 101; i += 1) {
Expand Down

0 comments on commit 8acfdb2

Please sign in to comment.