test_plugin.py 95 KB

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