test_plugin.py 95 KB

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