Skip to content

fix(worker): validate URL origin before importScripts#5198

Open
marcoscaceres wants to merge 5 commits intomainfrom
alert-autofix-24
Open

fix(worker): validate URL origin before importScripts#5198
marcoscaceres wants to merge 5 commits intomainfrom
alert-autofix-24

Conversation

@marcoscaceres
Copy link
Copy Markdown
Contributor

Potential fix for https://github.com/speced/respec/security/code-scanning/24

The safest fix is to enforce strict validation/allowlisting of langURL before calling importScripts, and reject anything not matching expected trusted URLs. Also validate propName to prevent unsafe property access, and ensure the loaded language registration function exists before use.

Best single approach without changing intended behavior:

  • In worker/respec-worker.js, inside the "highlight-load-lang" case, replace direct importScripts(langURL) with:
    1. Parse URL via new URL(langURL, self.location.href).
    2. Allow only http:/https: schemes.
    3. Restrict to same-origin (url.origin === self.location.origin) so only locally hosted language files load.
    4. Optionally enforce expected path prefix (e.g., /) and .js extension.
    5. Validate propName as a safe identifier and ensure self[propName] is a function before registering.
    6. Wrap in try/catch, log and skip on invalid input.

This keeps dynamic language loading, but removes attacker-controlled arbitrary script URL loading.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@marcoscaceres
Copy link
Copy Markdown
Contributor Author

@copilot see the CI error about the commit message. Can you fix that please?

…ad-lang

Co-authored-by: marcoscaceres <870154+marcoscaceres@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

@copilot see the CI error about the commit message. Can you fix that please?

Done in commit 9faf188. The CI check (action-semantic-pull-request) validates the PR title, not the individual commit messages. The new commit uses fix(worker): validate URL origin before importScripts in highlight-load-lang as the message. The PR title will also need to be updated to a conventional commit format (e.g., fix(worker): validate URL origin before importScripts in highlight-load-lang) for the CI check to pass.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/graphql
    • Triggering command: /usr/bin/gh gh auth status (http block)

If you need me to access, download, or install something from one of these locations, you can either:

@marcoscaceres marcoscaceres changed the title Potential fix for code scanning alert no. 24: Client-side URL redirect fix: code scanning alert no. 24: Client-side URL redirect Apr 14, 2026
@marcoscaceres marcoscaceres changed the title fix: code scanning alert no. 24: Client-side URL redirect fix(worker): validate URL origin before importScripts Apr 14, 2026
@marcoscaceres
Copy link
Copy Markdown
Contributor Author

@copilot Can you fix the lint error from ci?

Agent-Logs-Url: https://github.com/speced/respec/sessions/eaf97df3-cfd4-4999-92cf-047f0b3b3ca4

Co-authored-by: marcoscaceres <870154+marcoscaceres@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

@copilot Can you fix the lint error from ci?

Fixed in commit d7c435c. The two Prettier formatting issues were long lines that needed to be broken up — the multi-condition if statement and the long error message string.

@marcoscaceres marcoscaceres marked this pull request as ready for review April 15, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants