Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
A regex that tokenizes CSS.
var cssTokens = require("css-tokens")
// Tokenize a whole string of CSS:
cssString.match(cssTokens)
// [".", "foo", "{", "prop", ":", " ", "foo", ";", "}", "\n", ...]
// Rename the class `foo` to `bar`:
var lastToken
cssString.replace(cssTokens, function(token) {
if (lastToken === "." && token === "foo") {
return "bar"
}
lastToken = token
return token
})
// [".", "bar", "{", "prop", ":", " ", "foo", ";", "}", "\n", ...]
npm install css-tokens
var cssTokens = require("css-tokens")
cssTokens
A regex with the g
flag that matches CSS tokens.
The regex always matches, even invalid CSS and the empty string. For
example, cssTokens.exec(string)
never returns null
.
The next match is always directly after the previous. Each token has its own capturing group.
cssTokens.names
An array of names for each token, in the capturing group order.
Unterminated strings are still matched as strings. CSS strings cannot contain
(unescaped) newlines, so unterminated strings simply end at the end of the
line. You may use /['"]$/.test(matchedStringToken)
to determine if a string
was terminated or not.
Unterminated multi-line comments are also still matched as comments. They simply go on to the end of the string.
Unterminated unquoted urls are also still matched as unquoted urls. They continue as long as there are valid characters.
Invalid ASCII characters have their own capturing group.
Tokenizing CSS using regexes—in fact, one single regex—won’t be perfect. But that’s not the point either.
The only known “limitation” is the following:
url(http://www.w3.org/2000/svg)
url('http://www.w3.org/2000/svg')
The first line is matched as one single token (unquotedUrl), while the second is matched as four (name + punctuation + string + punctuation). This could be fixed, but isn’t to simplify the regex.
index.js is generated by running node generate-index.js
. The regex is written
in regex.coffee. Don’t worry, you don’t need to know anything about
CoffeeScript: regex.coffee should be kept as simple as possible. CoffeeScript
is only used for its block regexes, which have the following benefits:
RegExp("regex as a string. One backslash: \\\\")
).Everything else is written in JavaScript.
Version 0.1.0 (2014-03-09) ###
FAQs
A regex that tokenizes CSS.
The npm package css-tokens receives a total of 35 weekly downloads. As such, css-tokens popularity was classified as not popular.
We found that css-tokens demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.