|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +jest.mock("../LineageUtils", () => ({ default: {} })); |
| 19 | +jest.mock("../DataUtils", () => ({ default: {} })); |
| 20 | + |
| 21 | +import { escapeHtml } from "../index"; |
| 22 | + |
| 23 | +/** |
| 24 | + * Mirrors underscore _.escape used in classic UI: |
| 25 | + * - atlas-lineage (dashboardv2/external_lib) |
| 26 | + * - ProfileBarChart.js |
| 27 | + */ |
| 28 | +const underscoreEscape = (value: unknown): string => { |
| 29 | + if (value == null) { |
| 30 | + return ""; |
| 31 | + } |
| 32 | + |
| 33 | + const escapeMap: Record<string, string> = { |
| 34 | + "&": "&", |
| 35 | + "<": "<", |
| 36 | + ">": ">", |
| 37 | + '"': """, |
| 38 | + "'": "'", |
| 39 | + "`": "`" |
| 40 | + }; |
| 41 | + |
| 42 | + return String(value).replace(/[&<>"'`]/g, (match) => escapeMap[match]); |
| 43 | +}; |
| 44 | + |
| 45 | +const expectSafeTooltipText = (encoded: string) => { |
| 46 | + expect(encoded).not.toMatch(/<[^&!]/); |
| 47 | + expect(encoded).not.toContain("<script"); |
| 48 | + expect(encoded).not.toContain("<img"); |
| 49 | +}; |
| 50 | + |
| 51 | +describe("lineage tooltip escapeHtml (React)", () => { |
| 52 | + describe("HTML metacharacter encoding", () => { |
| 53 | + it("escapes angle brackets in malicious tooltip labels", () => { |
| 54 | + const payload = '<img src=x onerror="window.__xss=1">'; |
| 55 | + const escaped = escapeHtml(payload); |
| 56 | + |
| 57 | + expect(escaped).not.toContain("<img"); |
| 58 | + expect(escaped).toContain("<img"); |
| 59 | + expect(escaped).toContain("""); |
| 60 | + expectSafeTooltipText(escaped); |
| 61 | + }); |
| 62 | + |
| 63 | + it("escapes script tags", () => { |
| 64 | + const escaped = escapeHtml('<script>alert("x")</script>'); |
| 65 | + expect(escaped).toBe("<script>alert("x")</script>"); |
| 66 | + expectSafeTooltipText(escaped); |
| 67 | + }); |
| 68 | + |
| 69 | + it("escapes ampersand", () => { |
| 70 | + expect(escapeHtml("a & b")).toBe("a & b"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("double-encodes already-encoded ampersand entities safely", () => { |
| 74 | + expect(escapeHtml("a & b")).toBe("a &amp; b"); |
| 75 | + expectSafeTooltipText(escapeHtml("a & b")); |
| 76 | + }); |
| 77 | + |
| 78 | + it("escapes single quotes", () => { |
| 79 | + expect(escapeHtml("O'Brien")).toBe("O'Brien"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("escapes double quotes", () => { |
| 83 | + expect(escapeHtml('say "hello"')).toBe("say "hello""); |
| 84 | + }); |
| 85 | + |
| 86 | + it("escapes all special characters in one payload", () => { |
| 87 | + const payload = `<a href='x'>&"test"`; |
| 88 | + const escaped = escapeHtml(payload); |
| 89 | + expect(escaped).toBe("<a href='x'>&"test""); |
| 90 | + expectSafeTooltipText(escaped); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("nullish and empty inputs", () => { |
| 95 | + it("handles null and undefined values", () => { |
| 96 | + expect(escapeHtml(null)).toBe(""); |
| 97 | + expect(escapeHtml(undefined)).toBe(""); |
| 98 | + }); |
| 99 | + |
| 100 | + it("handles empty string", () => { |
| 101 | + expect(escapeHtml("")).toBe(""); |
| 102 | + }); |
| 103 | + |
| 104 | + it("handles whitespace-only label used when displayText is missing", () => { |
| 105 | + expect(escapeHtml(" ")).toBe(" "); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("numeric and coerced values", () => { |
| 110 | + it("coerces numeric values to string", () => { |
| 111 | + expect(escapeHtml(123)).toBe("123"); |
| 112 | + expect(escapeHtml(0)).toBe("0"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("coerces boolean values to string", () => { |
| 116 | + expect(escapeHtml(true)).toBe("true"); |
| 117 | + expect(escapeHtml(false)).toBe("false"); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe("plain text preservation", () => { |
| 122 | + it("preserves safe entity display names", () => { |
| 123 | + expect(escapeHtml("sales_db.customers")).toBe("sales_db.customers"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("preserves qualified names with dots and underscores", () => { |
| 127 | + expect(escapeHtml("hive.db.table_v1")).toBe("hive.db.table_v1"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("preserves unicode characters in entity names", () => { |
| 131 | + expect(escapeHtml("データ_テーブル")).toBe("データ_テーブル"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("preserves newlines in multiline query text", () => { |
| 135 | + const queryText = "SELECT *\nFROM t\nWHERE id = 1"; |
| 136 | + expect(escapeHtml(queryText)).toBe(queryText); |
| 137 | + expectSafeTooltipText(escapeHtml(queryText)); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("lineage tooltip field scenarios", () => { |
| 142 | + it("encodes toolTipLabel from entity displayText", () => { |
| 143 | + const label = 'db.table<script>alert(1)</script>'; |
| 144 | + const escaped = escapeHtml(label); |
| 145 | + expect(escaped).toBe("db.table<script>alert(1)</script>"); |
| 146 | + expectSafeTooltipText(escaped); |
| 147 | + }); |
| 148 | + |
| 149 | + it("encodes typeName values", () => { |
| 150 | + const escaped = escapeHtml("hive_table<script>"); |
| 151 | + expect(escaped).toBe("hive_table<script>"); |
| 152 | + expectSafeTooltipText(escaped); |
| 153 | + }); |
| 154 | + |
| 155 | + it("encodes queryText with SQL quotes and comparison operators", () => { |
| 156 | + const queryText = |
| 157 | + "SELECT * FROM t WHERE x < 1 AND name = \"foo\" AND y > 0"; |
| 158 | + const escaped = escapeHtml(queryText); |
| 159 | + |
| 160 | + expect(escaped).toContain("<"); |
| 161 | + expect(escaped).toContain(">"); |
| 162 | + expect(escaped).toContain("""); |
| 163 | + expectSafeTooltipText(escaped); |
| 164 | + }); |
| 165 | + |
| 166 | + it("encodes static toolTipTitle values", () => { |
| 167 | + expect(escapeHtml("Type")).toBe("Type"); |
| 168 | + expect(escapeHtml('Type<script>')).toBe("Type<script>"); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe("intentional differences from classic _.escape", () => { |
| 173 | + it("does not encode backtick in text content (safe inside span tags)", () => { |
| 174 | + expect(escapeHtml("db`table")).toBe("db`table"); |
| 175 | + expectSafeTooltipText(escapeHtml("db`table")); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +describe("classic UI tooltip encoding (underscore _.escape parity)", () => { |
| 181 | + const tooltipPayloads = [ |
| 182 | + '<img src=x onerror="alert(1)">', |
| 183 | + "O'Brien", |
| 184 | + "a & b", |
| 185 | + "sales_db.customers", |
| 186 | + 123, |
| 187 | + "", |
| 188 | + null, |
| 189 | + undefined |
| 190 | + ]; |
| 191 | + |
| 192 | + it.each(tooltipPayloads)( |
| 193 | + "underscoreEscape produces safe output for %p", |
| 194 | + (payload) => { |
| 195 | + const escaped = underscoreEscape(payload); |
| 196 | + |
| 197 | + if (payload == null) { |
| 198 | + expect(escaped).toBe(""); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + expectSafeTooltipText(escaped); |
| 203 | + |
| 204 | + const raw = String(payload); |
| 205 | + if (/[&<>"'`]/.test(raw)) { |
| 206 | + expect(escaped).not.toBe(raw); |
| 207 | + } else { |
| 208 | + expect(escaped).toBe(raw); |
| 209 | + } |
| 210 | + } |
| 211 | + ); |
| 212 | + |
| 213 | + it("handles null and undefined like production _.escape", () => { |
| 214 | + expect(underscoreEscape(null)).toBe(""); |
| 215 | + expect(underscoreEscape(undefined)).toBe(""); |
| 216 | + }); |
| 217 | + |
| 218 | + it("preserves numeric count values for profile bar chart tooltips", () => { |
| 219 | + expect(underscoreEscape(42)).toBe("42"); |
| 220 | + expect(underscoreEscape(0)).toBe("0"); |
| 221 | + }); |
| 222 | + |
| 223 | + it("encodes lineage queryText with quotes and angle brackets", () => { |
| 224 | + const queryText = 'SELECT * FROM t WHERE x < 1 AND name = "foo"'; |
| 225 | + const escaped = underscoreEscape(queryText); |
| 226 | + |
| 227 | + expect(escaped).toContain("<"); |
| 228 | + expect(escaped).toContain("""); |
| 229 | + expectSafeTooltipText(escaped); |
| 230 | + }); |
| 231 | + |
| 232 | + it("encodes lineage typeName values", () => { |
| 233 | + const escaped = underscoreEscape("hive_table<script>"); |
| 234 | + expect(escaped).toBe("hive_table<script>"); |
| 235 | + expectSafeTooltipText(escaped); |
| 236 | + }); |
| 237 | + |
| 238 | + it("encodes backtick characters used in entity names", () => { |
| 239 | + expect(underscoreEscape("db`table")).toBe("db`table"); |
| 240 | + }); |
| 241 | + |
| 242 | + it("react escapeHtml and classic _.escape both produce safe tooltip output", () => { |
| 243 | + const payloads = [ |
| 244 | + '<img src=x onerror="alert(1)">', |
| 245 | + '<script>alert(1)</script>', |
| 246 | + "O'Brien", |
| 247 | + "a & b", |
| 248 | + "hive_process", |
| 249 | + 'SELECT * FROM t WHERE x < 1 AND name = "foo"', |
| 250 | + "hive_table<script>" |
| 251 | + ]; |
| 252 | + |
| 253 | + payloads.forEach((payload) => { |
| 254 | + expectSafeTooltipText(escapeHtml(payload)); |
| 255 | + expectSafeTooltipText(underscoreEscape(payload)); |
| 256 | + }); |
| 257 | + }); |
| 258 | +}); |
0 commit comments