Files
ShakaPlayer/lib/dash/json_utils.js
Adil Burak Şen b9b313fd1b fix(dash-json): XML-escape text content and attributes in jsonToMpd (#10131)
## Problem

`JsonUtils.jsonToMpd()` in `lib/dash/json_utils.js` does not XML-escape
string values when building the MPD XML string. The `emit()` helper
calls `StringUtils.htmlUnescape()` on both attribute values (line 38)
and text content (lines 61, 120) — the wrong direction. `htmlUnescape()`
converts HTML entities back to raw characters but does **not** escape
raw `<`, `>`, or `"` characters.

This means any JSON string value that contains XML metacharacters is
embedded verbatim into the XML output and interpreted as markup by the
parser.

**Example — BaseURL injection:**

```json
{
  "Period": [{
    "AdaptationSet": [{
      "BaseURL": "</BaseURL><BaseURL>http://attacker.example/segments/"
    }]
  }]
}
```

`emit('BaseURL', {}, ...)` produces:

```xml
<BaseURL></BaseURL><BaseURL>http://attacker.example/segments/</BaseURL>
```

`dash_parser.js` collects **all** `BaseURL` children via
`TXml.findChildren(elem, 'BaseURL')` and adds every resolved URI to
`lastCalculatedBaseUris_`. An attacker who controls the manifest URL can
redirect segment requests to an arbitrary server.

The same injection path applies to `ContentProtection` and other child
elements, and to attribute values containing `"`.

## Fix

Replace the three `htmlUnescape()` calls with a local `xmlEscape()`
function that encodes `&`, `<`, `>`, and `"` as XML character
references. Remove the now-unused `StringUtils` import.

## Tests

Added two regression tests to `test/dash/json_utils_unit.js`:
- Text content injection: verifies that a `BaseURL` value with
`</BaseURL><BaseURL>http://attacker.example/` produces exactly **one**
`BaseURL` element with the literal string as content, not two parsed
elements.
- Attribute value injection: verifies that a `Period` `id` attribute
containing `"` does not break out of the attribute context.
2026-07-12 10:04:09 -07:00

214 lines
6.0 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// cspell:ignore subk subv
goog.provide('shaka.dash.JsonUtils');
/**
* @summary JSON processing utility functions.
*/
shaka.dash.JsonUtils = class {
/**
* Converts a DASH-JSON manifest (dash-json-schema) into an MPD XML string.
*
* @param {!Object} json - Root JSON object following dash-json-schema.
* @return {string}
*/
static jsonToMpd(json) {
const MPD_NS = 'urn:mpeg:dash:schema:mpd:2011';
/**
* XML-escapes a string for safe use in XML text or attribute values.
*
* @param {*} s
* @return {string}
*/
function xmlEscape(s) {
return String(s)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
/**
* Emit a tag with attributes and content.
*
* @param {string} tagName
* @param {!Object<string, ?>} attrs
* @param {string} inner
* @return {string}
*/
function emit(tagName, attrs, inner) {
let out = '<' + tagName;
for (const [k, v] of Object.entries(attrs)) {
out += ` ${k}="${xmlEscape(v)}"`;
}
if (inner === '') {
return out + '/>';
}
return out + '>' + inner + `</${tagName}>`;
}
/**
* Recursively build XML for a JSON node.
*
* @param {string} key Element name, WITHOUT prefix
* @param {*} node Value
* @param {!Object} nsMap Map prefix->URI
* @return {string}
*/
function buildNode(key, node, nsMap) {
if (node == null) {
return emit(key, {}, '');
}
// Primitive → <key>value</key>
if (typeof node !== 'object') {
return emit(key, {}, xmlEscape(String(node)));
}
// Object case
const attrs = {};
let textContent = '';
const children = [];
// Handle prefix groups (namespaced stuff)
const obj = /** @type {!Object<string,?>} */ (node);
for (const [k, v] of Object.entries(obj)) {
if (k === '$value' || k === '$ns') {
continue;
}
const isPrefixGroup = nsMap[k] !== undefined;
if (isPrefixGroup && typeof v === 'object' && v != null) {
const prefix = k;
const groupObj = v;
for (const [subk, subv] of Object.entries(groupObj)) {
if (Array.isArray(subv)) {
// Namespaced repeating child elements <prefix:subk>
for (const item of subv) {
const tag = `${prefix}:${subk}`;
children.push(buildNode(tag, item, nsMap));
}
} else if (typeof subv === 'object') {
// Single namespaced child element
const tag = `${prefix}:${subk}`;
children.push(buildNode(tag, subv, nsMap));
} else {
// Namespaced attribute prefix:attr
attrs[`${prefix}:${subk}`] = subv;
}
}
continue;
}
// Regular attribute / child element
const val = v;
const isObj = typeof val === 'object' && val != null;
if (!isObj) {
// scalar → attribute
attrs[k] = val;
} else {
// object or array → child
if (Array.isArray(val)) {
for (const item of val) {
children.push(buildNode(k, item, nsMap));
}
} else {
children.push(buildNode(k, val, nsMap));
}
}
}
if (node['$value'] !== undefined && node['$value'] !== null) {
textContent = xmlEscape(node['$value']);
}
return emit(key, attrs, textContent + children.join(''));
}
// ------------------------------------------------------------------
// Build namespace map prefix→URI from $ns
// ------------------------------------------------------------------
const nsMap = {};
if (json['$ns'] && typeof json['$ns'] === 'object') {
for (const [uri, info] of Object.entries(json['$ns'])) {
const prefix = info.prefix;
nsMap[prefix] = uri;
}
}
// ------------------------------------------------------------------
// Build root attributes: xmlns + xmlns:pref + scalar attrs at root
// ------------------------------------------------------------------
const rootAttrs = {
'xmlns': MPD_NS,
};
// nsMap: prefix→uri
for (const [prefix, uri] of Object.entries(nsMap)) {
rootAttrs[`xmlns:${prefix}`] = uri;
}
// Non-object root attributes
for (const [k, v] of Object.entries(json)) {
if (k === '$ns') {
continue;
}
if (typeof v !== 'object' || v === null) {
rootAttrs[k] = v;
}
}
// ------------------------------------------------------------------
// Build children of <MPD>
// ------------------------------------------------------------------
let mpdInner = '';
for (const [k, v] of Object.entries(json)) {
if (k === '$ns') {
continue;
}
if (k in nsMap) {
// Namespace group
const groupObj = /** @type {!Object<string,?>} */ (v);
for (const [subk, subv] of Object.entries(groupObj)) {
if (Array.isArray(subv)) {
for (const item of subv) {
const tag = `${k}:${subk}`;
mpdInner += buildNode(tag, item, nsMap);
}
} else if (typeof subv === 'object') {
const tag = `${k}:${subk}`;
mpdInner += buildNode(tag, subv, nsMap);
} else {
// namespaced attribute on root? (rare)
rootAttrs[`${k}:${subk}`] = subv;
}
}
continue;
}
if (typeof v === 'object' && v !== null) {
if (Array.isArray(v)) {
for (const item of v) {
mpdInner += buildNode(k, item, nsMap);
}
} else {
mpdInner += buildNode(k, v, nsMap);
}
}
}
return emit('MPD', rootAttrs, mpdInner);
}
};