Skip to content

Loading a malformed HTML project file crashes project import when expected <div class="code"> block is missing #7386

@sahu-virendra-1908

Description

@sahu-virendra-1908

Description

While testing project import behavior in Music Blocks, I noticed that loading certain malformed or non-project HTML files causes the import process to fail silently.

The issue happens in:

js/activity.js

when the loader detects that the file contains the word "html" and then immediately tries to extract project data using:

cleanData.match(...)[1]

Current logic:

if (cleanData.includes('id="codeBlock"')) {
    extracted = cleanData.match(
        '<div class="code" id="codeBlock">(.+?)</div>'
    )[1];
} else {
    extracted = cleanData.match(
        '<div class="code">(.+?)</div>'
    )[1];
}

If the loaded HTML file does not contain the expected Music Blocks project <div class="code"> element, match() returns null.

This causes:

TypeError: Cannot read properties of null (reading '1')

The exception is later swallowed by the surrounding try/catch, so the user only sees a blank canvas and the project does not load.

Expected Behavior

If the selected HTML file does not contain valid Music Blocks project data, the loader should fail gracefully and show a user-facing error message instead of crashing internally.

Example:

Cannot find project data in this HTML file.

Current Behavior

Malformed or unrelated HTML files can trigger a silent import failure.

The project loader:

  • throws a TypeError
  • stops loading
  • leaves the canvas blank
  • shows the error only in DevTools console

Steps to Reproduce

  1. Create an HTML file containing:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
Hello World
</body>
</html>
  1. Open Music Blocks
  2. Go to:
File → Open
  1. Select the HTML file

  2. Observe:

    • project fails to load
    • blank canvas remains
    • console shows:
TypeError: Cannot read properties of null (reading '1')

Root Cause

The loader assumes that any HTML file containing "html" also contains a valid Music Blocks project data block.

However, the code does not verify that match() actually succeeded before accessing [1].

Suggested Fix

Add a null check before reading the regex capture group:

let matchResult;

if (cleanData.includes('id="codeBlock"')) {
    matchResult = cleanData.match(
        '<div class="code" id="codeBlock">(.+?)</div>'
    );
} else {
    matchResult = cleanData.match(
        '<div class="code">(.+?)</div>'
    );
}

if (!matchResult || !matchResult[1]) {
    that.errorMsg(_("Cannot find project data in this HTML file."));
    finishLoading();
    return;
}

extracted = matchResult[1];

The same guard should also be applied to the second identical code path later in activity.js.

Checklist

  • I have read and followed the project's code of conduct.
  • I have searched for similar issues before creating this one.
  • I have provided all the necessary information to understand and reproduce the issue.
  • I am willing to contribute to the resolution of this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions