Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@ampproject/purifier
Advanced tools
The AMP Purifier library contains an AMP-specific configuration for DOMPurify.
This library is internally used by amp-mustache
to sanitize rendered Mustache.js templates before displaying them.
Install via:
npm i @ampproject/purifier
import {Purifier} from '@ampproject/purifier';
const purifier = new Purifier(document);
purifier.purifyHtml('a<script>b</script>c'); // "ac"
import Mustache from 'mustache';
import {Purifier} from '@ampproject/purifier';
const purifier = new Purifier(document);
const _unescapedValue = Mustache.Writer.prototype.unescapedValue;
Mustache.Writer.prototype.unescapedValue = function(token, context) {
const result = _unescapedValue(token, context);
return purifier.purifyTagsForTripleMustache(result);
};
const html = Mustache.render(template, data);
const body = purifier.purifyHtml(html);
for (const child of body.children) {
targetElement.appendChild(child);
}
The library has only a single export, the Purifier
class.
Purifier
constructornew Purifier(doc, config, attrRewrite)
doc
The base document to use. Usually window.document
.
config
Optional
DOMPurify configuration to use in addition to the AMP default one.
attrRewrite
Optional
A function that, if provided, will be called for every sanitized attribute in the output to change its value. It accepts the following attributes:
tagName
- name of tag containing the attributeattrName
- name of attributeattrValue
- current attribute valueThe returned value of this function is used as the new attribute value.
For example, this replaces the href
of all <a>
elements with example.com:
new Purifier(window.document, {}, (tagName, attrName, attrValue) => {
if (tagName === 'a' && attrName === 'href') {
return 'https://google.com';
}
return attrValue;
});
purifyHtml
purifyHtml(html)
Uses DOMPurify to sanitize HTML in a way that ensures the fragment is valid AMP.
html
The HTML code to sanitize (purify).
A element containing the sanitized html
markup.
purifyTagsForTripleMustache
purifyTagsForTripleMustache(html)
Uses DOMPurify to sanitize HTML with stricter policy for unescaped templates
e.g. triple mustache. See amp-mustache
documentation
for more information.
html
The HTML code to sanitize (purify).
Sanitized HTML (as a string).
getAllowedTags
getAllowedTags()
Gets a copy of the map of allowed tag names (standard DOMPurify config).
An object containing the list of allowed tags according to AMP's DOMPurify config.
validateAttributeChange
Returns whether an attribute addition/modification/removal is valid according to AMP's DOMPurify config.
validateAttributeChange(node, attr, value)
node
DOM node to check.
attr
Attribute name.
value
Attribute value (can be null).
true
if the given attribute change is valid, false
otherwise.
FAQs
AMP-specific sanitization library
The npm package @ampproject/purifier receives a total of 1 weekly downloads. As such, @ampproject/purifier popularity was classified as not popular.
We found that @ampproject/purifier demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.