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

Write title as innerText instead of innerHTML #3173

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

emilhem
Copy link

@emilhem emilhem commented Feb 7, 2025

Description

Changing the title update to use innerText instead of innerHTML calms some security checks such as GitHub's own code checking.

tsc is grumpy now though with the error Property 'innerText' does not exist on type 'Element'.

Not sure if i agree with tsc fully.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@geoffrey-eisenbarth
Copy link
Contributor

Are you able to run the test suite? Let me know if you need any help!

Copy link
Contributor

@geoffrey-eisenbarth geoffrey-eisenbarth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

fyi I can't approve the PR, just trying to help other contributors :)

Edit: as @Telroshan pointed out, this should still be .innerText, the type cast just makes it treat titleElt as an HTMLElement instead of the Element | null type that is output by find().

I need more sleep.

@Telroshan
Copy link
Collaborator

I must admit I'm a bit confused here ; innerHTML is a property of the Element interface (see MDN docs), so we shouldn't even need a type cast to HTMLElement, unless I'm missing something?

Moreover, the PR mentions replacing innerHTML by innerText but it looks like the current PR simply adds a type cast comment without changing the accessed property, is that intended?

Could you also share the ouput of the failing security checks you mentioned in the PR description?

@geoffrey-eisenbarth
Copy link
Contributor

@Telroshan The first commit changed .innerHTML to .innerText, but looks like when they added the type cast they reverted it back to .innerHTML.

You're right that .innerHTML is a property of Element, but as far as I can tell .innerText is not, which is why I suggested the type cast, since <title> will always be an HTMLElement.

Agreed it would be nice to have the output of the failing security test.

@emilhem
Copy link
Author

emilhem commented Feb 10, 2025

Here's the output from GitHub's CodeQL

Screenshot 2025-02-10 at 09-28-03 Client-side cross-site scripting · Code scanning alert #14 · volvo-cars_e2e-qa-blink

Unsure how I messed up the commit, but I'll commit now.

@emilhem emilhem force-pushed the write_title_as_text branch 2 times, most recently from 8ec2d75 to 8f85142 Compare February 10, 2025 08:37
@emilhem
Copy link
Author

emilhem commented Feb 10, 2025

Okay, so I accidentally rebased master then dev. That's why there was a bunch of extra stuff going on above.

The fix is in now, but the tests are failing since the type casting didn't seem to do the trick. Either I get

> [email protected] types-check
> tsc src/htmx.js --noEmit --checkJs --target es6 --lib dom,dom.iterable

src/htmx.js:4690:18 - error TS2339: Property 'innerText' does not exist on type 'Element'.

4690         titleElt.innerText = title
                      ~~~~~~~~~

Or if I move the type cast to where titleElt is set I get

> [email protected] types-check
> tsc src/htmx.js --noEmit --checkJs --target es6 --lib dom,dom.iterable

src/htmx.js:4688:13 - error TS2740: Type 'Element' is missing the following properties from type 'HTMLElement': accessKey, accessKeyLabel, autocapitalize, dir, and 123 more.

4688       const titleElt = find('title')
                 ~~~~~~~~

@emilhem
Copy link
Author

emilhem commented Feb 10, 2025

Well, I borked the commit history obviously... I'll see what I can do to fix it..

@Telroshan
Copy link
Collaborator

Ok I see, thanks for the screenshot

Tbh, this kind of warning is kinda a lost cause regarding htmx as we support eval which is reported as dangerous by many scanners
Though, as the title tag can only contain text as per the docs, I see no reason to oppose to use ìnnerText here

As for the property itself, I would maybe recommend textContent instead of innerText, as

Some security checks are grumpy when using innerHTML. Using innerText
instead calms them.

Signed-off-by: Emil Hemdal <[email protected]>
@Telroshan
Copy link
Collaborator

As for the branch, you would indeed want to rebase to dev and drop all the unnecessary commits

@emilhem emilhem force-pushed the write_title_as_text branch from 8f85142 to c16d1f2 Compare February 10, 2025 08:47
Also remove type hinting/casting since it is no longer needed

Signed-off-by: Emil Hemdal <[email protected]>
@emilhem
Copy link
Author

emilhem commented Feb 10, 2025

Done! Thanks @Telroshan ! :)

@Telroshan Telroshan added the enhancement New feature or request label Feb 10, 2025
@Telroshan Telroshan added the ready for review Issues that are ready to be considered for merging label Feb 10, 2025
@lukewarlow
Copy link

One benefit to this change is it's one less usage of a Trusted Types sink that will need handling if HTMX ever wants to support running in Trusted Types enforced environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready for review Issues that are ready to be considered for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants