Skip to content

Commit

Permalink
Add invalid rune checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lnbc1QWFyb24 committed Aug 10, 2022
1 parent b4ca48f commit fd010b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Base64Binary } from './base64'
import { binaryHashToHex, operatorToDescription, runeOperatorRegex } from './utils'
import { binaryHashToHex, invalidAscii, operatorToDescription, runeOperatorRegex } from './utils'
import type { DecodedRune } from './types'

export * from './types'

export const decode = (rune: string) => {
export const decode = (rune: string): DecodedRune | null => {
const runeBinary = Base64Binary.decode(rune)
const hashBinary = runeBinary.slice(0, 32)
const hash = binaryHashToHex(hashBinary)
Expand All @@ -13,12 +14,15 @@ export const decode = (rune: string) => {

const id = uniqueId.split('=')[1]

// invalid rune checks
if (!id) return null
if (restrictionStrings.some(invalidAscii)) return null

const restrictions = restrictionStrings.map((restriction) => {
const alternatives = restriction.split('|')

const summary = alternatives.reduce((str, alternative) => {
const [operator] = alternative.match(runeOperatorRegex) || []

if (!operator) return str

const [name, value] = alternative.split(operator)
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ export const operatorToDescription = (operator: string): string => {
return ''
}
}

export const invalidAscii = (str: string): boolean =>
!![...str].some((char) => char.charCodeAt(0) > 127)

0 comments on commit fd010b6

Please sign in to comment.