Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

5.2 KB
Raw Blame
import { describe, it, expect } from 'vitest'

describe('Security Tests', () => {
  describe('Input validation patterns', () => {
    it('should validate URL inputs', () => {
      const isValidURL = (url) => {
        try {
          const parsed = new URL(url)
          // Only allow http and https protocols
          return ['http:', 'https:'].includes(parsed.protocol)
        } catch {
          return false
        }
      }

      expect(isValidURL('https://example.com')).toBe(true)
      expect(isValidURL('http://localhost:3000')).toBe(true)
      expect(isValidURL('invalid-url')).toBe(false)
      expect(isValidURL('javascript:alert(1)')).toBe(false)
    })

    it('should sanitize file names', () => {
      const sanitizeFileName = (filename) => {
        return filename.replace(/[^a-zA-Z0-9._-]/g, '_')
      }

      expect(sanitizeFileName('avatar.vrm')).toBe('avatar.vrm')
      expect(sanitizeFileName('../../malicious.vrm')).toBe('.._.._malicious.vrm')
      expect(sanitizeFileName('<script>alert(1)</script>.vrm')).toBe('_script_alert_1___script_.vrm')
    })

    it('should validate file types', () => {
      const allowedTypes = ['.vrm', '.glb', '.gltf', '.fbx', '.png', '.jpg', '.jpeg']
      
      const isAllowedFileType = (filename) => {
        const ext = filename.toLowerCase().substring(filename.lastIndexOf('.'))
        return allowedTypes.includes(ext)
      }

      expect(isAllowedFileType('avatar.vrm')).toBe(true)
      expect(isAllowedFileType('texture.png')).toBe(true)
      expect(isAllowedFileType('malicious.exe')).toBe(false)
      expect(isAllowedFileType('script.js')).toBe(false)
    })
  })

  describe('Blockchain security patterns', () => {
    it('should validate wallet addresses', () => {
      const isValidEthereumAddress = (address) => {
        return /^0x[a-fA-F0-9]{40}$/.test(address)
      }

      const isValidSolanaAddress = (address) => {
        return /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address)
      }

      expect(isValidEthereumAddress('0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac')).toBe(true)
      expect(isValidEthereumAddress('invalid-address')).toBe(false)
      expect(isValidEthereumAddress('0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdca')).toBe(false) // too short

      expect(isValidSolanaAddress('11111111111111111111111111111112')).toBe(true)
      expect(isValidSolanaAddress('invalid-solana-address')).toBe(false)
    })

    it('should validate transaction amounts', () => {
      const isValidAmount = (amount) => {
        const num = parseFloat(amount)
        return !isNaN(num) && num > 0 && num < 1e18 // reasonable upper bound
      }

      expect(isValidAmount('0.1')).toBe(true)
      expect(isValidAmount('1000')).toBe(true)
      expect(isValidAmount('0')).toBe(false)
      expect(isValidAmount('-1')).toBe(false)
      expect(isValidAmount('invalid')).toBe(false)
      expect(isValidAmount('1e20')).toBe(false) // too large
    })
  })

  describe('XSS and injection prevention', () => {
    it('should escape HTML in user inputs', () => {
      const escapeHtml = (text) => {
        const div = document.createElement('div')
        div.textContent = text
        return div.innerHTML
      }

      expect(escapeHtml('<script>alert(1)</script>')).toBe('&lt;script&gt;alert(1)&lt;/script&gt;')
      expect(escapeHtml('Normal text')).toBe('Normal text')
      expect(escapeHtml('<img src=x onerror=alert(1)>')).toBe('&lt;img src=x onerror=alert(1)&gt;')
    })

    it('should validate JSON inputs', () => {
      const safeParseJSON = (text) => {
        try {
          const parsed = JSON.parse(text)
          // Additional validation can be added here
          return { success: true, data: parsed }
        } catch (error) {
          return { success: false, error: error.message }
        }
      }

      expect(safeParseJSON('{"valid": "json"}')).toEqual({
        success: true,
        data: { valid: 'json' }
      })

      expect(safeParseJSON('invalid json')).toEqual({
        success: false,
        error: expect.any(String)
      })
    })
  })

  describe('Rate limiting patterns', () => {
    it('should implement basic rate limiting logic', () => {
      class RateLimiter {
        constructor(maxRequests = 10, windowMs = 60000) {
          this.maxRequests = maxRequests
          this.windowMs = windowMs
          this.requests = new Map()
        }

        isAllowed(identifier) {
          const now = Date.now()
          const windowStart = now - this.windowMs
          
          if (!this.requests.has(identifier)) {
            this.requests.set(identifier, [])
          }

          const userRequests = this.requests.get(identifier)
          const validRequests = userRequests.filter(time => time > windowStart)
          
          if (validRequests.length >= this.maxRequests) {
            return false
          }

          validRequests.push(now)
          this.requests.set(identifier, validRequests)
          return true
        }
      }

      const limiter = new RateLimiter(3, 1000) // 3 requests per second

      expect(limiter.isAllowed('user1')).toBe(true)
      expect(limiter.isAllowed('user1')).toBe(true)
      expect(limiter.isAllowed('user1')).toBe(true)
      expect(limiter.isAllowed('user1')).toBe(false) // Rate limited
    })
  })
})