test_plugin.py 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  1. import os.path
  2. import zipfile
  3. from base64 import b64encode
  4. from io import BytesIO
  5. from unittest.mock import patch
  6. from uuid import uuid4
  7. import pytest
  8. import responses
  9. from django.utils.encoding import force_bytes
  10. from sentry.models import (
  11. ArtifactBundle,
  12. DebugIdArtifactBundle,
  13. File,
  14. ProjectArtifactBundle,
  15. Release,
  16. ReleaseArtifactBundle,
  17. ReleaseFile,
  18. SourceFileType,
  19. )
  20. from sentry.models.releasefile import update_artifact_index
  21. from sentry.testutils import RelayStoreHelper
  22. from sentry.testutils.helpers import override_options
  23. from sentry.testutils.helpers.datetime import before_now, iso_format
  24. from sentry.testutils.skips import requires_symbolicator
  25. from sentry.utils import json
  26. # IMPORTANT:
  27. #
  28. # This test suite requires Symbolicator in order to run correctly.
  29. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  30. #
  31. # If you are using a local instance of Symbolicator, you need to either change `system.url-prefix` to `system.internal-url-prefix`
  32. # inside `process_with_symbolicator` fixture inside `src/sentry/utils/pytest/fixtures.py`,
  33. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  34. BASE64_SOURCEMAP = "data:application/json;base64," + (
  35. b64encode(
  36. b'{"version":3,"file":"generated.js","sources":["/test.js"],"names":[],"mappings":"AAAA","sourcesContent":['
  37. b'"console.log(\\"hello, World!\\")"]}'
  38. )
  39. .decode("utf-8")
  40. .replace("\n", "")
  41. )
  42. INVALID_BASE64_SOURCEMAP = "data:application/json;base64,A"
  43. def get_fixture_path(name):
  44. return os.path.join(os.path.dirname(__file__), "fixtures", name)
  45. def load_fixture(name):
  46. with open(get_fixture_path(name), "rb") as fp:
  47. return fp.read()
  48. @pytest.mark.django_db(transaction=True)
  49. class TestJavascriptIntegration(RelayStoreHelper):
  50. @pytest.fixture(autouse=True)
  51. def initialize(self, default_projectkey, default_project):
  52. self.project = default_project
  53. self.projectkey = default_projectkey
  54. self.organization = self.project.organization
  55. self.min_ago = iso_format(before_now(minutes=1))
  56. # We disable scraping per-test when necessary.
  57. self.project.update_option("sentry:scrape_javascript", True)
  58. @requires_symbolicator
  59. @pytest.mark.symbolicator
  60. def test_adds_contexts_without_device(self, process_with_symbolicator):
  61. data = {
  62. "timestamp": self.min_ago,
  63. "message": "hello",
  64. "platform": "javascript",
  65. "request": {
  66. "url": "http://example.com",
  67. "headers": [
  68. [
  69. "User-Agent",
  70. "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
  71. "Chrome/28.0.1500.72 Safari/537.36",
  72. ]
  73. ],
  74. },
  75. }
  76. event = self.post_and_retrieve_event(data)
  77. contexts = event.interfaces["contexts"].to_json()
  78. assert contexts.get("os") == {"name": "Windows", "version": "8", "type": "os"}
  79. assert contexts.get("device") is None
  80. @requires_symbolicator
  81. @pytest.mark.symbolicator
  82. @pytest.mark.skip(
  83. reason="temp disable test due to circular dependency before merging PR: https://github.com/getsentry/relay/pull/2004"
  84. )
  85. def test_adds_contexts_with_device(self, process_with_symbolicator):
  86. data = {
  87. "timestamp": self.min_ago,
  88. "message": "hello",
  89. "platform": "javascript",
  90. "request": {
  91. "url": "http://example.com",
  92. "headers": [
  93. [
  94. "User-Agent",
  95. "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SCH-R530U Build/JSS15J) AppleWebKit/534.30 ("
  96. "KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 USCC-R530U",
  97. ]
  98. ],
  99. },
  100. }
  101. event = self.post_and_retrieve_event(data)
  102. contexts = event.interfaces["contexts"].to_json()
  103. assert contexts.get("os") == {"name": "Android", "type": "os", "version": "4.3"}
  104. assert contexts.get("browser") == {"name": "Android", "type": "browser", "version": "4.3"}
  105. assert contexts.get("device") == {
  106. "family": "Samsung SCH-R530U",
  107. "type": "device",
  108. "model": "SCH-R530U",
  109. "brand": "Samsung",
  110. }
  111. @requires_symbolicator
  112. @pytest.mark.symbolicator
  113. def test_adds_contexts_with_ps4_device(self, process_with_symbolicator):
  114. data = {
  115. "timestamp": self.min_ago,
  116. "message": "hello",
  117. "platform": "javascript",
  118. "request": {
  119. "url": "http://example.com",
  120. "headers": [
  121. [
  122. "User-Agent",
  123. "Mozilla/5.0 (PlayStation 4 3.55) AppleWebKit/537.78 (KHTML, like Gecko)",
  124. ]
  125. ],
  126. },
  127. }
  128. event = self.post_and_retrieve_event(data)
  129. contexts = event.interfaces["contexts"].to_json()
  130. assert contexts.get("os") is None
  131. assert contexts.get("browser") is None
  132. assert contexts.get("device") == {
  133. "family": "PlayStation 4",
  134. "type": "device",
  135. "model": "PlayStation 4",
  136. "brand": "Sony",
  137. }
  138. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  139. def test_source_expansion(self, mock_fetch_by_url):
  140. data = {
  141. "timestamp": self.min_ago,
  142. "message": "hello",
  143. "platform": "javascript",
  144. "exception": {
  145. "values": [
  146. {
  147. "type": "Error",
  148. "stacktrace": {
  149. "frames": [
  150. {
  151. "abs_path": "http://example.com/foo.js",
  152. "filename": "foo.js",
  153. "lineno": 4,
  154. "colno": 0,
  155. },
  156. {
  157. "abs_path": "http://example.com/foo.js",
  158. "filename": "foo.js",
  159. "lineno": 1,
  160. "colno": 0,
  161. },
  162. ]
  163. },
  164. }
  165. ]
  166. },
  167. }
  168. mock_fetch_by_url.return_value.body = force_bytes("\n".join("hello world"))
  169. mock_fetch_by_url.return_value.encoding = None
  170. mock_fetch_by_url.return_value.headers = {}
  171. event = self.post_and_retrieve_event(data)
  172. mock_fetch_by_url.assert_called_once_with("http://example.com/foo.js")
  173. exception = event.interfaces["exception"]
  174. frame_list = exception.values[0].stacktrace.frames
  175. frame = frame_list[0]
  176. assert frame.pre_context == ["h", "e", "l"]
  177. assert frame.context_line == "l"
  178. assert frame.post_context == ["o", " ", "w", "o", "r"]
  179. frame = frame_list[1]
  180. assert not frame.pre_context
  181. assert frame.context_line == "h"
  182. assert frame.post_context == ["e", "l", "l", "o", " "]
  183. # no source map means no raw_stacktrace
  184. assert exception.values[0].raw_stacktrace is None
  185. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  186. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  187. def test_inlined_sources(self, mock_discover_sourcemap, mock_fetch_by_url):
  188. data = {
  189. "timestamp": self.min_ago,
  190. "message": "hello",
  191. "platform": "javascript",
  192. "exception": {
  193. "values": [
  194. {
  195. "type": "Error",
  196. "stacktrace": {
  197. "frames": [
  198. {
  199. "abs_path": "http://example.com/test.min.js",
  200. "filename": "test.js",
  201. "lineno": 1,
  202. "colno": 1,
  203. }
  204. ]
  205. },
  206. }
  207. ]
  208. },
  209. }
  210. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  211. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  212. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  213. mock_fetch_by_url.return_value.encoding = None
  214. event = self.post_and_retrieve_event(data)
  215. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  216. exception = event.interfaces["exception"]
  217. frame_list = exception.values[0].stacktrace.frames
  218. frame = frame_list[0]
  219. assert not frame.pre_context
  220. assert frame.context_line == 'console.log("hello, World!")'
  221. assert not frame.post_context
  222. assert frame.data["sourcemap"] == "http://example.com/test.min.js"
  223. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  224. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  225. def test_invalid_base64_sourcemap_returns_an_error(
  226. self, mock_discover_sourcemap, mock_fetch_by_url
  227. ):
  228. data = {
  229. "timestamp": self.min_ago,
  230. "message": "hello",
  231. "platform": "javascript",
  232. "exception": {
  233. "values": [
  234. {
  235. "type": "Error",
  236. "stacktrace": {
  237. "frames": [
  238. {
  239. "abs_path": "http://example.com/test.min.js",
  240. "filename": "test.js",
  241. "lineno": 1,
  242. "colno": 1,
  243. },
  244. ]
  245. },
  246. }
  247. ]
  248. },
  249. }
  250. mock_discover_sourcemap.return_value = INVALID_BASE64_SOURCEMAP
  251. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  252. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  253. mock_fetch_by_url.return_value.encoding = None
  254. event = self.post_and_retrieve_event(data)
  255. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  256. assert len(event.data["errors"]) == 1
  257. assert event.data["errors"][0] == {
  258. "url": "<base64>",
  259. "reason": "Invalid base64-encoded string: "
  260. "number of data characters (1) cannot be 1 more than a multiple of 4",
  261. "type": "js_invalid_source",
  262. }
  263. @patch("sentry.lang.javascript.processor.SmCache.from_bytes")
  264. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  265. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  266. def test_sourcemap_cache_is_constructed_only_once_if_an_error_is_raised(
  267. self, mock_discover_sourcemap, mock_fetch_by_url, mock_from_bytes
  268. ):
  269. data = {
  270. "timestamp": self.min_ago,
  271. "message": "hello",
  272. "platform": "javascript",
  273. "exception": {
  274. "values": [
  275. {
  276. "type": "Error",
  277. "stacktrace": {
  278. "frames": [
  279. {
  280. "abs_path": "http://example.com/test.min.js",
  281. "filename": "test.js",
  282. "lineno": 1,
  283. "colno": 1,
  284. },
  285. {
  286. "abs_path": "http://example.com/test.min.js",
  287. "filename": "test.js",
  288. "lineno": 1,
  289. "colno": 1,
  290. },
  291. {
  292. "abs_path": "http://example.com/test.min.js",
  293. "filename": "test.js",
  294. "lineno": 1,
  295. "colno": 1,
  296. },
  297. ]
  298. },
  299. }
  300. ]
  301. },
  302. }
  303. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  304. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  305. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  306. mock_fetch_by_url.return_value.encoding = None
  307. mock_from_bytes.side_effect = Exception()
  308. self.post_and_retrieve_event(data)
  309. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  310. mock_from_bytes.assert_called_once()
  311. @requires_symbolicator
  312. @pytest.mark.symbolicator
  313. def test_error_message_translations(self, process_with_symbolicator):
  314. data = {
  315. "timestamp": self.min_ago,
  316. "message": "hello",
  317. "platform": "javascript",
  318. "logentry": {
  319. "formatted": "ReferenceError: Impossible de d\xe9finir une propri\xe9t\xe9 \xab foo \xbb : objet non "
  320. "extensible"
  321. },
  322. "exception": {
  323. "values": [
  324. {"type": "Error", "value": "P\u0159\xedli\u0161 mnoho soubor\u016f"},
  325. {
  326. "type": "Error",
  327. "value": "foo: wyst\u0105pi\u0142 nieoczekiwany b\u0142\u0105d podczas pr\xf3by uzyskania "
  328. "informacji o metadanych",
  329. },
  330. ]
  331. },
  332. }
  333. event = self.post_and_retrieve_event(data)
  334. message = event.interfaces["logentry"]
  335. assert (
  336. message.formatted
  337. == "ReferenceError: Cannot define property 'foo': object is not extensible"
  338. )
  339. exception = event.interfaces["exception"]
  340. assert exception.values[0].value == "Too many files"
  341. assert (
  342. exception.values[1].value
  343. == "foo: an unexpected failure occurred while trying to obtain metadata information"
  344. )
  345. @requires_symbolicator
  346. @pytest.mark.symbolicator
  347. def test_sourcemap_source_expansion(self, process_with_symbolicator):
  348. self.project.update_option("sentry:scrape_javascript", False)
  349. release = Release.objects.create(
  350. organization_id=self.project.organization_id, version="abc"
  351. )
  352. release.add_project(self.project)
  353. for file in ["file.min.js", "file1.js", "file2.js", "file.sourcemap.js"]:
  354. with open(get_fixture_path(file), "rb") as f:
  355. f1 = File.objects.create(
  356. name=file,
  357. type="release.file",
  358. headers={},
  359. )
  360. f1.putfile(f)
  361. ReleaseFile.objects.create(
  362. name=f"http://example.com/{f1.name}",
  363. release_id=release.id,
  364. organization_id=self.project.organization_id,
  365. file=f1,
  366. )
  367. data = {
  368. "timestamp": self.min_ago,
  369. "message": "hello",
  370. "platform": "javascript",
  371. "release": "abc",
  372. "exception": {
  373. "values": [
  374. {
  375. "type": "Error",
  376. "stacktrace": {
  377. "frames": [
  378. {
  379. "abs_path": "http://example.com/file.min.js",
  380. "filename": "file.min.js",
  381. "lineno": 1,
  382. "colno": 39,
  383. },
  384. # NOTE: Intentionally source is not retrieved from this HTML file
  385. {
  386. "function": 'function: "HTMLDocument.<anonymous>"',
  387. "abs_path": "http//example.com/index.html",
  388. "filename": "index.html",
  389. "lineno": 283,
  390. "colno": 17,
  391. "in_app": False,
  392. },
  393. # NOTE: a mixed stack trace with a native frame:
  394. {
  395. "instruction_addr": "0xd10349",
  396. },
  397. ]
  398. },
  399. }
  400. ]
  401. },
  402. }
  403. event = self.post_and_retrieve_event(data)
  404. assert event.data["errors"] == [
  405. {"type": "js_no_source", "url": "http//example.com/index.html"}
  406. ]
  407. exception = event.interfaces["exception"]
  408. frame_list = exception.values[0].stacktrace.frames
  409. frame = frame_list[0]
  410. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  411. expected = "\treturn a + b; // fôo"
  412. assert frame.context_line == expected
  413. if process_with_symbolicator:
  414. assert frame.post_context == ["}"]
  415. else:
  416. assert frame.post_context == ["}", ""]
  417. raw_frame_list = exception.values[0].raw_stacktrace.frames
  418. raw_frame = raw_frame_list[0]
  419. assert not raw_frame.pre_context
  420. assert (
  421. raw_frame.context_line
  422. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  423. 'divide(a,b){"use strict";try{return multip {snip}'
  424. )
  425. if process_with_symbolicator:
  426. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js"]
  427. else:
  428. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  429. assert raw_frame.lineno == 1
  430. # Since we couldn't expand source for the 2nd frame, both
  431. # its raw and original form should be identical
  432. assert raw_frame_list[1] == frame_list[1]
  433. # The second non-js frame should be untouched
  434. assert raw_frame_list[2] == frame_list[2]
  435. @requires_symbolicator
  436. @pytest.mark.symbolicator
  437. def test_sourcemap_source_expansion_ab_test(self, process_with_symbolicator):
  438. self.project.update_option("sentry:scrape_javascript", False)
  439. release = Release.objects.create(
  440. organization_id=self.project.organization_id, version="abc"
  441. )
  442. release.add_project(self.project)
  443. for file in ["file.min.js", "file1.js", "file2.js", "file.sourcemap.js"]:
  444. with open(get_fixture_path(file), "rb") as f:
  445. f1 = File.objects.create(
  446. name=file,
  447. type="release.file",
  448. headers={},
  449. )
  450. f1.putfile(f)
  451. ReleaseFile.objects.create(
  452. name=f"http://example.com/{f1.name}",
  453. release_id=release.id,
  454. organization_id=self.project.organization_id,
  455. file=f1,
  456. )
  457. data = {
  458. "timestamp": self.min_ago,
  459. "message": "hello",
  460. "platform": "javascript",
  461. "release": "abc",
  462. "exception": {
  463. "values": [
  464. {
  465. "type": "Error",
  466. "stacktrace": {
  467. "frames": [
  468. {
  469. "abs_path": "http://example.com/file.min.js",
  470. "filename": "file.min.js",
  471. "lineno": 1,
  472. "colno": 39,
  473. },
  474. # NOTE: Intentionally source is not retrieved from this HTML file
  475. {
  476. "function": 'function: "HTMLDocument.<anonymous>"',
  477. "abs_path": "http//example.com/index.html",
  478. "filename": "index.html",
  479. "lineno": 283,
  480. "colno": 17,
  481. "in_app": False,
  482. },
  483. ]
  484. },
  485. }
  486. ]
  487. },
  488. }
  489. with override_options({"symbolicator.sourcemaps-processing-ab-test": 1.0}):
  490. event = self.post_and_retrieve_event(data)
  491. assert event.data["errors"] == [
  492. {"type": "js_no_source", "url": "http//example.com/index.html"}
  493. ]
  494. exception = event.interfaces["exception"]
  495. frame_list = exception.values[0].stacktrace.frames
  496. frame = frame_list[0]
  497. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  498. expected = "\treturn a + b; // fôo"
  499. assert frame.context_line == expected
  500. assert frame.post_context == ["}", ""]
  501. raw_frame_list = exception.values[0].raw_stacktrace.frames
  502. raw_frame = raw_frame_list[0]
  503. assert not raw_frame.pre_context
  504. assert (
  505. raw_frame.context_line
  506. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  507. 'divide(a,b){"use strict";try{return multip {snip}'
  508. )
  509. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  510. assert raw_frame.lineno == 1
  511. # Since we couldn't expand source for the 2nd frame, both
  512. # its raw and original form should be identical
  513. assert raw_frame_list[1] == frame_list[1]
  514. @requires_symbolicator
  515. @pytest.mark.symbolicator
  516. def test_sourcemap_embedded_source_expansion(self, process_with_symbolicator):
  517. self.project.update_option("sentry:scrape_javascript", False)
  518. release = Release.objects.create(
  519. organization_id=self.project.organization_id, version="abc"
  520. )
  521. release.add_project(self.project)
  522. for file in ["embedded.js", "embedded.js.map"]:
  523. with open(get_fixture_path(file), "rb") as f:
  524. f1 = File.objects.create(
  525. name=file,
  526. type="release.file",
  527. headers={},
  528. )
  529. f1.putfile(f)
  530. ReleaseFile.objects.create(
  531. name=f"http://example.com/{f1.name}",
  532. release_id=release.id,
  533. organization_id=self.project.organization_id,
  534. file=f1,
  535. )
  536. data = {
  537. "timestamp": self.min_ago,
  538. "message": "hello",
  539. "platform": "javascript",
  540. "release": "abc",
  541. "exception": {
  542. "values": [
  543. {
  544. "type": "Error",
  545. "stacktrace": {
  546. "frames": [
  547. {
  548. "abs_path": "http://example.com/embedded.js",
  549. "filename": "file.min.js",
  550. "lineno": 1,
  551. "colno": 39,
  552. },
  553. # NOTE: Intentionally source is not retrieved from this HTML file
  554. {
  555. "function": 'function: "HTMLDocument.<anonymous>"',
  556. "abs_path": "http//example.com/index.html",
  557. "filename": "index.html",
  558. "lineno": 283,
  559. "colno": 17,
  560. "in_app": False,
  561. },
  562. ]
  563. },
  564. }
  565. ]
  566. },
  567. }
  568. event = self.post_and_retrieve_event(data)
  569. assert event.data["errors"] == [
  570. {"type": "js_no_source", "url": "http//example.com/index.html"}
  571. ]
  572. exception = event.interfaces["exception"]
  573. frame_list = exception.values[0].stacktrace.frames
  574. frame = frame_list[0]
  575. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  576. expected = "\treturn a + b; // fôo"
  577. assert frame.context_line == expected
  578. if process_with_symbolicator:
  579. assert frame.post_context == ["}"]
  580. else:
  581. assert frame.post_context == ["}", ""]
  582. @requires_symbolicator
  583. @pytest.mark.symbolicator
  584. def test_sourcemap_nofiles_source_expansion(self, process_with_symbolicator):
  585. project = self.project
  586. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  587. release.add_project(project)
  588. with open(get_fixture_path("nofiles.js"), "rb") as f:
  589. f_minified = File.objects.create(
  590. name="nofiles.js", type="release.file", headers={"Content-Type": "application/json"}
  591. )
  592. f_minified.putfile(f)
  593. ReleaseFile.objects.create(
  594. name=f"~/{f_minified.name}",
  595. release_id=release.id,
  596. organization_id=project.organization_id,
  597. file=f_minified,
  598. )
  599. with open(get_fixture_path("nofiles.js.map"), "rb") as f:
  600. f_sourcemap = File.objects.create(
  601. name="nofiles.js.map",
  602. type="release.file",
  603. headers={"Content-Type": "application/json"},
  604. )
  605. f_sourcemap.putfile(f)
  606. ReleaseFile.objects.create(
  607. name=f"app:///{f_sourcemap.name}",
  608. release_id=release.id,
  609. organization_id=project.organization_id,
  610. file=f_sourcemap,
  611. )
  612. data = {
  613. "timestamp": self.min_ago,
  614. "message": "hello",
  615. "platform": "javascript",
  616. "release": "abc",
  617. "exception": {
  618. "values": [
  619. {
  620. "type": "Error",
  621. "stacktrace": {
  622. "frames": [{"abs_path": "app:///nofiles.js", "lineno": 1, "colno": 39}]
  623. },
  624. }
  625. ]
  626. },
  627. }
  628. event = self.post_and_retrieve_event(data)
  629. assert "errors" not in event.data
  630. exception = event.interfaces["exception"]
  631. frame_list = exception.values[0].stacktrace.frames
  632. assert len(frame_list) == 1
  633. frame = frame_list[0]
  634. assert frame.abs_path == "app:///nofiles.js"
  635. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  636. assert frame.context_line == "\treturn a + b; // fôo"
  637. if process_with_symbolicator:
  638. assert frame.post_context == ["}"]
  639. else:
  640. assert frame.post_context == ["}", ""]
  641. @requires_symbolicator
  642. @pytest.mark.symbolicator
  643. def test_indexed_sourcemap_source_expansion(self, process_with_symbolicator):
  644. self.project.update_option("sentry:scrape_javascript", False)
  645. release = Release.objects.create(
  646. organization_id=self.project.organization_id, version="abc"
  647. )
  648. release.add_project(self.project)
  649. for file in ["indexed.min.js", "file1.js", "file2.js", "indexed.sourcemap.js"]:
  650. with open(get_fixture_path(file), "rb") as f:
  651. f1 = File.objects.create(
  652. name=file,
  653. type="release.file",
  654. headers={},
  655. )
  656. f1.putfile(f)
  657. ReleaseFile.objects.create(
  658. name=f"http://example.com/{f1.name}",
  659. release_id=release.id,
  660. organization_id=self.project.organization_id,
  661. file=f1,
  662. )
  663. data = {
  664. "timestamp": self.min_ago,
  665. "message": "hello",
  666. "platform": "javascript",
  667. "release": "abc",
  668. "exception": {
  669. "values": [
  670. {
  671. "type": "Error",
  672. "stacktrace": {
  673. "frames": [
  674. {
  675. "abs_path": "http://example.com/indexed.min.js",
  676. "filename": "indexed.min.js",
  677. "lineno": 1,
  678. "colno": 39,
  679. },
  680. {
  681. "abs_path": "http://example.com/indexed.min.js",
  682. "filename": "indexed.min.js",
  683. "lineno": 2,
  684. "colno": 44,
  685. },
  686. ]
  687. },
  688. }
  689. ]
  690. },
  691. }
  692. event = self.post_and_retrieve_event(data)
  693. assert "errors" not in event.data
  694. exception = event.interfaces["exception"]
  695. frame_list = exception.values[0].stacktrace.frames
  696. frame = frame_list[0]
  697. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  698. expected = "\treturn a + b; // fôo"
  699. assert frame.context_line == expected
  700. if process_with_symbolicator:
  701. assert frame.post_context == ["}"]
  702. else:
  703. assert frame.post_context == ["}", ""]
  704. raw_frame_list = exception.values[0].raw_stacktrace.frames
  705. raw_frame = raw_frame_list[0]
  706. assert not raw_frame.pre_context
  707. assert raw_frame.context_line == 'function add(a,b){"use strict";return a+b}'
  708. if process_with_symbolicator:
  709. assert raw_frame.post_context == [
  710. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  711. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  712. "//# sourceMappingURL=indexed.sourcemap.js",
  713. ]
  714. else:
  715. assert raw_frame.post_context == [
  716. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  717. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  718. "//# sourceMappingURL=indexed.sourcemap.js",
  719. "",
  720. ]
  721. assert raw_frame.lineno == 1
  722. frame = frame_list[1]
  723. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  724. assert frame.context_line == "\treturn a * b;"
  725. assert frame.post_context == [
  726. "}",
  727. "function divide(a, b) {",
  728. '\t"use strict";',
  729. "\ttry {",
  730. "\t\treturn multiply(add(a, b), a, b) / c;",
  731. ]
  732. raw_frame = raw_frame_list[1]
  733. assert raw_frame.pre_context == ['function add(a,b){"use strict";return a+b}']
  734. assert (
  735. raw_frame.context_line
  736. == 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  737. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}"
  738. )
  739. if process_with_symbolicator:
  740. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js"]
  741. else:
  742. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js", ""]
  743. assert raw_frame.lineno == 2
  744. @requires_symbolicator
  745. @pytest.mark.symbolicator
  746. def test_expansion_via_debug(self, process_with_symbolicator):
  747. project = self.project
  748. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  749. release.add_project(project)
  750. # file.min.js
  751. # ------------
  752. with open(get_fixture_path("file.min.js"), "rb") as f:
  753. f_minified = File.objects.create(
  754. name="file.min.js",
  755. type="release.file",
  756. headers={"Content-Type": "application/json"},
  757. )
  758. f_minified.putfile(f)
  759. # Intentionally omit hostname - use alternate artifact path lookup instead
  760. # /file1.js vs http://example.com/file1.js
  761. ReleaseFile.objects.create(
  762. name=f"~/{f_minified.name}?foo=bar",
  763. release_id=release.id,
  764. organization_id=project.organization_id,
  765. file=f_minified,
  766. )
  767. # file1.js
  768. # ---------
  769. with open(get_fixture_path("file1.js"), "rb") as f:
  770. f1 = File.objects.create(
  771. name="file1.js", type="release.file", headers={"Content-Type": "application/json"}
  772. )
  773. f1.putfile(f)
  774. ReleaseFile.objects.create(
  775. name=f"http://example.com/{f1.name}",
  776. release_id=release.id,
  777. organization_id=project.organization_id,
  778. file=f1,
  779. )
  780. # file2.js
  781. # ----------
  782. with open(get_fixture_path("file2.js"), "rb") as f:
  783. f2 = File.objects.create(
  784. name="file2.js", type="release.file", headers={"Content-Type": "application/json"}
  785. )
  786. f2.putfile(f)
  787. ReleaseFile.objects.create(
  788. name=f"http://example.com/{f2.name}",
  789. release_id=release.id,
  790. organization_id=project.organization_id,
  791. file=f2,
  792. )
  793. # To verify that the full url has priority over the relative url,
  794. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  795. # hostname that points to an empty file. If the processor chooses
  796. # this empty file over the correct file2.js, it will not locate
  797. # context for the 2nd frame.
  798. with open(get_fixture_path("empty.js"), "rb") as f:
  799. f2_empty = File.objects.create(
  800. name="empty.js", type="release.file", headers={"Content-Type": "application/json"}
  801. )
  802. f2_empty.putfile(f)
  803. ReleaseFile.objects.create(
  804. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  805. release_id=release.id,
  806. organization_id=project.organization_id,
  807. file=f2_empty,
  808. )
  809. # sourcemap
  810. # ----------
  811. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  812. f_sourcemap = File.objects.create(
  813. name="file.sourcemap.js",
  814. type="release.file",
  815. headers={"Content-Type": "application/json"},
  816. )
  817. f_sourcemap.putfile(f)
  818. ReleaseFile.objects.create(
  819. name=f"http://example.com/{f_sourcemap.name}",
  820. release_id=release.id,
  821. organization_id=project.organization_id,
  822. file=f_sourcemap,
  823. )
  824. data = {
  825. "timestamp": self.min_ago,
  826. "message": "hello",
  827. "platform": "javascript",
  828. "release": "abc",
  829. "exception": {
  830. "values": [
  831. {
  832. "type": "Error",
  833. "stacktrace": {
  834. "frames": [
  835. {
  836. "abs_path": "http://example.com/file.min.js?foo=bar",
  837. "filename": "file.min.js",
  838. "lineno": 1,
  839. "colno": 39,
  840. },
  841. {
  842. "abs_path": "http://example.com/file.min.js?foo=bar",
  843. "filename": "file.min.js",
  844. "lineno": 1,
  845. "colno": 79,
  846. },
  847. ]
  848. },
  849. }
  850. ]
  851. },
  852. }
  853. event = self.post_and_retrieve_event(data)
  854. assert "errors" not in event.data
  855. exception = event.interfaces["exception"]
  856. frame_list = exception.values[0].stacktrace.frames
  857. frame = frame_list[0]
  858. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  859. assert frame.context_line == "\treturn a + b; // fôo"
  860. if process_with_symbolicator:
  861. assert frame.post_context == ["}"]
  862. else:
  863. assert frame.post_context == ["}", ""]
  864. frame = frame_list[1]
  865. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  866. assert frame.context_line == "\treturn a * b;"
  867. assert frame.post_context == [
  868. "}",
  869. "function divide(a, b) {",
  870. '\t"use strict";',
  871. "\ttry {",
  872. "\t\treturn multiply(add(a, b), a, b) / c;",
  873. ]
  874. @requires_symbolicator
  875. @pytest.mark.symbolicator
  876. def test_expansion_via_distribution_release_artifacts(self, process_with_symbolicator):
  877. project = self.project
  878. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  879. release.add_project(project)
  880. dist = release.add_dist("foo")
  881. # file.min.js
  882. # ------------
  883. with open(get_fixture_path("file.min.js"), "rb") as f:
  884. f_minified = File.objects.create(
  885. name="file.min.js",
  886. type="release.file",
  887. headers={"Content-Type": "application/json"},
  888. )
  889. f_minified.putfile(f)
  890. # Intentionally omit hostname - use alternate artifact path lookup instead
  891. # /file1.js vs http://example.com/file1.js
  892. ReleaseFile.objects.create(
  893. name=f"~/{f_minified.name}?foo=bar",
  894. release_id=release.id,
  895. dist_id=dist.id,
  896. organization_id=project.organization_id,
  897. file=f_minified,
  898. )
  899. # file1.js
  900. # ---------
  901. with open(get_fixture_path("file1.js"), "rb") as f:
  902. f1 = File.objects.create(
  903. name="file1.js",
  904. type="release.file",
  905. headers={"Content-Type": "application/json"},
  906. )
  907. f1.putfile(f)
  908. ReleaseFile.objects.create(
  909. name=f"http://example.com/{f1.name}",
  910. release_id=release.id,
  911. dist_id=dist.id,
  912. organization_id=project.organization_id,
  913. file=f1,
  914. )
  915. # file2.js
  916. # ----------
  917. with open(get_fixture_path("file2.js"), "rb") as f:
  918. f2 = File.objects.create(
  919. name="file2.js",
  920. type="release.file",
  921. headers={"Content-Type": "application/json"},
  922. )
  923. f2.putfile(f)
  924. ReleaseFile.objects.create(
  925. name=f"http://example.com/{f2.name}",
  926. release_id=release.id,
  927. dist_id=dist.id,
  928. organization_id=project.organization_id,
  929. file=f2,
  930. )
  931. # To verify that the full url has priority over the relative url,
  932. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  933. # hostname that points to an empty file. If the processor chooses
  934. # this empty file over the correct file2.js, it will not locate
  935. # context for the 2nd frame.
  936. with open(get_fixture_path("empty.js"), "rb") as f:
  937. f2_empty = File.objects.create(
  938. name="empty.js",
  939. type="release.file",
  940. headers={"Content-Type": "application/json"},
  941. )
  942. f2_empty.putfile(f)
  943. ReleaseFile.objects.create(
  944. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  945. release_id=release.id,
  946. dist_id=dist.id,
  947. organization_id=project.organization_id,
  948. file=f2_empty,
  949. )
  950. # sourcemap
  951. # ----------
  952. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  953. f_sourcemap = File.objects.create(
  954. name="file.sourcemap.js",
  955. type="release.file",
  956. headers={"Content-Type": "application/json"},
  957. )
  958. f_sourcemap.putfile(f)
  959. ReleaseFile.objects.create(
  960. name=f"http://example.com/{f_sourcemap.name}",
  961. release_id=release.id,
  962. dist_id=dist.id,
  963. organization_id=project.organization_id,
  964. file=f_sourcemap,
  965. )
  966. data = {
  967. "timestamp": self.min_ago,
  968. "message": "hello",
  969. "platform": "javascript",
  970. "release": "abc",
  971. "dist": "foo",
  972. "exception": {
  973. "values": [
  974. {
  975. "type": "Error",
  976. "stacktrace": {
  977. "frames": [
  978. {
  979. "abs_path": "http://example.com/file.min.js?foo=bar",
  980. "filename": "file.min.js",
  981. "lineno": 1,
  982. "colno": 39,
  983. },
  984. {
  985. "abs_path": "http://example.com/file.min.js?foo=bar",
  986. "filename": "file.min.js",
  987. "lineno": 1,
  988. "colno": 79,
  989. },
  990. ]
  991. },
  992. }
  993. ]
  994. },
  995. }
  996. event = self.post_and_retrieve_event(data)
  997. assert "errors" not in event.data
  998. exception = event.interfaces["exception"]
  999. frame_list = exception.values[0].stacktrace.frames
  1000. frame = frame_list[0]
  1001. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1002. assert frame.context_line == "\treturn a + b; // fôo"
  1003. if process_with_symbolicator:
  1004. assert frame.post_context == ["}"]
  1005. else:
  1006. assert frame.post_context == ["}", ""]
  1007. frame = frame_list[1]
  1008. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1009. assert frame.context_line == "\treturn a * b;"
  1010. assert frame.post_context == [
  1011. "}",
  1012. "function divide(a, b) {",
  1013. '\t"use strict";',
  1014. "\ttry {",
  1015. "\t\treturn multiply(add(a, b), a, b) / c;",
  1016. ]
  1017. @responses.activate
  1018. def test_sourcemap_expansion_with_missing_source(self):
  1019. """
  1020. Tests a successful sourcemap expansion that points to source files
  1021. that are not found.
  1022. """
  1023. responses.add(
  1024. responses.GET,
  1025. "http://example.com/file.min.js",
  1026. body=load_fixture("file.min.js"),
  1027. content_type="application/javascript; charset=utf-8",
  1028. )
  1029. responses.add(
  1030. responses.GET,
  1031. "http://example.com/file.sourcemap.js",
  1032. body=load_fixture("file.sourcemap.js"),
  1033. content_type="application/json; charset=utf-8",
  1034. )
  1035. responses.add(responses.GET, "http://example.com/file1.js", body="Not Found", status=404)
  1036. data = {
  1037. "timestamp": self.min_ago,
  1038. "message": "hello",
  1039. "platform": "javascript",
  1040. "exception": {
  1041. "values": [
  1042. {
  1043. "type": "Error",
  1044. "stacktrace": {
  1045. # Add two frames. We only want to see the
  1046. # error once though.
  1047. "frames": [
  1048. {
  1049. "abs_path": "http://example.com/file.min.js",
  1050. "filename": "file.min.js",
  1051. "lineno": 1,
  1052. "colno": 39,
  1053. },
  1054. {
  1055. "abs_path": "http://example.com/file.min.js",
  1056. "filename": "file.min.js",
  1057. "lineno": 1,
  1058. "colno": 39,
  1059. },
  1060. ]
  1061. },
  1062. }
  1063. ]
  1064. },
  1065. }
  1066. event = self.post_and_retrieve_event(data)
  1067. assert event.data["errors"] == [
  1068. {"url": "http://example.com/file1.js", "type": "fetch_invalid_http_code", "value": 404}
  1069. ]
  1070. exception = event.interfaces["exception"]
  1071. frame_list = exception.values[0].stacktrace.frames
  1072. frame = frame_list[0]
  1073. # no context information ...
  1074. assert not frame.pre_context
  1075. assert not frame.context_line
  1076. assert not frame.post_context
  1077. # ... but line, column numbers are still correctly mapped
  1078. assert frame.lineno == 3
  1079. assert frame.colno == 9
  1080. @responses.activate
  1081. def test_failed_sourcemap_expansion(self):
  1082. """
  1083. Tests attempting to parse an indexed source map where each section has a "url"
  1084. property - this is unsupported and should fail.
  1085. """
  1086. responses.add(
  1087. responses.GET,
  1088. "http://example.com/unsupported.min.js",
  1089. body=load_fixture("unsupported.min.js"),
  1090. content_type="application/javascript; charset=utf-8",
  1091. )
  1092. responses.add(
  1093. responses.GET,
  1094. "http://example.com/unsupported.sourcemap.js",
  1095. body=load_fixture("unsupported.sourcemap.js"),
  1096. content_type="application/json; charset=utf-8",
  1097. )
  1098. data = {
  1099. "timestamp": self.min_ago,
  1100. "message": "hello",
  1101. "platform": "javascript",
  1102. "exception": {
  1103. "values": [
  1104. {
  1105. "type": "Error",
  1106. "stacktrace": {
  1107. "frames": [
  1108. {
  1109. "abs_path": "http://example.com/unsupported.min.js",
  1110. "filename": "indexed.min.js",
  1111. "lineno": 1,
  1112. "colno": 39,
  1113. }
  1114. ]
  1115. },
  1116. }
  1117. ]
  1118. },
  1119. }
  1120. event = self.post_and_retrieve_event(data)
  1121. assert event.data["errors"] == [
  1122. {"url": "http://example.com/unsupported.sourcemap.js", "type": "js_invalid_source"}
  1123. ]
  1124. def test_failed_sourcemap_expansion_data_url(self):
  1125. data = {
  1126. "timestamp": self.min_ago,
  1127. "message": "hello",
  1128. "platform": "javascript",
  1129. "exception": {
  1130. "values": [
  1131. {
  1132. "type": "Error",
  1133. "stacktrace": {
  1134. "frames": [
  1135. {
  1136. "abs_path": "data:application/javascript,base46,asfasf",
  1137. "filename": "indexed.min.js",
  1138. "lineno": 1,
  1139. "colno": 39,
  1140. }
  1141. ]
  1142. },
  1143. }
  1144. ]
  1145. },
  1146. }
  1147. event = self.post_and_retrieve_event(data)
  1148. assert event.data["errors"] == [{"url": "<data url>", "type": "js_no_source"}]
  1149. @responses.activate
  1150. def test_failed_sourcemap_expansion_missing_location_entirely(self):
  1151. responses.add(
  1152. responses.GET,
  1153. "http://example.com/indexed.min.js",
  1154. body="//# sourceMappingURL=indexed.sourcemap.js",
  1155. )
  1156. responses.add(responses.GET, "http://example.com/indexed.sourcemap.js", body="{}")
  1157. data = {
  1158. "timestamp": self.min_ago,
  1159. "message": "hello",
  1160. "platform": "javascript",
  1161. "exception": {
  1162. "values": [
  1163. {
  1164. "type": "Error",
  1165. "stacktrace": {
  1166. "frames": [
  1167. {
  1168. "abs_path": "http://example.com/indexed.min.js",
  1169. "filename": "indexed.min.js",
  1170. "lineno": 1,
  1171. "colno": 1,
  1172. },
  1173. {
  1174. "abs_path": "http://example.com/indexed.min.js",
  1175. "filename": "indexed.min.js",
  1176. },
  1177. ]
  1178. },
  1179. }
  1180. ]
  1181. },
  1182. }
  1183. event = self.post_and_retrieve_event(data)
  1184. assert "errors" not in event.data
  1185. @responses.activate
  1186. def test_html_response_for_js(self):
  1187. responses.add(
  1188. responses.GET,
  1189. "http://example.com/invalid_file1.js",
  1190. body=" <!DOCTYPE html><html><head></head><body></body></html>",
  1191. )
  1192. responses.add(
  1193. responses.GET,
  1194. "http://example.com/invalid_file2.js",
  1195. body="<!doctype html><html><head></head><body></body></html>",
  1196. )
  1197. responses.add(
  1198. responses.GET,
  1199. "http://example.com/valid_file.html",
  1200. body=(
  1201. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1202. ),
  1203. )
  1204. data = {
  1205. "timestamp": self.min_ago,
  1206. "message": "hello",
  1207. "platform": "javascript",
  1208. "exception": {
  1209. "values": [
  1210. {
  1211. "type": "Error",
  1212. "stacktrace": {
  1213. "frames": [
  1214. {
  1215. "abs_path": "http://example.com/invalid_file1.js",
  1216. "filename": "invalid_file1.js",
  1217. "lineno": 1,
  1218. "colno": 39,
  1219. },
  1220. {
  1221. "abs_path": "http://example.com/invalid_file2.js",
  1222. "filename": "invalid_file2.js",
  1223. "lineno": 1,
  1224. "colno": 39,
  1225. },
  1226. {
  1227. "abs_path": "http://example.com/valid_file.html",
  1228. "filename": "valid_file.html",
  1229. "lineno": 1,
  1230. "colno": 1,
  1231. },
  1232. ]
  1233. },
  1234. }
  1235. ]
  1236. },
  1237. }
  1238. event = self.post_and_retrieve_event(data)
  1239. assert event.data["errors"] == [
  1240. {"url": "http://example.com/invalid_file1.js", "type": "js_invalid_content"},
  1241. {"url": "http://example.com/invalid_file2.js", "type": "js_invalid_content"},
  1242. ]
  1243. def _test_expansion_via_release_archive(
  1244. self, link_sourcemaps: bool, process_with_symbolicator: bool
  1245. ):
  1246. project = self.project
  1247. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1248. release.add_project(project)
  1249. manifest = {
  1250. "org": self.organization.slug,
  1251. "release": release.version,
  1252. "files": {
  1253. "files/_/_/file.min.js": {
  1254. "url": "http://example.com/file.min.js",
  1255. "type": "minified_source",
  1256. },
  1257. "files/_/_/file1.js": {
  1258. "url": "http://example.com/file1.js",
  1259. "type": "source",
  1260. },
  1261. "files/_/_/file2.js": {
  1262. "url": "http://example.com/file2.js",
  1263. "type": "source",
  1264. },
  1265. "files/_/_/file.sourcemap.js": {
  1266. "url": "http://example.com/file.sourcemap.js",
  1267. "type": "source_map",
  1268. },
  1269. },
  1270. }
  1271. compressed = BytesIO(b"SYSB")
  1272. with zipfile.ZipFile(compressed, "a") as zip_file:
  1273. for rel_path, entry in manifest["files"].items():
  1274. name = os.path.basename(rel_path)
  1275. content = load_fixture(name)
  1276. if name == "file.min.js" and not link_sourcemaps:
  1277. # Remove link to source map, add to header instead
  1278. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  1279. entry["headers"] = {"Sourcemap": "file.sourcemap.js"}
  1280. zip_file.writestr(rel_path, content)
  1281. zip_file.writestr("manifest.json", json.dumps(manifest))
  1282. compressed.seek(0)
  1283. file = File.objects.create(name="doesnt_matter", type="release.bundle")
  1284. file.putfile(compressed)
  1285. update_artifact_index(release, None, file)
  1286. data = {
  1287. "timestamp": self.min_ago,
  1288. "message": "hello",
  1289. "platform": "javascript",
  1290. "release": "abc",
  1291. "exception": {
  1292. "values": [
  1293. {
  1294. "type": "Error",
  1295. "stacktrace": {
  1296. "frames": [
  1297. {
  1298. "abs_path": "http://example.com/file.min.js",
  1299. "filename": "file.min.js",
  1300. "lineno": 1,
  1301. "colno": 39,
  1302. },
  1303. {
  1304. "abs_path": "http://example.com/file.min.js",
  1305. "filename": "file.min.js",
  1306. "lineno": 1,
  1307. "colno": 79,
  1308. },
  1309. ]
  1310. },
  1311. }
  1312. ]
  1313. },
  1314. }
  1315. event = self.post_and_retrieve_event(data)
  1316. assert "errors" not in event.data
  1317. exception = event.interfaces["exception"]
  1318. frame_list = exception.values[0].stacktrace.frames
  1319. frame = frame_list[0]
  1320. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1321. assert frame.context_line == "\treturn a + b; // fôo"
  1322. if process_with_symbolicator:
  1323. assert frame.post_context == ["}"]
  1324. else:
  1325. assert frame.post_context == ["}", ""]
  1326. frame = frame_list[1]
  1327. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1328. assert frame.context_line == "\treturn a * b;"
  1329. assert frame.post_context == [
  1330. "}",
  1331. "function divide(a, b) {",
  1332. '\t"use strict";',
  1333. "\ttry {",
  1334. "\t\treturn multiply(add(a, b), a, b) / c;",
  1335. ]
  1336. @requires_symbolicator
  1337. @pytest.mark.symbolicator
  1338. def test_expansion_via_release_archive(self, process_with_symbolicator):
  1339. self._test_expansion_via_release_archive(
  1340. link_sourcemaps=True, process_with_symbolicator=process_with_symbolicator
  1341. )
  1342. @requires_symbolicator
  1343. @pytest.mark.symbolicator
  1344. def test_expansion_via_release_archive_no_sourcemap_link(self, process_with_symbolicator):
  1345. self._test_expansion_via_release_archive(
  1346. link_sourcemaps=False, process_with_symbolicator=process_with_symbolicator
  1347. )
  1348. @requires_symbolicator
  1349. @pytest.mark.symbolicator
  1350. def test_node_processing(self, process_with_symbolicator):
  1351. project = self.project
  1352. release = Release.objects.create(
  1353. organization_id=project.organization_id, version="nodeabc123"
  1354. )
  1355. release.add_project(project)
  1356. with open(get_fixture_path("dist.bundle.js"), "rb") as f:
  1357. f_minified = File.objects.create(
  1358. name="dist.bundle.js",
  1359. type="release.file",
  1360. headers={"Content-Type": "application/javascript"},
  1361. )
  1362. f_minified.putfile(f)
  1363. ReleaseFile.objects.create(
  1364. name=f"~/{f_minified.name}",
  1365. release_id=release.id,
  1366. organization_id=project.organization_id,
  1367. file=f_minified,
  1368. )
  1369. with open(get_fixture_path("dist.bundle.js.map"), "rb") as f:
  1370. f_sourcemap = File.objects.create(
  1371. name="dist.bundle.js.map",
  1372. type="release.file",
  1373. headers={"Content-Type": "application/javascript"},
  1374. )
  1375. f_sourcemap.putfile(f)
  1376. ReleaseFile.objects.create(
  1377. name=f"~/{f_sourcemap.name}",
  1378. release_id=release.id,
  1379. organization_id=project.organization_id,
  1380. file=f_sourcemap,
  1381. )
  1382. data = {
  1383. "timestamp": self.min_ago,
  1384. "message": "hello",
  1385. "platform": "node",
  1386. "release": "nodeabc123",
  1387. "exception": {
  1388. "values": [
  1389. {
  1390. "type": "Error",
  1391. "stacktrace": {
  1392. "frames": [
  1393. {
  1394. "filename": "app:///dist.bundle.js",
  1395. "function": "bar",
  1396. "lineno": 9,
  1397. "colno": 2321,
  1398. },
  1399. {
  1400. "filename": "app:///dist.bundle.js",
  1401. "function": "foo",
  1402. "lineno": 3,
  1403. "colno": 2308,
  1404. },
  1405. {
  1406. "filename": "app:///dist.bundle.js",
  1407. "function": "App",
  1408. "lineno": 3,
  1409. "colno": 1011,
  1410. },
  1411. {
  1412. "filename": "app:///dist.bundle.js",
  1413. "function": "Object.<anonymous>",
  1414. "lineno": 1,
  1415. "colno": 1014,
  1416. },
  1417. {
  1418. "filename": "app:///dist.bundle.js",
  1419. "function": "__webpack_require__",
  1420. "lineno": 20,
  1421. "colno": 30,
  1422. },
  1423. {
  1424. "filename": "app:///dist.bundle.js",
  1425. "function": "<unknown>",
  1426. "lineno": 18,
  1427. "colno": 63,
  1428. },
  1429. ]
  1430. },
  1431. }
  1432. ]
  1433. },
  1434. }
  1435. event = self.post_and_retrieve_event(data)
  1436. exception = event.interfaces["exception"]
  1437. frame_list = exception.values[0].stacktrace.frames
  1438. assert len(frame_list) == 6
  1439. def assert_abs_path(abs_path):
  1440. # This makes the test assertion forward compatible with percent-encoded URLs
  1441. # See https://github.com/getsentry/symbolicator/pull/1137
  1442. assert abs_path in (
  1443. "webpack:///webpack/bootstrap d9a5a31d9276b73873d3",
  1444. "webpack:///webpack/bootstrap%20d9a5a31d9276b73873d3",
  1445. )
  1446. assert_abs_path(frame_list[0].abs_path)
  1447. assert frame_list[0].function == "bar"
  1448. assert frame_list[0].lineno == 8
  1449. assert_abs_path(frame_list[1].abs_path)
  1450. assert frame_list[1].function == "foo"
  1451. assert frame_list[1].lineno == 2
  1452. assert_abs_path(frame_list[2].abs_path)
  1453. assert frame_list[2].function == "App"
  1454. assert frame_list[2].lineno == 2
  1455. assert_abs_path(frame_list[3].abs_path)
  1456. assert frame_list[3].function == "Object.<anonymous>"
  1457. assert frame_list[3].lineno == 1
  1458. assert_abs_path(frame_list[4].abs_path)
  1459. assert frame_list[4].function == "__webpack_require__"
  1460. assert frame_list[4].lineno == 19
  1461. assert_abs_path(frame_list[5].abs_path)
  1462. assert frame_list[5].function == "<unknown>"
  1463. assert frame_list[5].lineno == 16
  1464. @responses.activate
  1465. def test_no_fetch_from_http(self):
  1466. responses.add(
  1467. responses.GET,
  1468. "http://example.com/node_app.min.js",
  1469. body=load_fixture("node_app.min.js"),
  1470. content_type="application/javascript; charset=utf-8",
  1471. )
  1472. responses.add(
  1473. responses.GET,
  1474. "http://example.com/node_app.min.js.map",
  1475. body=load_fixture("node_app.min.js.map"),
  1476. content_type="application/javascript; charset=utf-8",
  1477. )
  1478. data = {
  1479. "timestamp": self.min_ago,
  1480. "message": "hello",
  1481. "platform": "node",
  1482. "exception": {
  1483. "values": [
  1484. {
  1485. "type": "Error",
  1486. "stacktrace": {
  1487. "frames": [
  1488. {
  1489. "abs_path": "node_bootstrap.js",
  1490. "filename": "node_bootstrap.js",
  1491. "lineno": 1,
  1492. "colno": 38,
  1493. },
  1494. {
  1495. "abs_path": "timers.js",
  1496. "filename": "timers.js",
  1497. "lineno": 1,
  1498. "colno": 39,
  1499. },
  1500. {
  1501. "abs_path": "webpack:///internal",
  1502. "filename": "internal",
  1503. "lineno": 1,
  1504. "colno": 43,
  1505. },
  1506. {
  1507. "abs_path": "webpack:///~/some_dep/file.js",
  1508. "filename": "file.js",
  1509. "lineno": 1,
  1510. "colno": 41,
  1511. },
  1512. {
  1513. "abs_path": "webpack:///./node_modules/file.js",
  1514. "filename": "file.js",
  1515. "lineno": 1,
  1516. "colno": 42,
  1517. },
  1518. {
  1519. "abs_path": "http://example.com/node_app.min.js",
  1520. "filename": "node_app.min.js",
  1521. "lineno": 1,
  1522. "colno": 40,
  1523. },
  1524. ]
  1525. },
  1526. }
  1527. ]
  1528. },
  1529. }
  1530. event = self.post_and_retrieve_event(data)
  1531. exception = event.interfaces["exception"]
  1532. frame_list = exception.values[0].stacktrace.frames
  1533. # This one should not process, so this one should be none.
  1534. assert exception.values[0].raw_stacktrace is None
  1535. # None of the in app should update
  1536. for x in range(6):
  1537. assert not frame_list[x].in_app
  1538. @responses.activate
  1539. def test_html_file_with_query_param_ending_with_js_extension(self):
  1540. responses.add(
  1541. responses.GET,
  1542. "http://example.com/file.html",
  1543. body=(
  1544. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1545. ),
  1546. )
  1547. data = {
  1548. "timestamp": self.min_ago,
  1549. "message": "hello",
  1550. "platform": "javascript",
  1551. "exception": {
  1552. "values": [
  1553. {
  1554. "type": "Error",
  1555. "stacktrace": {
  1556. "frames": [
  1557. {
  1558. "abs_path": "http://example.com/file.html?sw=iddqd1337.js",
  1559. "filename": "file.html",
  1560. "lineno": 1,
  1561. "colno": 1,
  1562. },
  1563. ]
  1564. },
  1565. }
  1566. ]
  1567. },
  1568. }
  1569. event = self.post_and_retrieve_event(data)
  1570. assert "errors" not in event.data
  1571. def test_expansion_with_allow_scraping_false(self):
  1572. project = self.project
  1573. project.organization.update_option("sentry:scrape_javascript", False)
  1574. data = {
  1575. "timestamp": self.min_ago,
  1576. "message": "hello",
  1577. "platform": "javascript",
  1578. "release": "1.0",
  1579. "exception": {
  1580. "values": [
  1581. {
  1582. "type": "Error",
  1583. "stacktrace": {
  1584. "frames": [
  1585. {
  1586. "abs_path": "http://example.com/file.min.js",
  1587. "filename": "file.min.js",
  1588. "lineno": 1,
  1589. "colno": 39,
  1590. },
  1591. ]
  1592. },
  1593. }
  1594. ]
  1595. },
  1596. }
  1597. event = self.post_and_retrieve_event(data)
  1598. assert len(event.data["errors"]) == 1
  1599. assert event.data["errors"][0] == {
  1600. "type": "js_scraping_disabled",
  1601. "url": "http://example.com/file.min.js",
  1602. }
  1603. @requires_symbolicator
  1604. @pytest.mark.symbolicator
  1605. def test_expansion_with_debug_id(self, process_with_symbolicator):
  1606. project = self.project
  1607. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1608. release.add_project(project)
  1609. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1610. compressed = BytesIO(b"SYSB")
  1611. with zipfile.ZipFile(compressed, "a") as zip_file:
  1612. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1613. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1614. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1615. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1616. zip_file.writestr(
  1617. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  1618. )
  1619. zip_file.writestr(
  1620. "manifest.json",
  1621. json.dumps(
  1622. {
  1623. "org": self.organization.slug,
  1624. "release": release.version,
  1625. "files": {
  1626. "files/_/_/file.min.js": {
  1627. "url": "~/file.min.js",
  1628. "type": "minified_source",
  1629. "headers": {
  1630. "content-type": "application/json",
  1631. "debug-id": debug_id,
  1632. "sourcemap": "file.sourcemap.js",
  1633. },
  1634. },
  1635. "files/_/_/file1.js": {
  1636. "url": "~/file1.js",
  1637. "type": "source",
  1638. "headers": {
  1639. "content-type": "application/json",
  1640. },
  1641. },
  1642. "files/_/_/file2.js": {
  1643. "url": "~/file2.js",
  1644. "type": "source",
  1645. "headers": {
  1646. "content-type": "application/json",
  1647. },
  1648. },
  1649. "files/_/_/empty.js": {
  1650. "url": "~/empty.js",
  1651. "type": "source",
  1652. "headers": {
  1653. "content-type": "application/json",
  1654. },
  1655. },
  1656. "files/_/_/file.wc.sourcemap.js": {
  1657. "url": "~/file.wc.sourcemap.js",
  1658. "type": "source_map",
  1659. "headers": {
  1660. "content-type": "application/json",
  1661. "debug-id": debug_id,
  1662. },
  1663. },
  1664. },
  1665. }
  1666. ),
  1667. )
  1668. compressed.seek(0)
  1669. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1670. file.putfile(compressed)
  1671. # We want to also store the release files for this bundle, to check if they work together.
  1672. compressed.seek(0)
  1673. file_for_release = File.objects.create(name="bundle.zip", type="release.bundle")
  1674. file_for_release.putfile(compressed)
  1675. update_artifact_index(release, None, file_for_release)
  1676. artifact_bundle = ArtifactBundle.objects.create(
  1677. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1678. )
  1679. ProjectArtifactBundle.objects.create(
  1680. organization_id=self.organization.id,
  1681. project_id=self.project.id,
  1682. artifact_bundle=artifact_bundle,
  1683. )
  1684. DebugIdArtifactBundle.objects.create(
  1685. organization_id=self.organization.id,
  1686. debug_id=debug_id,
  1687. artifact_bundle=artifact_bundle,
  1688. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1689. )
  1690. DebugIdArtifactBundle.objects.create(
  1691. organization_id=self.organization.id,
  1692. debug_id=debug_id,
  1693. artifact_bundle=artifact_bundle,
  1694. source_file_type=SourceFileType.SOURCE_MAP.value,
  1695. )
  1696. data = {
  1697. "timestamp": self.min_ago,
  1698. "message": "hello",
  1699. "platform": "javascript",
  1700. "release": "abc",
  1701. "exception": {
  1702. "values": [
  1703. {
  1704. "type": "Error",
  1705. "stacktrace": {
  1706. "frames": [
  1707. {
  1708. "abs_path": "http://example.com/file.min.js",
  1709. "filename": "file.min.js",
  1710. "lineno": 1,
  1711. "colno": 39,
  1712. },
  1713. {
  1714. "abs_path": "http://example.com/file.min.js",
  1715. "filename": "file.min.js",
  1716. "lineno": 1,
  1717. "colno": 79,
  1718. },
  1719. # We want also to test the source without minification.
  1720. {
  1721. "abs_path": "http://example.com/file1.js",
  1722. "filename": "file1.js",
  1723. "lineno": 3,
  1724. "colno": 12,
  1725. },
  1726. ]
  1727. },
  1728. }
  1729. ]
  1730. },
  1731. "debug_meta": {
  1732. "images": [
  1733. {
  1734. "type": "sourcemap",
  1735. "debug_id": debug_id,
  1736. "code_file": "http://example.com/file.min.js",
  1737. }
  1738. ]
  1739. },
  1740. }
  1741. event = self.post_and_retrieve_event(data)
  1742. assert "errors" not in event.data
  1743. exception = event.interfaces["exception"]
  1744. frame_list = exception.values[0].stacktrace.frames
  1745. frame = frame_list[0]
  1746. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1747. assert frame.context_line == "\treturn a + b; // fôo"
  1748. if process_with_symbolicator:
  1749. assert frame.post_context == ["}"]
  1750. else:
  1751. assert frame.post_context == ["}", ""]
  1752. frame = frame_list[1]
  1753. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1754. assert frame.context_line == "\treturn a * b;"
  1755. assert frame.post_context == [
  1756. "}",
  1757. "function divide(a, b) {",
  1758. '\t"use strict";',
  1759. "\ttry {",
  1760. "\t\treturn multiply(add(a, b), a, b) / c;",
  1761. ]
  1762. frame = frame_list[2]
  1763. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1764. assert frame.context_line == "\treturn a + b; // fôo"
  1765. if process_with_symbolicator:
  1766. assert frame.post_context == ["}"]
  1767. else:
  1768. assert frame.post_context == ["}", ""]
  1769. @requires_symbolicator
  1770. @pytest.mark.symbolicator
  1771. def test_expansion_with_debug_id_and_sourcemap_without_sources_content(
  1772. self, process_with_symbolicator
  1773. ):
  1774. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1775. compressed = BytesIO(b"SYSB")
  1776. with zipfile.ZipFile(compressed, "a") as zip_file:
  1777. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1778. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1779. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1780. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1781. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  1782. zip_file.writestr(
  1783. "manifest.json",
  1784. json.dumps(
  1785. {
  1786. "files": {
  1787. "files/_/_/file.min.js": {
  1788. "url": "~/file.min.js",
  1789. "type": "minified_source",
  1790. "headers": {
  1791. "content-type": "application/json",
  1792. "debug-id": debug_id,
  1793. "sourcemap": "file.sourcemap.js",
  1794. },
  1795. },
  1796. "files/_/_/file1.js": {
  1797. "url": "~/file1.js",
  1798. "type": "source",
  1799. "headers": {
  1800. "content-type": "application/json",
  1801. },
  1802. },
  1803. "files/_/_/file2.js": {
  1804. "url": "~/file2.js",
  1805. "type": "source",
  1806. "headers": {
  1807. "content-type": "application/json",
  1808. },
  1809. },
  1810. "files/_/_/empty.js": {
  1811. "url": "~/empty.js",
  1812. "type": "source",
  1813. "headers": {
  1814. "content-type": "application/json",
  1815. },
  1816. },
  1817. "files/_/_/file.sourcemap.js": {
  1818. "url": "~/file.sourcemap.js",
  1819. "type": "source_map",
  1820. "headers": {
  1821. "content-type": "application/json",
  1822. "debug-id": debug_id,
  1823. },
  1824. },
  1825. }
  1826. }
  1827. ),
  1828. )
  1829. compressed.seek(0)
  1830. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1831. file.putfile(compressed)
  1832. artifact_bundle = ArtifactBundle.objects.create(
  1833. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1834. )
  1835. ProjectArtifactBundle.objects.create(
  1836. organization_id=self.organization.id,
  1837. project_id=self.project.id,
  1838. artifact_bundle=artifact_bundle,
  1839. )
  1840. DebugIdArtifactBundle.objects.create(
  1841. organization_id=self.organization.id,
  1842. debug_id=debug_id,
  1843. artifact_bundle=artifact_bundle,
  1844. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1845. )
  1846. DebugIdArtifactBundle.objects.create(
  1847. organization_id=self.organization.id,
  1848. debug_id=debug_id,
  1849. artifact_bundle=artifact_bundle,
  1850. source_file_type=SourceFileType.SOURCE_MAP.value,
  1851. )
  1852. data = {
  1853. "timestamp": self.min_ago,
  1854. "message": "hello",
  1855. "platform": "javascript",
  1856. "release": "abc",
  1857. "exception": {
  1858. "values": [
  1859. {
  1860. "type": "Error",
  1861. "stacktrace": {
  1862. "frames": [
  1863. {
  1864. "abs_path": "http://example.com/file.min.js",
  1865. "filename": "file.min.js",
  1866. "lineno": 1,
  1867. "colno": 39,
  1868. },
  1869. {
  1870. "abs_path": "http://example.com/file.min.js",
  1871. "filename": "file.min.js",
  1872. "lineno": 1,
  1873. "colno": 79,
  1874. },
  1875. ]
  1876. },
  1877. }
  1878. ]
  1879. },
  1880. "debug_meta": {
  1881. "images": [
  1882. {
  1883. "type": "sourcemap",
  1884. "debug_id": debug_id,
  1885. "code_file": "http://example.com/file.min.js",
  1886. }
  1887. ]
  1888. },
  1889. }
  1890. event = self.post_and_retrieve_event(data)
  1891. # NOTE: Symbolicator processor has a better fallback to pull specific files for source context
  1892. if process_with_symbolicator:
  1893. assert "errors" not in event.data
  1894. exception = event.interfaces["exception"]
  1895. frame_list = exception.values[0].stacktrace.frames
  1896. frame = frame_list[0]
  1897. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1898. assert frame.context_line == "\treturn a + b; // fôo"
  1899. if process_with_symbolicator:
  1900. assert frame.post_context == ["}"]
  1901. else:
  1902. assert frame.post_context == ["}", ""]
  1903. frame = frame_list[1]
  1904. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1905. assert frame.context_line == "\treturn a * b;"
  1906. assert frame.post_context == [
  1907. "}",
  1908. "function divide(a, b) {",
  1909. '\t"use strict";',
  1910. "\ttry {",
  1911. "\t\treturn multiply(add(a, b), a, b) / c;",
  1912. ]
  1913. else:
  1914. assert len(event.data["errors"]) == 3
  1915. assert event.data["errors"][0] == {
  1916. "type": "js_missing_sources_content",
  1917. "source": "http://example.com/file.min.js",
  1918. "sourcemap": f"debug-id://{debug_id}/~/file.sourcemap.js",
  1919. }
  1920. @requires_symbolicator
  1921. @pytest.mark.symbolicator
  1922. def test_expansion_with_debug_id_and_malformed_sourcemap(self, process_with_symbolicator):
  1923. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1924. compressed = BytesIO(b"SYSB")
  1925. with zipfile.ZipFile(compressed, "a") as zip_file:
  1926. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1927. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1928. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1929. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1930. zip_file.writestr(
  1931. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  1932. )
  1933. zip_file.writestr(
  1934. "manifest.json",
  1935. json.dumps(
  1936. {
  1937. "files": {
  1938. "files/_/_/file.min.js": {
  1939. "url": "~/file.min.js",
  1940. "type": "minified_source",
  1941. "headers": {
  1942. "content-type": "application/json",
  1943. "debug-id": debug_id,
  1944. "sourcemap": "file.malformed.sourcemap.js",
  1945. },
  1946. },
  1947. "files/_/_/file1.js": {
  1948. "url": "~/file1.js",
  1949. "type": "source",
  1950. "headers": {
  1951. "content-type": "application/json",
  1952. },
  1953. },
  1954. "files/_/_/file2.js": {
  1955. "url": "~/file2.js",
  1956. "type": "source",
  1957. "headers": {
  1958. "content-type": "application/json",
  1959. },
  1960. },
  1961. "files/_/_/empty.js": {
  1962. "url": "~/empty.js",
  1963. "type": "source",
  1964. "headers": {
  1965. "content-type": "application/json",
  1966. },
  1967. },
  1968. "files/_/_/file.malformed.sourcemap.js": {
  1969. "url": "~/file.malformed.sourcemap.js",
  1970. "type": "source_map",
  1971. "headers": {
  1972. "content-type": "application/json",
  1973. "debug-id": debug_id,
  1974. },
  1975. },
  1976. }
  1977. }
  1978. ),
  1979. )
  1980. compressed.seek(0)
  1981. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1982. file.putfile(compressed)
  1983. artifact_bundle = ArtifactBundle.objects.create(
  1984. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1985. )
  1986. ProjectArtifactBundle.objects.create(
  1987. organization_id=self.organization.id,
  1988. project_id=self.project.id,
  1989. artifact_bundle=artifact_bundle,
  1990. )
  1991. DebugIdArtifactBundle.objects.create(
  1992. organization_id=self.organization.id,
  1993. debug_id=debug_id,
  1994. artifact_bundle=artifact_bundle,
  1995. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1996. )
  1997. DebugIdArtifactBundle.objects.create(
  1998. organization_id=self.organization.id,
  1999. debug_id=debug_id,
  2000. artifact_bundle=artifact_bundle,
  2001. source_file_type=SourceFileType.SOURCE_MAP.value,
  2002. )
  2003. data = {
  2004. "timestamp": self.min_ago,
  2005. "message": "hello",
  2006. "platform": "javascript",
  2007. "release": "abc",
  2008. "exception": {
  2009. "values": [
  2010. {
  2011. "type": "Error",
  2012. "stacktrace": {
  2013. "frames": [
  2014. {
  2015. "abs_path": "http://example.com/file.min.js",
  2016. "filename": "file.min.js",
  2017. "lineno": 1,
  2018. "colno": 39,
  2019. },
  2020. {
  2021. "abs_path": "http://example.com/file.min.js",
  2022. "filename": "file.min.js",
  2023. "lineno": 1,
  2024. "colno": 79,
  2025. },
  2026. ]
  2027. },
  2028. }
  2029. ]
  2030. },
  2031. "debug_meta": {
  2032. "images": [
  2033. {
  2034. "type": "sourcemap",
  2035. "debug_id": debug_id,
  2036. "code_file": "http://example.com/file.min.js",
  2037. }
  2038. ]
  2039. },
  2040. }
  2041. event = self.post_and_retrieve_event(data)
  2042. assert len(event.data["errors"]) == 1
  2043. # NOTE: Its not obvious what data we want here yet.
  2044. if process_with_symbolicator:
  2045. assert event.data["errors"][0] == {
  2046. "type": "js_invalid_source",
  2047. "url": "http://example.com/file.malformed.sourcemap.js",
  2048. }
  2049. else:
  2050. assert event.data["errors"][0] == {
  2051. "type": "js_invalid_source",
  2052. "debug_id": f"debug-id://{debug_id}/~/file.malformed.sourcemap.js",
  2053. }
  2054. @requires_symbolicator
  2055. @pytest.mark.symbolicator
  2056. def test_expansion_with_debug_id_not_found(self, process_with_symbolicator):
  2057. project = self.project
  2058. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2059. release.add_project(project)
  2060. manifest = {
  2061. "org": self.organization.slug,
  2062. "release": release.version,
  2063. "files": {
  2064. "files/_/_/file.min.js": {
  2065. "url": "http://example.com/file.min.js",
  2066. "type": "minified_source",
  2067. },
  2068. "files/_/_/file1.js": {
  2069. "url": "http://example.com/file1.js",
  2070. "type": "source",
  2071. },
  2072. "files/_/_/file2.js": {
  2073. "url": "http://example.com/file2.js",
  2074. "type": "source",
  2075. },
  2076. "files/_/_/file.sourcemap.js": {
  2077. "url": "http://example.com/file.sourcemap.js",
  2078. "type": "source_map",
  2079. },
  2080. },
  2081. }
  2082. compressed = BytesIO(b"SYSB")
  2083. with zipfile.ZipFile(compressed, "a") as zip_file:
  2084. for rel_path, entry in manifest["files"].items():
  2085. name = os.path.basename(rel_path)
  2086. content = load_fixture(name)
  2087. if name == "file.min.js":
  2088. # Remove link to source map, add to header instead
  2089. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  2090. entry["headers"] = {"Sourcemap": "file.sourcemap.js"}
  2091. zip_file.writestr(rel_path, content)
  2092. zip_file.writestr("manifest.json", json.dumps(manifest))
  2093. compressed.seek(0)
  2094. file = File.objects.create(name="release_bundle.zip", type="release.bundle")
  2095. file.putfile(compressed)
  2096. update_artifact_index(release, None, file)
  2097. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2098. data = {
  2099. "timestamp": self.min_ago,
  2100. "message": "hello",
  2101. "platform": "javascript",
  2102. "release": "abc",
  2103. "exception": {
  2104. "values": [
  2105. {
  2106. "type": "Error",
  2107. "stacktrace": {
  2108. "frames": [
  2109. {
  2110. "abs_path": "http://example.com/file.min.js",
  2111. "filename": "file.min.js",
  2112. "lineno": 1,
  2113. "colno": 39,
  2114. },
  2115. {
  2116. "abs_path": "http://example.com/file.min.js",
  2117. "filename": "file.min.js",
  2118. "lineno": 1,
  2119. "colno": 79,
  2120. },
  2121. # We want also to test the source without minification.
  2122. {
  2123. "abs_path": "http://example.com/file1.js",
  2124. "filename": "file1.js",
  2125. "lineno": 3,
  2126. "colno": 12,
  2127. },
  2128. ]
  2129. },
  2130. }
  2131. ]
  2132. },
  2133. "debug_meta": {
  2134. "images": [
  2135. {
  2136. "type": "sourcemap",
  2137. "debug_id": debug_id,
  2138. "code_file": "http://example.com/file.min.js",
  2139. }
  2140. ]
  2141. },
  2142. }
  2143. event = self.post_and_retrieve_event(data)
  2144. exception = event.interfaces["exception"]
  2145. frame_list = exception.values[0].stacktrace.frames
  2146. frame = frame_list[0]
  2147. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2148. assert frame.context_line == "\treturn a + b; // fôo"
  2149. if process_with_symbolicator:
  2150. assert frame.post_context == ["}"]
  2151. else:
  2152. assert frame.post_context == ["}", ""]
  2153. frame = frame_list[1]
  2154. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2155. assert frame.context_line == "\treturn a * b;"
  2156. assert frame.post_context == [
  2157. "}",
  2158. "function divide(a, b) {",
  2159. '\t"use strict";',
  2160. "\ttry {",
  2161. "\t\treturn multiply(add(a, b), a, b) / c;",
  2162. ]
  2163. frame = frame_list[2]
  2164. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2165. assert frame.context_line == "\treturn a + b; // fôo"
  2166. if process_with_symbolicator:
  2167. assert frame.post_context == ["}"]
  2168. else:
  2169. assert frame.post_context == ["}", ""]
  2170. @requires_symbolicator
  2171. @pytest.mark.symbolicator
  2172. def test_expansion_with_release_dist_pair_x(self, process_with_symbolicator):
  2173. project = self.project
  2174. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2175. release.add_project(project)
  2176. dist = release.add_dist("android")
  2177. # We want to also add debug_id information inside the manifest but not in the stack trace to replicate a
  2178. # real edge case that we can incur in.
  2179. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2180. compressed = BytesIO(b"SYSB")
  2181. with zipfile.ZipFile(compressed, "a") as zip_file:
  2182. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2183. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2184. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2185. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2186. zip_file.writestr(
  2187. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  2188. )
  2189. zip_file.writestr(
  2190. "manifest.json",
  2191. json.dumps(
  2192. {
  2193. "files": {
  2194. "files/_/_/file.min.js": {
  2195. "url": "~/file.min.js",
  2196. "type": "minified_source",
  2197. "headers": {
  2198. "content-type": "application/json",
  2199. "sourcemap": "file.wc.sourcemap.js",
  2200. "debug-id": debug_id,
  2201. },
  2202. },
  2203. "files/_/_/file1.js": {
  2204. "url": "~/file1.js",
  2205. "type": "source",
  2206. "headers": {
  2207. "content-type": "application/json",
  2208. },
  2209. },
  2210. "files/_/_/file2.js": {
  2211. "url": "~/file2.js",
  2212. "type": "source",
  2213. "headers": {
  2214. "content-type": "application/json",
  2215. },
  2216. },
  2217. "files/_/_/empty.js": {
  2218. "url": "~/empty.js",
  2219. "type": "source",
  2220. "headers": {
  2221. "content-type": "application/json",
  2222. },
  2223. },
  2224. "files/_/_/file.wc.sourcemap.js": {
  2225. "url": "~/file.wc.sourcemap.js",
  2226. "type": "source_map",
  2227. "headers": {
  2228. "content-type": "application/json",
  2229. "debug-id": debug_id,
  2230. },
  2231. },
  2232. },
  2233. }
  2234. ),
  2235. )
  2236. compressed.seek(0)
  2237. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2238. file.putfile(compressed)
  2239. artifact_bundle = ArtifactBundle.objects.create(
  2240. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2241. )
  2242. ProjectArtifactBundle.objects.create(
  2243. organization_id=self.organization.id,
  2244. project_id=self.project.id,
  2245. artifact_bundle=artifact_bundle,
  2246. )
  2247. ReleaseArtifactBundle.objects.create(
  2248. organization_id=self.organization.id,
  2249. release_name=release.version,
  2250. dist_name=dist.name,
  2251. artifact_bundle=artifact_bundle,
  2252. )
  2253. data = {
  2254. "timestamp": self.min_ago,
  2255. "message": "hello",
  2256. "platform": "javascript",
  2257. "release": release.version,
  2258. "dist": dist.name,
  2259. "exception": {
  2260. "values": [
  2261. {
  2262. "type": "Error",
  2263. "stacktrace": {
  2264. "frames": [
  2265. {
  2266. "abs_path": "http://example.com/file.min.js",
  2267. "filename": "file.min.js",
  2268. "lineno": 1,
  2269. "colno": 39,
  2270. },
  2271. {
  2272. "abs_path": "http://example.com/file.min.js",
  2273. "filename": "file.min.js",
  2274. "lineno": 1,
  2275. "colno": 79,
  2276. },
  2277. # We want also to test the source without minification.
  2278. {
  2279. "abs_path": "http://example.com/file1.js",
  2280. "filename": "file1.js",
  2281. "lineno": 3,
  2282. "colno": 12,
  2283. },
  2284. ]
  2285. },
  2286. }
  2287. ]
  2288. },
  2289. }
  2290. event = self.post_and_retrieve_event(data)
  2291. assert "errors" not in event.data
  2292. exception = event.interfaces["exception"]
  2293. frame_list = exception.values[0].stacktrace.frames
  2294. frame = frame_list[0]
  2295. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2296. assert frame.context_line == "\treturn a + b; // fôo"
  2297. if process_with_symbolicator:
  2298. assert frame.post_context == ["}"]
  2299. else:
  2300. assert frame.post_context == ["}", ""]
  2301. frame = frame_list[1]
  2302. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2303. assert frame.context_line == "\treturn a * b;"
  2304. assert frame.post_context == [
  2305. "}",
  2306. "function divide(a, b) {",
  2307. '\t"use strict";',
  2308. "\ttry {",
  2309. "\t\treturn multiply(add(a, b), a, b) / c;",
  2310. ]
  2311. frame = frame_list[2]
  2312. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2313. assert frame.context_line == "\treturn a + b; // fôo"
  2314. if process_with_symbolicator:
  2315. assert frame.post_context == ["}"]
  2316. else:
  2317. assert frame.post_context == ["}", ""]
  2318. @requires_symbolicator
  2319. @pytest.mark.symbolicator
  2320. def test_expansion_with_release_dist_pair_and_sourcemap_without_sources_content(
  2321. self, process_with_symbolicator
  2322. ):
  2323. project = self.project
  2324. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2325. release.add_project(project)
  2326. dist = release.add_dist("android")
  2327. compressed = BytesIO(b"SYSB")
  2328. with zipfile.ZipFile(compressed, "a") as zip_file:
  2329. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2330. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2331. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2332. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2333. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  2334. zip_file.writestr(
  2335. "manifest.json",
  2336. json.dumps(
  2337. {
  2338. "files": {
  2339. "files/_/_/file.min.js": {
  2340. "url": "~/file.min.js",
  2341. "type": "minified_source",
  2342. "headers": {
  2343. "content-type": "application/json",
  2344. "sourcemap": "file.sourcemap.js",
  2345. },
  2346. },
  2347. "files/_/_/file1.js": {
  2348. "url": "~/file1.js",
  2349. "type": "source",
  2350. "headers": {
  2351. "content-type": "application/json",
  2352. },
  2353. },
  2354. "files/_/_/file2.js": {
  2355. "url": "~/file2.js",
  2356. "type": "source",
  2357. "headers": {
  2358. "content-type": "application/json",
  2359. },
  2360. },
  2361. "files/_/_/empty.js": {
  2362. "url": "~/empty.js",
  2363. "type": "source",
  2364. "headers": {
  2365. "content-type": "application/json",
  2366. },
  2367. },
  2368. "files/_/_/file.sourcemap.js": {
  2369. "url": "~/file.sourcemap.js",
  2370. "type": "source_map",
  2371. "headers": {
  2372. "content-type": "application/json",
  2373. },
  2374. },
  2375. }
  2376. }
  2377. ),
  2378. )
  2379. compressed.seek(0)
  2380. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2381. file.putfile(compressed)
  2382. artifact_bundle = ArtifactBundle.objects.create(
  2383. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2384. )
  2385. ProjectArtifactBundle.objects.create(
  2386. organization_id=self.organization.id,
  2387. project_id=self.project.id,
  2388. artifact_bundle=artifact_bundle,
  2389. )
  2390. ReleaseArtifactBundle.objects.create(
  2391. organization_id=self.organization.id,
  2392. release_name=release.version,
  2393. dist_name=dist.name,
  2394. artifact_bundle=artifact_bundle,
  2395. )
  2396. data = {
  2397. "timestamp": self.min_ago,
  2398. "message": "hello",
  2399. "platform": "javascript",
  2400. "release": release.version,
  2401. "dist": dist.name,
  2402. "exception": {
  2403. "values": [
  2404. {
  2405. "type": "Error",
  2406. "stacktrace": {
  2407. "frames": [
  2408. {
  2409. "abs_path": "http://example.com/file.min.js",
  2410. "filename": "file.min.js",
  2411. "lineno": 1,
  2412. "colno": 39,
  2413. },
  2414. {
  2415. "abs_path": "http://example.com/file.min.js",
  2416. "filename": "file.min.js",
  2417. "lineno": 1,
  2418. "colno": 79,
  2419. },
  2420. ]
  2421. },
  2422. }
  2423. ]
  2424. },
  2425. }
  2426. event = self.post_and_retrieve_event(data)
  2427. # NOTE: Symbolicator processor has a better fallback to pull specific files for source context
  2428. if process_with_symbolicator:
  2429. assert "errors" not in event.data
  2430. exception = event.interfaces["exception"]
  2431. frame_list = exception.values[0].stacktrace.frames
  2432. frame = frame_list[0]
  2433. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2434. assert frame.context_line == "\treturn a + b; // fôo"
  2435. if process_with_symbolicator:
  2436. assert frame.post_context == ["}"]
  2437. else:
  2438. assert frame.post_context == ["}", ""]
  2439. frame = frame_list[1]
  2440. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2441. assert frame.context_line == "\treturn a * b;"
  2442. assert frame.post_context == [
  2443. "}",
  2444. "function divide(a, b) {",
  2445. '\t"use strict";',
  2446. "\ttry {",
  2447. "\t\treturn multiply(add(a, b), a, b) / c;",
  2448. ]
  2449. else:
  2450. assert len(event.data["errors"]) == 3
  2451. assert event.data["errors"][0] == {
  2452. "type": "js_missing_sources_content",
  2453. "source": "http://example.com/file.min.js",
  2454. "sourcemap": "http://example.com/file.sourcemap.js",
  2455. }
  2456. @requires_symbolicator
  2457. @pytest.mark.symbolicator
  2458. def test_expansion_with_release_and_malformed_sourcemap(self, process_with_symbolicator):
  2459. project = self.project
  2460. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2461. release.add_project(project)
  2462. compressed = BytesIO(b"SYSB")
  2463. with zipfile.ZipFile(compressed, "a") as zip_file:
  2464. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2465. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2466. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2467. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2468. zip_file.writestr(
  2469. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  2470. )
  2471. zip_file.writestr(
  2472. "manifest.json",
  2473. json.dumps(
  2474. {
  2475. "files": {
  2476. "files/_/_/file.min.js": {
  2477. "url": "~/file.min.js",
  2478. "type": "minified_source",
  2479. "headers": {
  2480. "content-type": "application/json",
  2481. "sourcemap": "file.malformed.sourcemap.js",
  2482. },
  2483. },
  2484. "files/_/_/file1.js": {
  2485. "url": "~/file1.js",
  2486. "type": "source",
  2487. "headers": {
  2488. "content-type": "application/json",
  2489. },
  2490. },
  2491. "files/_/_/file2.js": {
  2492. "url": "~/file2.js",
  2493. "type": "source",
  2494. "headers": {
  2495. "content-type": "application/json",
  2496. },
  2497. },
  2498. "files/_/_/empty.js": {
  2499. "url": "~/empty.js",
  2500. "type": "source",
  2501. "headers": {
  2502. "content-type": "application/json",
  2503. },
  2504. },
  2505. "files/_/_/file.malformed.sourcemap.js": {
  2506. "url": "~/file.malformed.sourcemap.js",
  2507. "type": "source_map",
  2508. "headers": {
  2509. "content-type": "application/json",
  2510. },
  2511. },
  2512. }
  2513. }
  2514. ),
  2515. )
  2516. compressed.seek(0)
  2517. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2518. file.putfile(compressed)
  2519. artifact_bundle = ArtifactBundle.objects.create(
  2520. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2521. )
  2522. ProjectArtifactBundle.objects.create(
  2523. organization_id=self.organization.id,
  2524. project_id=self.project.id,
  2525. artifact_bundle=artifact_bundle,
  2526. )
  2527. ReleaseArtifactBundle.objects.create(
  2528. organization_id=self.organization.id,
  2529. release_name=release.version,
  2530. artifact_bundle=artifact_bundle,
  2531. )
  2532. data = {
  2533. "timestamp": self.min_ago,
  2534. "message": "hello",
  2535. "platform": "javascript",
  2536. "release": release.version,
  2537. "exception": {
  2538. "values": [
  2539. {
  2540. "type": "Error",
  2541. "stacktrace": {
  2542. "frames": [
  2543. {
  2544. "abs_path": "http://example.com/file.min.js",
  2545. "filename": "file.min.js",
  2546. "lineno": 1,
  2547. "colno": 39,
  2548. },
  2549. {
  2550. "abs_path": "http://example.com/file.min.js",
  2551. "filename": "file.min.js",
  2552. "lineno": 1,
  2553. "colno": 79,
  2554. },
  2555. ]
  2556. },
  2557. }
  2558. ]
  2559. },
  2560. }
  2561. event = self.post_and_retrieve_event(data)
  2562. assert len(event.data["errors"]) == 1
  2563. assert event.data["errors"][0] == {
  2564. "type": "js_invalid_source",
  2565. "url": "http://example.com/file.malformed.sourcemap.js",
  2566. }