Skip to content

Commit

Permalink
JWT Userstore: check for key validity
Browse files Browse the repository at this point in the history
  • Loading branch information
WietseWind committed Feb 29, 2024
1 parent 0794ad0 commit 8fd17fb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/JwtUserdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {throwIfError} from './utils'

const log = Debug('xumm-sdk:xapp:userdata')

const validateKey = (key: string) => {
if(!(typeof key === 'string' && key.match(/^[a-z0-9]{3,}$/))) {
throw new Error('Invalid key, only a-z0-9 (min three chars) allowed: ' + key)
}
}

export class JwtUserdata {
private Meta: Meta

Expand All @@ -31,6 +37,9 @@ export class JwtUserdata {

public async get (key: string | string[]): Promise<AnyJson> {
const keys = Array.isArray(key) ? key.join(',') : key

keys.split(',').forEach(k => validateKey(k))

const call = await this.Meta.call<JwtUserdataGet>('userdata/' + keys, 'GET')

throwIfError(call)
Expand All @@ -39,6 +48,7 @@ export class JwtUserdata {
}

public async delete (key: string): Promise<boolean> {
validateKey(key)
const call = await this.Meta.call<JwtUserdataDelete>('userdata/' + key, 'DELETE')

throwIfError(call)
Expand All @@ -47,6 +57,7 @@ export class JwtUserdata {
}

public async set (key: string, data: AnyJson): Promise<boolean> {
validateKey(key)
const call = await this.Meta.call<JwtUserdataSet>('userdata/' + key, 'POST', data)

throwIfError(call)
Expand Down

0 comments on commit 8fd17fb

Please sign in to comment.