Closed Bug 1333953 Opened 7 years ago Closed 6 years ago

Theming API - validate colors in the schema

Categories

(WebExtensions :: Themes, defect, P2)

defect

Tracking

(Not tracked)

RESOLVED FIXED
mozilla61

People

(Reporter: mattw, Assigned: ntim, Mentored)

References

Details

(Whiteboard: user-story, triaged, [ntim-intern-project])

Attachments

(1 obsolete file)

The engineering plan mentions supporting a wide range of color values that we'll eventually support. The way we currently handle this is by accepting arbitrary strings and then validating them in the script. However, it would be more efficient and somewhat less prone to bugs if we could catch these errors during schema validation.

Based on the engineering document, at least the following values should be supported:

 - 3-digit and 6-digit hex values
 - rgb and rgba values
 - hsl and hsla values
Right now we don't have any special validation for the colors, but if they're not of the 3- or 4-length array type that Chrome uses, then they will get validated as a CSS color when they're applied. If they aren't a valid CSS color, then no value will get applied for that property. There isn't any explicit error presented to the theme author.

This doesn't seem like something that we should block enabling on Nightly for. I'll remove this from blocking that bug.
No longer blocks: 1341722
Blocks: themingapi
No longer blocks: themingapi-framework
Blocks: themingapi-framework
No longer blocks: themingapi
Priority: -- → P5
mass move of existing themes bugs to new WebExtensions: Themes component
Component: WebExtensions: Frontend → WebExtensions: Themes
P2, because right now we expose internal CSS variables to themes.
Priority: P5 → P2
Assignee: nobody → anejaalisha
Status: NEW → ASSIGNED
Mentor: jaws, ntim.bugs
The schema for themes is located at /toolkit/components/extensions/schemas/theme.json and the code to validate it would be embedded in the schema. We already have some validation that you can see there for the ThemeColor, but we also allow a String as a choice which is what this bug is concerned about.

For string names we should probably only support the color names listed in https://searchfox.org/mozilla-central/source/gfx/src/nsColorNameList.h. Unfortunately I don't see a good way for us to include that list during build time so we may need to duplicate it. It would be nice to have a test that tries all of the values we support, this way the test would fail if a new color name gets introduced. I don't know of an API to get those color names though.

In addition, we should make sure that we support each of these values:

 - 3-digit, 6-digit, and 8-digit (new since this bug was filed) hex values
 - rgb and rgba values
 - hsl and hsla values
I largely understood what needs to be done, but where do you want me to place the duplicated file for the color names?
Flags: needinfo?(jaws)
It's probably not a new file, just a list of colors in a regex pattern.
Yeah, I agree with comment 6. It would just be located inside of theme.json.
Flags: needinfo?(jaws)
Sorry for the back and forth here, but I talked more with Tim and we've decided to simplify this a bit.

Instead of adding one long regex pattern to theme.json, we can allow all string values to pass through the schema, and then use _sanitizeCSSColor to convert color names to their proper RGBA values. We'll want to change the function so it doesn't strip the alpha component, possibly adding a boolean argument that can be passed in to remove the alpha component and updating the one callsite to use that argument.

Then we can use regex for the other types of validation.
Okay, that makes sense. Thanks!
If we are verifying the names of colors in _sanitizeCSSColor, how should I get the color names? Should it be hardcoded in /toolkit/modules/LightweightThemeConsumer.jsm or dynamically load from https://searchfox.org/mozilla-central/source/gfx/src/nsColorNameList.h?
Flags: needinfo?(jaws)
(In reply to anejaalisha from comment #10)
> If we are verifying the names of colors in _sanitizeCSSColor, how should I
> get the color names? Should it be hardcoded in
> /toolkit/modules/LightweightThemeConsumer.jsm or dynamically load from
> https://searchfox.org/mozilla-central/source/gfx/src/nsColorNameList.h?

getComputedStyle should convert the colors to rgba automatically, getting rid of the names.
Attached patch Bug1333953.patch (obsolete) — Splinter Review
I probably just want to make sure if my understanding is correct. In the function call, I have hardcoded 'true' for now to take your feedback. I'll change it later.

Am I on the right track?
Flags: needinfo?(ntim.bugs)
Attachment #8950079 - Flags: feedback?(ntim.bugs)
Attachment #8950079 - Flags: feedback?(jaws)
Comment on attachment 8950079 [details] [diff] [review]
Bug1333953.patch

Review of attachment 8950079 [details] [diff] [review]:
-----------------------------------------------------------------

Yeah, that's the right idea.
Attachment #8950079 - Flags: feedback?(jaws) → feedback+
Flags: needinfo?(jaws)
Flags: needinfo?(ntim.bugs)
Comment on attachment 8950079 [details] [diff] [review]
Bug1333953.patch

Review of attachment 8950079 [details] [diff] [review]:
-----------------------------------------------------------------

You'll also need to use _sanitizeCSSColor in other calls of _setProperty as well, with alpha set to true.

::: toolkit/modules/LightweightThemeConsumer.jsm
@@ +133,5 @@
>      root.style.removeProperty("--lwt-text-color");
>      root.style.removeProperty("--lwt-accent-color");
>      let textcolor = aData.textcolor || "black";
>      _setProperty(root, active, "--lwt-text-color", textcolor);
> +    _setProperty(root, active, "--lwt-accent-color", this._sanitizeCSSColor(aData.accentcolor, true) || "white");

This should probably have the alpha parameter set to false.
Attachment #8950079 - Flags: feedback?(ntim.bugs)
anejaalisha, have you been able to make any progress on this?
Flags: needinfo?(anejaalisha)
I might actually be tackling this in bug 1417883 (the second patch)
Flags: needinfo?(anejaalisha)
Fixed by the refactor in bug 1417883. Bug 1449327 is adding tests for sanitization.
Assignee: anejaalisha → ntim.bugs
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla61
Product: Toolkit → WebExtensions
Attachment #8950079 - Attachment is obsolete: true
Whiteboard: user-story, triaged → user-story, triaged, [ntim-intern-project]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: