test_plugin.py 95 KB

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