Skip to content

Commit 04febaf

Browse files
authored
fix: sanitize aem paths to da content paths for library (#928)
1 parent f45e409 commit 04febaf

4 files changed

Lines changed: 73 additions & 6 deletions

File tree

blocks/edit/da-library/da-library.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import getSheet from '../../shared/sheet.js';
66
import inlinesvg from '../../shared/inlinesvg.js';
77
import { daFetch } from '../../shared/utils.js';
88
import searchFor from './helpers/search.js';
9-
import { OOTB_PLUGINS, loadLibrary, getItemDetails, getPreviewStatus, ref } from './helpers/helpers.js';
9+
import {
10+
OOTB_PLUGINS,
11+
loadLibrary,
12+
getItemDetails,
13+
getPreviewStatus,
14+
aemToContentUrl,
15+
ref,
16+
} from './helpers/helpers.js';
1017

1118
const sheet = await getSheet('/blocks/edit/da-library/da-library.css');
1219
const buttons = await getSheet(`${getNx()}/styles/buttons.css`);
@@ -184,7 +191,7 @@ class DaLibrary extends LitElement {
184191
}
185192

186193
async handleTemplateClick(item) {
187-
const resp = await daFetch(`${item.value}`);
194+
const resp = await daFetch(aemToContentUrl(item.value));
188195
if (!resp.ok) return;
189196
let text = await resp.text();
190197

blocks/edit/da-library/helpers/helpers.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { DOMParser } from 'da-y-wrapper';
33
import getPathDetails from '../../../shared/pathDetails.js';
44
import { daFetch, aemAdmin, fetchDaConfigs, getFirstSheet, getSheetByName } from '../../../shared/utils.js';
5+
import { CON_ORIGIN } from '../../../shared/constants.js';
56
import { openAssets } from '../../da-assets/da-assets.js';
67
import { fetchKeyAutocompleteData } from '../../prose/plugins/slashMenu/keyAutocomplete.js';
78
import { sanitizeName } from '../../../../scripts/utils.js';
@@ -44,11 +45,27 @@ function setupBlockOptions(library) {
4445
if (blockJsonUrl) fetchKeyAutocompleteData(blockJsonUrl);
4546
}
4647

48+
const AEM_CONTENT_HOST = /\.(aem|hlx)\.(page|live)$/;
49+
50+
export function aemToContentUrl(url) {
51+
try {
52+
const { hostname, pathname, search } = new URL(url);
53+
if (!AEM_CONTENT_HOST.test(hostname)) return url;
54+
const parts = hostname.split('--');
55+
if (parts.length !== 3) return url;
56+
const [, site, orgWithTld] = parts;
57+
const [org] = orgWithTld.split('.');
58+
return `${CON_ORIGIN}/${org}/${site}${pathname}${search}`;
59+
} catch {
60+
return url;
61+
}
62+
}
63+
4764
export async function getItems(sources, format) {
4865
const items = [];
4966
for (const source of sources) {
5067
try {
51-
const resp = await daFetch(source);
68+
const resp = await daFetch(aemToContentUrl(source));
5269
const json = await resp.json();
5370
if (json.data) {
5471
items.push(...formatData(json.data, format));

blocks/edit/da-library/helpers/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { daFetch, getFirstSheet } from '../../../shared/utils.js';
22
import { getMetadata } from '../../utils/helpers.js';
3-
import { parseDom } from './helpers.js';
3+
import { parseDom, aemToContentUrl } from './helpers.js';
44

55
const AEM_ORIGIN = ['hlx.page', 'hlx.live', 'aem.page', 'aem.live'];
66

@@ -191,7 +191,7 @@ export async function getBlocks(sources) {
191191
}
192192

193193
try {
194-
const resp = await daFetch(url, { noRedirect: true });
194+
const resp = await daFetch(aemToContentUrl(url), { noRedirect: true });
195195
if (!resp.ok) throw new Error('Something went wrong.');
196196
const data = await resp.json();
197197
urlCache.set(url, data);
@@ -210,7 +210,7 @@ export async function getBlocks(sources) {
210210
if (block.name && block.path) {
211211
acc.push({
212212
...block,
213-
loadVariants: getBlockVariants(block.path),
213+
loadVariants: getBlockVariants(aemToContentUrl(block.path)),
214214
});
215215
}
216216
});

test/unit/blocks/edit/da-library/helpers.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
getMetadata,
99
getPreviewUrl,
1010
getAemUrlVars,
11+
aemToContentUrl,
1112
getItemDetails,
1213
getItems,
1314
getPreviewStatus,
@@ -118,6 +119,35 @@ describe('da-library/helpers exports', () => {
118119
});
119120
});
120121

122+
describe('aemToContentUrl', () => {
123+
it('Rewrites aem.page preview URLs to content.da.live', () => {
124+
expect(aemToContentUrl('https://main--site--org.aem.page/blocks/cards'))
125+
.to.equal('https://content.da.live/org/site/blocks/cards');
126+
});
127+
128+
it('Rewrites aem.live, hlx.page and hlx.live URLs the same way', () => {
129+
expect(aemToContentUrl('https://main--site--org.aem.live/blocks/cards'))
130+
.to.equal('https://content.da.live/org/site/blocks/cards');
131+
expect(aemToContentUrl('https://main--site--org.hlx.page/blocks/cards'))
132+
.to.equal('https://content.da.live/org/site/blocks/cards');
133+
expect(aemToContentUrl('https://feature--site--org.hlx.live/deep/nested/path'))
134+
.to.equal('https://content.da.live/org/site/deep/nested/path');
135+
});
136+
137+
it('Leaves content.da.live and admin.da.live URLs unchanged', () => {
138+
const con = 'https://content.da.live/org/site/blocks/cards';
139+
expect(aemToContentUrl(con)).to.equal(con);
140+
const admin = 'https://admin.da.live/source/org/site/blocks/cards';
141+
expect(aemToContentUrl(admin)).to.equal(admin);
142+
});
143+
144+
it('Leaves unrelated origins, relative paths and non-URL strings unchanged', () => {
145+
expect(aemToContentUrl('https://example.com/blocks/cards')).to.equal('https://example.com/blocks/cards');
146+
expect(aemToContentUrl('/blocks/cards.json')).to.equal('/blocks/cards.json');
147+
expect(aemToContentUrl('not a url')).to.equal('not a url');
148+
});
149+
});
150+
121151
describe('getItemDetails', () => {
122152
it('Parses an aem.live URL', () => {
123153
const result = getItemDetails({ path: 'https://main--repo--org.aem.live/folder/page' });
@@ -236,6 +266,19 @@ describe('da-library/helpers/index getBlocks', () => {
236266
expect(result).to.deep.equal([]);
237267
});
238268

269+
it('Rewrites an AEM source URL to content.da.live before fetching', async () => {
270+
let captured;
271+
window.fetch = (url) => {
272+
captured = url;
273+
return Promise.resolve(new Response(
274+
JSON.stringify({ ':type': 'sheet', data: [] }),
275+
{ status: 200 },
276+
));
277+
};
278+
await getBlocks(['https://main--repo--org.aem.page/blocks.json']);
279+
expect(captured).to.equal('https://content.da.live/org/repo/blocks.json');
280+
});
281+
239282
it('Caches fetched source data so subsequent calls do not refetch', async () => {
240283
let calls = 0;
241284
window.fetch = () => {

0 commit comments

Comments
 (0)