MAJOR=4
MINOR=53
DEVNAME=tty53
  F   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þYp1¬ã�f1�­„?üÿÉ„/6È_�uÐb&=LyZËcõÈÇ 6    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe HttpLog, :aggregate_failures do
  subject(:http_log) { build(:http_log) }

  describe 'callbacks' do
    # See https://github.com/zammad/zammad/issues/2100
    it 'converts request/response message data to UTF-8 before saving' do
      http_log.request[:content]  = 'foo'.dup.force_encoding('ascii-8bit')
      http_log.response[:content] = 'bar'.dup.force_encoding('ascii-8bit')

      expect { http_log.save }
        .to change { http_log.request[:content].encoding.name }.from('ASCII-8BIT').to('UTF-8')
        .and change { http_log.response[:content].encoding.name }.from('ASCII-8BIT').to('UTF-8')
    end

    context 'when sensitive data is present' do
      subject(:http_log) { create(:http_log, request: { headers: "Authorization: Bearer supersecrettoken\nCookie: session_id=12345; user_id=67890", url: 'https://example.com/api?access_token=query_secret_token&other=param' }, response: { body: 'access_token="anothersecret" api_key: "key-value" secret = "my_secret"' }) }

      it 'masks sensitive data in request/response before saving', :aggregate_failures do
        expect(http_log.request['headers']).to eq("Authorization: Bearer [FILTERED]\nCookie: session_id=[FILTERED]; user_id=[FILTERED]")
        expect(http_log.request['url']).to eq('https://example.com/api?access_token=[FILTERED]&other=param')
        expect(http_log.response['body']).to eq('access_token="[FILTERED]" api_key: "[FILTERED]" secret = "[FILTERED]"')
      end
    end

    context 'when long data is present' do
      subject(:http_log) do
        create(:http_log, request: { content: "{\"images\":[\"iVBORw0KGgoAAAANSUhEUgAABEkAAAECCAYAAAACQolFAAABY2lDQ1BrQ0dDb2xvclNwYWNlRGlzcGxheVAzAAAok\nX2QsUvDUBDGv1aloHUQHRwcMolDlJIKuji0FURxCFXB6pS+pqmQxkeSIgU3/4GC/4EKzm4Whzo6OAiik+jm5KTgouV5L4mkInqP435877vjOCA5bn\nBu9wOoO75bXMorm6UtJfWMBL0gDObxnK6vSv6uP+P9PvTeTstZv///jcGK6TGqn5QZxl0fSKjE+p7PJe8Tj7m0FHFLshXyieRyyOeBZ71YIL4mVlj\nNqBC/EKvlHt3q4brdYNEOcvu06WysyTmUE1jEDjxw2DDQhAId2T/8s4G/gF1yN\nFSn4UafOrJkSI\"]}" }, response: { body: "data:image/png;base64,iVBORw0KGgoAAAAAACWZVhJZk1NACoAAAAIAAUBEgADAAAAAQABAAABGgAFAAAAAQAAAEoBGwAFAAAAAQAAAFIBKAADAAAAAQACAACHaQAEAAAAAQ
AAAFoAAAAAAAAAkAAAAAEAAACQAAAAAQADk=" })
      end

      it 'truncated long data in request/response before saving', :aggregate_failures do
        expect(http_log.request['content']).to eq('{"images":["iVBORw0KGgoAAAANSUhEUgAABEkAA...[TRUNCATED]"]}')
        expect(http_log.response['body']).to eq('data:image/png;base64,iVBORw0...[TRUNCATED]')
      end
    end
  end

  describe '.mask_sensitive_data' do
    let(:input) { 'Authorization: Basic dXNlcjpwYXNzd29yZA==' }

    context 'when the input includes authorization basic part' do
      it 'masks Basic authorization headers' do
        expect(described_class.mask_sensitive_data(input)).to eq('Authorization: Basic [FILTERED]')
      end
    end

    context 'when input has no sensitive parts' do
      it 'returns the input unchanged' do
        plain = 'no secrets here'
        expect(described_class.mask_sensitive_data(plain)).to eq('no secrets here')
      end
    end

    context 'when the input includes authorization bearer part' do
      let(:dot_separated_bearer_token) do
        %w[
          eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9
          eyJzdWIiOiJ1c2VyQGV4YW1wbGUuY29tIiwicm9sZSI6ImFwcCJ9
          c2lnbmF0dXJl
        ].join('.')
      end

      it 'masks Bearer authorization headers' do
        bearer = 'Authorization: Bearer supersecrettoken'
        expect(described_class.mask_sensitive_data(bearer)).to eq('Authorization: Bearer [FILTERED]')
      end

      it 'masks dot-separated Bearer authorization headers completely' do
        bearer = "authorization: Bearer #{dot_separated_bearer_token}"

        expect(described_class.mask_sensitive_data(bearer)).to eq('Authorization: Bearer [FILTERED]')
      end

    