test_plugin.py 94 KB

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