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.

10.4 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest'

describe('Blockchain Security Tests', () => {
  describe('Environment Variable Security', () => {
    it('should not expose sensitive keys in client code', () => {
      // Test that sensitive environment variables are properly handled
      const mockEnvVars = {
        VITE_HELIUS_KEY: 'test-key',
        VITE_OPENSEA_KEY: 'test-opensea-key',
        VITE_PINATA_API_KEY: 'test-pinata-key',
        VITE_PINATA_API_SECRET: 'test-pinata-secret'
      }

      // Simulate checking that keys exist but aren't exposed
      Object.keys(mockEnvVars).forEach(key => {
        expect(key.startsWith('VITE_')).toBe(true) // Only VITE_ vars should be accessible
      })
    })

    it('should validate RPC URL construction', () => {
      const mockKey = 'test-api-key'
      const constructRpcUrl = (key) => {
        if (!key || typeof key !== 'string' || key.length < 8) {
          throw new Error('Invalid API key')
        }
        return `https://devnet.helius-rpc.com/?api-key=${key}`
      }

      expect(constructRpcUrl(mockKey)).toBe('https://devnet.helius-rpc.com/?api-key=test-api-key')
      expect(() => constructRpcUrl('')).toThrow('Invalid API key')
      expect(() => constructRpcUrl('short')).toThrow('Invalid API key')
    })
  })

  describe('Wallet Address Validation', () => {
    it('should validate Ethereum addresses properly', () => {
      const isValidEthereumAddress = (address) => {
        if (!address) return false
        return /^0x[a-fA-F0-9]{40}$/.test(address)
      }

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

    it('should validate Solana addresses properly', () => {
      const isValidSolanaAddress = (address) => {
        if (!address) return false
        return /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address)
      }

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

    it('should sanitize wallet addresses for display', () => {
      const sanitizeAddress = (address) => {
        if (!address) return ''
        const cleanAddress = address.replace(/[^a-zA-Z0-9]/g, '')
        if (cleanAddress.length < 8) return ''
        return cleanAddress
      }

      expect(sanitizeAddress('0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac')).toBe('0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac')
      expect(sanitizeAddress('malicious<script>alert(1)</script>address')).toBe('maliciousscriptalert1scriptaddress')
      expect(sanitizeAddress('')).toBe('')
    })
  })

  describe('Transaction Security', () => {
    it('should validate transaction parameters', () => {
      const validateTransactionParams = (params) => {
        const { to, value, data } = params
        
        // Validate recipient address
        if (!to || !/^0x[a-fA-F0-9]{40}$/.test(to)) {
          throw new Error('Invalid recipient address')
        }

        // Validate value is not negative or excessive
        const valueNum = parseFloat(value)
        if (isNaN(valueNum) || valueNum < 0 || valueNum > 1000) {
          throw new Error('Invalid transaction value')
        }

        // Validate data is hex if provided
        if (data && !/^0x[a-fA-F0-9]*$/.test(data)) {
          throw new Error('Invalid transaction data')
        }

        return true
      }

      expect(validateTransactionParams({
        to: '0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac',
        value: '0.1',
        data: '0x'
      })).toBe(true)

      expect(() => validateTransactionParams({
        to: 'invalid-address',
        value: '0.1'
      })).toThrow('Invalid recipient address')

      expect(() => validateTransactionParams({
        to: '0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac',
        value: '-1'
      })).toThrow('Invalid transaction value')

      expect(() => validateTransactionParams({
        to: '0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac',
        value: '0.1',
        data: 'not-hex'
      })).toThrow('Invalid transaction data')
    })

    it('should implement rate limiting for transactions', () => {
      class TransactionRateLimiter {
        constructor() {
          this.transactions = new Map()
          this.maxPerHour = 10
          this.windowMs = 60 * 60 * 1000 // 1 hour
        }

        canTransact(address) {
          const now = Date.now()
          const windowStart = now - this.windowMs

          if (!this.transactions.has(address)) {
            this.transactions.set(address, [])
          }

          const userTransactions = this.transactions.get(address)
          const recentTransactions = userTransactions.filter(time => time > windowStart)

          if (recentTransactions.length >= this.maxPerHour) {
            return false
          }

          recentTransactions.push(now)
          this.transactions.set(address, recentTransactions)
          return true
        }
      }

      const limiter = new TransactionRateLimiter()
      const address = '0x742d35Cc6634C0532925a3b8D7FB85F30D7Fdcac'

      // Should allow initial transactions
      for (let i = 0; i < 10; i++) {
        expect(limiter.canTransact(address)).toBe(true)
      }

      // Should block after rate limit
      expect(limiter.canTransact(address)).toBe(false)
    })
  })

  describe('API Security', () => {
    it('should validate API responses', () => {
      const validateSolanaResponse = (response) => {
        if (!response || typeof response !== 'object') {
          throw new Error('Invalid response format')
        }

        if (!response.result || !Array.isArray(response.result.items)) {
          throw new Error('Invalid response structure')
        }

        // Validate each item has required fields
        response.result.items.forEach((item, index) => {
          if (!item.id || typeof item.id !== 'string') {
            throw new Error(`Invalid item ID at index ${index}`)
          }
        })

        return true
      }

      const validResponse = {
        jsonrpc: "2.0",
        result: {
          items: [
            { id: "test-id-1", content: { metadata: { name: "Test NFT" } } },
            { id: "test-id-2", content: { metadata: { name: "Test NFT 2" } } }
          ]
        }
      }

      expect(validateSolanaResponse(validResponse)).toBe(true)

      expect(() => validateSolanaResponse(null)).toThrow('Invalid response format')
      expect(() => validateSolanaResponse({ result: null })).toThrow('Invalid response structure')
      expect(() => validateSolanaResponse({ 
        result: { items: [{ content: {} }] } 
      })).toThrow('Invalid item ID at index 0')
    })

    it('should sanitize metadata inputs', () => {
      const sanitizeMetadata = (metadata) => {
        if (!metadata || typeof metadata !== 'object') {
          return {}
        }

        const sanitized = {}
        const allowedFields = ['name', 'description', 'image', 'attributes']

        allowedFields.forEach(field => {
          if (metadata[field]) {
            if (typeof metadata[field] === 'string') {
              // Remove HTML tags iteratively to prevent incomplete sanitization
              let clean = metadata[field]
              let previous
              do {
                previous = clean
                clean = clean.replace(/<[^>]*>/g, '')
              } while (clean !== previous)
              
              sanitized[field] = clean.substring(0, 1000)
            } else if (field === 'attributes' && Array.isArray(metadata[field])) {
              sanitized[field] = metadata[field].slice(0, 20) // Limit attributes
            }
          }
        })

        return sanitized
      }

      const maliciousMetadata = {
        name: '<script>alert("xss")</script>Test NFT',
        description: 'Valid description',
        maliciousField: 'This should be removed',
        attributes: new Array(100).fill({ trait_type: 'test' }) // Too many attributes
      }

      const sanitized = sanitizeMetadata(maliciousMetadata)
      expect(sanitized.name).toBe('alert("xss")Test NFT') // Script tags removed by iterative sanitization
      expect(sanitized.description).toBe('Valid description')
      expect(sanitized.maliciousField).toBeUndefined()
      expect(sanitized.attributes).toHaveLength(20)
    })
  })

  describe('Smart Contract Interaction Security', () => {
    it('should validate contract addresses before interaction', () => {
      const isValidContractAddress = (address) => {
        // Basic Ethereum address validation
        if (!address || !/^0x[a-fA-F0-9]{40}$/.test(address)) {
          return false
        }

        // Additional checks for known malicious addresses could be added here
        const knownMaliciousAddresses = [
          '0x0000000000000000000000000000000000000000', // Zero address
          '0x000000000000000000000000000000000000dead'  // Dead address
        ]

        return !knownMaliciousAddresses.includes(address.toLowerCase())
      }

      expect(isValidContractAddress('0xFF9C1b15B16263C61d017ee9F65C50e4AE0113D7')).toBe(true)
      expect(isValidContractAddress('0x0000000000000000000000000000000000000000')).toBe(false)
      expect(isValidContractAddress('invalid')).toBe(false)
    })

    it('should validate function calls and parameters', () => {
      const validateContractCall = (functionName, params) => {
        const allowedFunctions = ['tokenURI', 'balanceOf', 'ownerOf', 'approve']
        
        if (!allowedFunctions.includes(functionName)) {
          throw new Error(`Function ${functionName} not allowed`)
        }

        if (functionName === 'tokenURI' && (!params.tokenId || isNaN(params.tokenId))) {
          throw new Error('Invalid tokenId for tokenURI')
        }

        return true
      }

      expect(validateContractCall('tokenURI', { tokenId: 123 })).toBe(true)
      expect(() => validateContractCall('transferFrom', {})).toThrow('Function transferFrom not allowed')
      expect(() => validateContractCall('tokenURI', { tokenId: 'invalid' })).toThrow('Invalid tokenId for tokenURI')
    })
  })
})