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

ensure color is string before calling toLowerCase() #10129

Merged
merged 2 commits into from
May 20, 2024

Conversation

chris48s
Copy link
Member

@chris48s chris48s commented Apr 29, 2024

Fixes #10128

I don't have a reliable repro for this bug, but somewhere along the line we are allowing a hex colour which is all numeric to not be a string.

I suggest hitting with a hammer until it plays nicely 🔨

(I'm aware that somewhere a typescript enthusiast is having an aneurysm)

@chris48s chris48s added bug Bugs in badges and the frontend npm-package Badge generation and badge templates labels Apr 29, 2024
Copy link
Contributor

github-actions bot commented Apr 29, 2024

Messages
📖 ✨ Thanks for your contribution to Shields, @chris48s!

Generated by 🚫 dangerJS against 6b74016

@@ -49,7 +49,7 @@ function normalizeColor(color) {
} else if (color in aliases) {
return aliases[color]
} else if (isHexColor(color)) {
return `#${color.toLowerCase()}`
return `#${color.toString().toLowerCase()}`
Copy link
Contributor

@LitoMore LitoMore May 4, 2024

Choose a reason for hiding this comment

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

How about moving the toString() to isHexColor()? Like this below:

function isHexColor(s = '') {
  return hexColorRegex.test(String(s))
}

The String(s) is safer here. Even if the value is null, it will not throw an error.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that would really help tbh.

The issue here is that the current implementation of isHexColor() may already return true on a non-string input (I guess .test() implicitly casts to string), leading us to try and call toLowerCase() on an int. For example:

> isHexColor(111111)
true

so even if we make the cast explicit, we'd still need to call toString() in this block, or do it somewhere before we reach here.

Null or undefined already return false with the current implementation:

> isHexColor(null)
false
> isHexColor(undefined)
false

Copy link
Member

@PyvesB PyvesB left a comment

Choose a reason for hiding this comment

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

Sounds reasonable to me 👍

@chris48s chris48s enabled auto-merge May 11, 2024 19:23
@chris48s chris48s added this pull request to the merge queue May 20, 2024
Merged via the queue into badges:master with commit 1380017 May 20, 2024
23 checks passed
@chris48s chris48s deleted the 10128-tostring branch May 20, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs in badges and the frontend npm-package Badge generation and badge templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: color.toLowerCase is not a function
3 participants