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: unmet constraints should return a 400 #589

Merged
merged 2 commits into from
Nov 12, 2024
Merged
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
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 bad json-ld', 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
Loading