import { MatchOptions, WordSequenceMatch, DictionaryMatch, MatcherBaseClass, L33tMatch, MatchExtended, } from '../../types' import { WORD_SEQUENCE_SEPARATOR_CHARS } from '../../data/const' // a gap of only separators. '' (plain concatenation) counts too, since there // is no character to reject const isSeparatorRun = (text: string) => { for (const char of text) { if (!WORD_SEQUENCE_SEPARATOR_CHARS.has(char)) { return false } } return true } /* *------------------------------------------------------------------------------- * word sequences (oneTwoThree, fourFiveSix) ------------------------------ *------------------------------------------------------------------------------- */ class MatchWordSequence extends MatcherBaseClass { match(matchOptions: MatchOptions): WordSequenceMatch[] { const { password, matches = [] } = matchOptions const dictionaryMatches = matches.filter( (match): match is DictionaryMatch | L33tMatch => match.pattern === 'dictionary' && this.options.isWordSequence(match.dictionaryName), ) const filteredDictionaryMatches = this.filterDictionaryMatches(dictionaryMatches) // Find sequences of consecutive words. These are added as additional // candidates alongside the dictionary/l33t matches they're built from - // Scoring's DP picks whichever is cheaper, so the component matches must // stay in `matches` rather than being removed here. return this.findWordSequences(filteredDictionaryMatches, password) } private filterDictionaryMatches(matches: (L33tMatch | DictionaryMatch)[]) { // sort by start index, then by end index return ( matches .sort((a, b) => { if (a.i !== b.i) return a.i - b.i if (a.j !== b.j) return a.j - b.j // prefer forward over reversed if (a.reversed !== b.reversed) return a.reversed ? 1 : -1 // prefer non-l33t if (a.l33t !== b.l33t) return a.l33t ? 1 : -1 // prefer better rank return a.rank - b.rank }) // Keep only non-overlapping matches, favoring earlier ones .reduce((acc, match) => { const last = acc[acc.length - 1] if (!last || match.i > last.j) { acc.push(match) } return acc }, []) ) } private findWordSequences( wordMatches: MatchExtended[], password: string, ): WordSequenceMatch[] { const sequences: WordSequenceMatch[] = [] if (wordMatches.length === 0) { return sequences } // Find all possible sequences for (let startIdx = 0; startIdx < wordMatches.length; startIdx += 1) { const sequencesFromStart = this.findSequencesFromStart( wordMatches, startIdx, password, ) sequences.push(...sequencesFromStart) } return sequences } private findSequencesFromStart( sortedMatches: MatchExtended[], startIdx: number, password: string, ): WordSequenceMatch[] { const sequences: WordSequenceMatch[] = [] const startMatch = sortedMatches[startIdx] // Start with single word sequence const currentSequence = [startMatch] // Sequences starting later are covered by the caller's loop over startIdx for ( let nextIdx = startIdx + 1; nextIdx < sortedMatches.length; nextIdx += 1 ) { const nextMatch = sortedMatches[nextIdx] // sortedMatches always comes from filterDictionaryMatches, which already // guarantees each entry starts after the previous one ends, so a // non-extending match is always a gap, never an overlap to skip. if (!this.isValidWordSequence(currentSequence, nextMatch, password)) { break } // A separator run can make the longer sequence more expensive than // stopping here and matching the run separately (e.g. as a repeat), so // keep the sequence so far as a candidate before extending over it. const lastWord = currentSequence[currentSequence.length - 1] if (currentSequence.length > 1 && nextMatch.i - lastWord.j > 2) { sequences.push(this.createWordSequenceMatch(currentSequence, password)) } currentSequence.push(nextMatch) } if (currentSequence.length > 1) { sequences.push(this.createWordSequenceMatch(currentSequence, password)) } return sequences } private isValidWordSequence( currentSequence: MatchExtended[], nextMatch: MatchExtended, password: string, ): boolean { // Get the text between the last word and the next word const lastWord = currentSequence[currentSequence.length - 1] const textBetween = password.slice(lastWord.j + 1, nextMatch.i) // For snake_case, kebab-case, simple concatenation or a run of separators // like '..'. Longer runs are not free: scoring charges for them. const hasValidSeparator = isSeparatorRun(textBetween) // For camelCase, we need to check if the next word starts with uppercase // and there's no separator (or just a single character that could be uppercase) const isCamelCase = textBetween.length === 1 && textBetween === textBetween.toUpperCase() && textBetween !== textBetween.toLowerCase() return hasValidSeparator || isCamelCase } private createWordSequenceMatch( sequence: MatchExtended[], password: string, ): WordSequenceMatch { const firstMatch = sequence[0] const lastMatch = sequence[sequence.length - 1] const words = sequence.map((match) => match.matchedWord) // one entry per gap, so scoring can tell which words are separated const separators = sequence .slice(1) .map((match, index) => password.slice(sequence[index].j + 1, match.i)) // Determine if sequence is ascending (by rank) const ranks = sequence.map((match) => match.rank) const ascending = ranks.every((rank, i) => i === 0 || rank >= ranks[i - 1]) // Use the most common dictionary name, or the first one const dictionaryNames = sequence.map((match) => match.dictionaryName) const dictionaryName = this.getMostCommon(dictionaryNames) || firstMatch.dictionaryName return { pattern: 'wordSequence', i: firstMatch.i, j: lastMatch.j, token: password.slice(firstMatch.i, lastMatch.j + 1), words, wordCount: words.length, dictionaryName, ascending, separators, } } private getMostCommon(array: T[]): T | null { if (array.length === 0) return null const counts = new Map() let maxCount = 0 let mostCommon: T | null = null array.forEach((item) => { const count = (counts.get(item) || 0) + 1 counts.set(item, count) if (count > maxCount) { maxCount = count mostCommon = item } }) return mostCommon } } export default MatchWordSequence