|
| 1 | +import * as SentryCore from '@sentry/core'; |
| 2 | +import { vi } from 'vitest'; |
| 3 | + |
| 4 | +import { getTracingMetaTags, isValidBaggageString } from '../../src/server/meta'; |
| 5 | + |
| 6 | +const mockedSpan = { |
| 7 | + toTraceparent: () => '12345678901234567890123456789012-1234567890123456-1', |
| 8 | + transaction: { |
| 9 | + getDynamicSamplingContext: () => ({ |
| 10 | + environment: 'production', |
| 11 | + }), |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +const mockedHub = { |
| 16 | + getScope: () => ({ |
| 17 | + getPropagationContext: () => ({ |
| 18 | + traceId: '123', |
| 19 | + }), |
| 20 | + }), |
| 21 | + getClient: () => ({}), |
| 22 | +}; |
| 23 | + |
| 24 | +describe('getTracingMetaTags', () => { |
| 25 | + it('returns the tracing tags from the span, if it is provided', () => { |
| 26 | + { |
| 27 | + // @ts-expect-error - only passing a partial span object |
| 28 | + const tags = getTracingMetaTags(mockedSpan, mockedHub); |
| 29 | + |
| 30 | + expect(tags).toEqual({ |
| 31 | + sentryTrace: '<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>', |
| 32 | + baggage: '<meta name="baggage" content="sentry-environment=production"/>', |
| 33 | + }); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns propagationContext DSC data if no span is available', () => { |
| 38 | + const tags = getTracingMetaTags(undefined, { |
| 39 | + ...mockedHub, |
| 40 | + // @ts-expect-error - only passing a partial scope object |
| 41 | + getScope: () => ({ |
| 42 | + getPropagationContext: () => ({ |
| 43 | + traceId: '12345678901234567890123456789012', |
| 44 | + sampled: true, |
| 45 | + spanId: '1234567890123456', |
| 46 | + dsc: { |
| 47 | + environment: 'staging', |
| 48 | + public_key: 'key', |
| 49 | + trace_id: '12345678901234567890123456789012', |
| 50 | + }, |
| 51 | + }), |
| 52 | + }), |
| 53 | + }); |
| 54 | + |
| 55 | + expect(tags).toEqual({ |
| 56 | + sentryTrace: expect.stringMatching( |
| 57 | + /<meta name="sentry-trace" content="12345678901234567890123456789012-(.{16})-1"\/>/, |
| 58 | + ), |
| 59 | + baggage: |
| 60 | + '<meta name="baggage" content="sentry-environment=staging,sentry-public_key=key,sentry-trace_id=12345678901234567890123456789012"/>', |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns only the `sentry-trace` tag if no DSC is available', () => { |
| 65 | + vi.spyOn(SentryCore, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({ |
| 66 | + trace_id: '', |
| 67 | + public_key: undefined, |
| 68 | + }); |
| 69 | + |
| 70 | + const tags = getTracingMetaTags( |
| 71 | + // @ts-expect-error - only passing a partial span object |
| 72 | + { |
| 73 | + toTraceparent: () => '12345678901234567890123456789012-1234567890123456-1', |
| 74 | + transaction: undefined, |
| 75 | + }, |
| 76 | + mockedHub, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(tags).toEqual({ |
| 80 | + sentryTrace: '<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>', |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns only the `sentry-trace` tag if no DSC is available', () => { |
| 85 | + vi.spyOn(SentryCore, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({ |
| 86 | + trace_id: '', |
| 87 | + public_key: undefined, |
| 88 | + }); |
| 89 | + |
| 90 | + const tags = getTracingMetaTags( |
| 91 | + // @ts-expect-error - only passing a partial span object |
| 92 | + { |
| 93 | + toTraceparent: () => '12345678901234567890123456789012-1234567890123456-1', |
| 94 | + transaction: undefined, |
| 95 | + }, |
| 96 | + { |
| 97 | + ...mockedHub, |
| 98 | + getClient: () => undefined, |
| 99 | + }, |
| 100 | + ); |
| 101 | + |
| 102 | + expect(tags).toEqual({ |
| 103 | + sentryTrace: '<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1"/>', |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('isValidBaggageString', () => { |
| 109 | + it.each([ |
| 110 | + 'sentry-environment=production', |
| 111 | + 'sentry-environment=staging,sentry-public_key=key,sentry-trace_id=abc', |
| 112 | + // @ is allowed in values |
| 113 | + |
| 114 | + // spaces are allowed around the delimiters |
| 115 | + 'sentry-environment=staging , sentry-public_key=key ,[email protected]', |
| 116 | + 'sentry-environment=staging , thirdparty=value ,[email protected]', |
| 117 | + // these characters are explicitly allowed for keys in the baggage spec: |
| 118 | + "!#$%&'*+-.^_`|~1234567890abcxyzABCXYZ=true", |
| 119 | + // special characters in values are fine (except for ",;\ - see other test) |
| 120 | + 'key=(value)', |
| 121 | + 'key=[{(value)}]', |
| 122 | + 'key=some$value', |
| 123 | + 'key=more#value', |
| 124 | + 'key=max&value', |
| 125 | + 'key=max:value', |
| 126 | + 'key=x=value', |
| 127 | + ])('returns true if the baggage string is valid (%s)', baggageString => { |
| 128 | + expect(isValidBaggageString(baggageString)).toBe(true); |
| 129 | + }); |
| 130 | + |
| 131 | + it.each([ |
| 132 | + // baggage spec doesn't permit leading spaces |
| 133 | + ' sentry-environment=production,sentry-publickey=key,sentry-trace_id=abc', |
| 134 | + // no spaces in keys or values |
| 135 | + 'sentry-public key=key', |
| 136 | + 'sentry-publickey=my key', |
| 137 | + // no delimiters ("(),/:;<=>?@[\]{}") in keys |
| 138 | + 'asdf(x=value', |
| 139 | + 'asdf)x=value', |
| 140 | + 'asdf,x=value', |
| 141 | + 'asdf/x=value', |
| 142 | + 'asdf:x=value', |
| 143 | + 'asdf;x=value', |
| 144 | + 'asdf<x=value', |
| 145 | + 'asdf>x=value', |
| 146 | + 'asdf?x=value', |
| 147 | + 'asdf@x=value', |
| 148 | + 'asdf[x=value', |
| 149 | + 'asdf]x=value', |
| 150 | + 'asdf\\x=value', |
| 151 | + 'asdf{x=value', |
| 152 | + 'asdf}x=value', |
| 153 | + // no ,;\" in values |
| 154 | + 'key=va,lue', |
| 155 | + 'key=va;lue', |
| 156 | + 'key=va\\lue', |
| 157 | + 'key=va"lue"', |
| 158 | + // baggage headers can have properties but we currently don't support them |
| 159 | + 'sentry-environment=production;prop1=foo;prop2=bar,nextkey=value', |
| 160 | + // no fishy stuff |
| 161 | + 'absolutely not a valid baggage string', |
| 162 | + 'val"/><script>alert("xss")</script>', |
| 163 | + 'something"/>', |
| 164 | + '<script>alert("xss")</script>', |
| 165 | + '/>', |
| 166 | + '" onblur="alert("xss")', |
| 167 | + ])('returns false if the baggage string is invalid (%s)', baggageString => { |
| 168 | + expect(isValidBaggageString(baggageString)).toBe(false); |
| 169 | + }); |
| 170 | + |
| 171 | + it('returns false if the baggage string is empty', () => { |
| 172 | + expect(isValidBaggageString('')).toBe(false); |
| 173 | + }); |
| 174 | + |
| 175 | + it('returns false if the baggage string is empty', () => { |
| 176 | + expect(isValidBaggageString(undefined)).toBe(false); |
| 177 | + }); |
| 178 | +}); |
0 commit comments