Skip to content

Commit

Permalink
fix: break up greedy host fragment parsing regex (#274)
Browse files Browse the repository at this point in the history
It's easier to reason about each step this way, and also not susceptible
to redos.
  • Loading branch information
wraithgar authored Nov 20, 2024
1 parent 9b07068 commit e47b7e4
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

const maybeJoin = (...args) => args.every(arg => arg) ? args.join('') : ''
const maybeEncode = (arg) => arg ? encodeURIComponent(arg) : ''
const formatHashFragment = (f) => f.toLowerCase().replace(/^\W+|\/|\W+$/g, '').replace(/\W+/g, '-')
const formatHashFragment = (f) => f.toLowerCase()
.replace(/^\W+/g, '') // strip leading non-characters
.replace(/\W+$/g, '') // strip trailing non-characters
.replace(/\//g, '') // strip all slashes
.replace(/\W+/g, '-') // replace remaining non-characters with '-'

const defaults = {
sshtemplate: ({ domain, user, project, committish }) =>
Expand Down

0 comments on commit e47b7e4

Please sign in to comment.