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

Fix for bug in demo hooks-sanitize-css-demo.html #931

Closed
koosvanderkolk opened this issue Apr 3, 2024 · 3 comments
Closed

Fix for bug in demo hooks-sanitize-css-demo.html #931

koosvanderkolk opened this issue Apr 3, 2024 · 3 comments

Comments

@koosvanderkolk
Copy link

I tried to implement this example:
https://github.com/cure53/DOMPurify/blob/main/demos/hooks-sanitize-css-demo.html

but got my styles removed as there seems to be a small bug in the validateStyles function (see comments in below code)

function validateStyles(output, styles) {
  Object.keys(styles).forEach(prop => {  // prop is the index, not the CSS style property
    const value = styles[prop]; // value is the CSS style property
    if (value && typeof value === 'string') {
      const normalizedProp = prop.replace(/([A-Z])/g, '-$1').toLowerCase();
      if (allowed_properties.includes(normalizedProp) && (allow_css_functions || !/\w+\(/.test(value))) {
        output.push(`${normalizedProp}:${value};`);
      }
    }
  });
}

Below function gave me the correct result:

function validateStyles(output, styles) {
  Object.keys(styles).forEach(function(index) {
    if (styles.hasOwnProperty(index)) {
      let normalizedKey = styles[index].replace(/([A-Z])/g, '-$1').toLowerCase();
      if (allowed_properties.includes(normalizedKey)) {
        let value = styles[normalizedKey];
        output.push(`${normalizedKey}:${value};`);
      }
    }
  });
}
@cure53
Copy link
Owner

cure53 commented Apr 3, 2024

Oh, nice, thanks - wanna spin up a PR? 🙂

cure53 added a commit that referenced this issue Apr 4, 2024
fix: fixed the CSS sanitizer demo hook, thanks @koosvanderkolk
@cure53
Copy link
Owner

cure53 commented Apr 4, 2024

All sorted, thanks again :)

@cure53 cure53 closed this as completed Apr 4, 2024
@koosvanderkolk
Copy link
Author

Great thanks!

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

No branches or pull requests

2 participants