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

Validate absolute paths in index.html #13

Merged
merged 2 commits into from
Mar 27, 2025
Merged
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
21 changes: 19 additions & 2 deletions src/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import { useSynthetix } from './useSynthetix';
import { carWriterOutToBlob, downloadCarFile, readFileAsUint8Array } from './utils';
import { getApiUrl } from './utils';

const checkIndexHtmlAbsolutePaths = async (file) => {
const fileContent = new TextDecoder().decode(await file.arrayBuffer());
const absolutePathRegex = /\b(src|href)="\/[^"]+"/g;
const matches = fileContent.match(absolutePathRegex);
return matches || [];
};

export function Project() {
const { heliaCar, fs } = useHelia();
const [params, setParams] = useParams();
Expand All @@ -28,18 +35,28 @@ export function Project() {
const [currentRemovingCid, setCurrentRemovingCid] = useState(null);
const [currentDagGetCid, setCurrentDagGetCid] = useState({ cidLoading: null, cidDag: null });

const handleFolderUpload = useCallback((e) => {
const handleFolderUpload = useCallback(async (e) => {
const filesToUpload = [...e.target.files];

if (filesToUpload.length === 0) {
return;
}

if (!filesToUpload.some((file) => file.name === 'index.html')) {
const indexHtmlFile = filesToUpload.find((file) => file.name === 'index.html');
if (!indexHtmlFile) {
setFileUploadError('Error: The index.html file is required for upload!');
return;
}

const absolutePaths = await checkIndexHtmlAbsolutePaths(indexHtmlFile);
if (absolutePaths.length > 0) {
setFileUploadError(
`Error: Absolute paths found in index.html: ${absolutePaths.join(', ')}. Please replace them with relative paths.`
);
e.target.value = '';
return;
}

setFileUploadError(null);
setFiles(filesToUpload);

Expand Down