test_plugin.py 97 KB

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