|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { byteLength } = require('internal/buffer_util'); |
| 4 | + |
| 5 | +exports.utf8Slice = (buf, start, end) => { |
| 6 | + return slice(buf, start, end, 'utf8Slice'); |
| 7 | +}; |
| 8 | + |
| 9 | +exports.asciiSlice = (buf, start, end) => { |
| 10 | + return slice(buf, start, end, 'asciiSlice'); |
| 11 | +}; |
| 12 | + |
| 13 | +exports.ucs2Slice = (buf, start, end) => { |
| 14 | + return slice(buf, start, end, 'ucs2Slice'); |
| 15 | +}; |
| 16 | + |
| 17 | +exports.latin1Slice = (buf, start, end) => { |
| 18 | + return slice(buf, start, end, 'latin1Slice'); |
| 19 | +}; |
| 20 | + |
| 21 | +exports.hexSlice = (buf, start, end) => { |
| 22 | + return slice(buf, start, end, 'hexSlice'); |
| 23 | +}; |
| 24 | + |
| 25 | +function slice(buf, start, end, encodingSlice) { |
| 26 | + let result = ''; |
| 27 | + let i = start; |
| 28 | + for (const taint of getSubtaint(buf._taint, start, end)) { |
| 29 | + result += buf[encodingSlice](i, taint.begin); |
| 30 | + result += buf[encodingSlice](taint.begin, taint.end).setTaint('buffer'); |
| 31 | + i = taint.end; |
| 32 | + } |
| 33 | + result += buf[encodingSlice](i, end); |
| 34 | + return result; |
| 35 | +} |
| 36 | + |
| 37 | +function getSubtaint(taint, begin, end) { |
| 38 | + const result = []; |
| 39 | + for (var i in taint) { |
| 40 | + const range = taint[i]; |
| 41 | + if (range.end < begin || range.begin > end) { |
| 42 | + continue; |
| 43 | + } else if (range.begin >= begin && range.end <= end) { |
| 44 | + result.push({ begin: range.begin, end: range.end }); |
| 45 | + } else if (range.begin < begin && range.end <= end) { |
| 46 | + result.push({ begin: begin, end: range.end }); |
| 47 | + } else if (range.begin < begin && range.end > end) { |
| 48 | + result.push({ begin: begin, end: end }); |
| 49 | + } else if (range.begin >= begin && range.end > end) { |
| 50 | + result.push({ begin: range.begin, end: end }); |
| 51 | + } |
| 52 | + } |
| 53 | + return result; |
| 54 | +} |
| 55 | + |
| 56 | +exports.applyTaintToBuffer = applyTaintToBuffer; |
| 57 | + |
| 58 | +function applyTaintToBuffer(buf, val, start, end, encoding) { |
| 59 | + |
| 60 | + buf._taint = []; |
| 61 | + |
| 62 | + if (typeof val !== 'string') return buf; |
| 63 | + |
| 64 | + for (const taint of val.getTaint()) { |
| 65 | + const offset = byteLength(val.slice(0, taint.begin) |
| 66 | + , encoding); |
| 67 | + const offsetEnd = offset + byteLength(val.slice(taint.begin |
| 68 | + , taint.end), encoding); |
| 69 | + const helpTaint = [{ begin: offset, end: offsetEnd }]; |
| 70 | + buf._taint.push(getSubtaint(helpTaint, start, end)[0]); |
| 71 | + } |
| 72 | + return buf; |
| 73 | +} |
| 74 | + |
| 75 | +exports.ucs2Write = (buf, string, offset, length) => { |
| 76 | + return write(buf, string, offset, length, 'utf16le', 'ucs2Write'); |
| 77 | +}; |
| 78 | + |
| 79 | +exports.utf8Write = (buf, string, offset, length) => { |
| 80 | + return write(buf, string, offset, length, 'utf8', 'utf8Write'); |
| 81 | +}; |
| 82 | + |
| 83 | +exports.asciiWrite = (buf, string, offset, length) => { |
| 84 | + return write(buf, string, offset, length, 'ascii', 'asciiWrite'); |
| 85 | +}; |
| 86 | + |
| 87 | +function write(buf, string, offset, length, encoding, encodingSlice) { |
| 88 | + const result = buf[encodingSlice](string, offset, length); |
| 89 | + applyTaintToPartialBuffer(buf, string, offset, length, encoding); |
| 90 | + return result; |
| 91 | +} |
| 92 | + |
| 93 | +// exports.applyTaintToPartialBuffer = applyTaintToPartialBuffer; |
| 94 | + |
| 95 | +function applyTaintToPartialBuffer(buf, string, offset, length, encoding) { |
| 96 | + |
| 97 | + const end = offset + length; |
| 98 | + const taintResult = []; |
| 99 | + |
| 100 | + // Step 1: Keep taint before string range to be inserted. |
| 101 | + for (const taint of buf._taint) { |
| 102 | + if (taint.end < offset) { |
| 103 | + taintResult.push(taint); |
| 104 | + } else if (taint.begin < offset && taint.end >= offset) { |
| 105 | + const helpTaint = [{ begin: taint.begin, end: offset - 1 }]; |
| 106 | + taintResult.push(helpTaint); |
| 107 | + } else { |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + // Step 2: Keep taint from string |
| 112 | + for (const taint of string.getTaint()) { |
| 113 | + const taintBegin = byteLength(string.slice(0, taint.begin) |
| 114 | + , encoding); |
| 115 | + const taintEnd = byteLength(string.slice(taint.begin, taint.end) |
| 116 | + , encoding); |
| 117 | + const helpTaint = [{ begin: offset + taintBegin, |
| 118 | + end: offset + taintEnd }]; |
| 119 | + taintResult.push(getSubtaint(helpTaint, offset, end)[0]); |
| 120 | + } |
| 121 | + // Step 3: Keep taint after string range to be inserted. |
| 122 | + for (const taint of buf._taint) { |
| 123 | + if (taint.end <= end) { |
| 124 | + continue; |
| 125 | + } else if (taint.begin <= end && taint.end > end) { |
| 126 | + const helpTaint = [{ begin: end + 1, end: taint.end }]; |
| 127 | + taintResult.push(helpTaint); |
| 128 | + } else if (taint.begin > end) { |
| 129 | + taintResult.push(taint); |
| 130 | + } |
| 131 | + } |
| 132 | + // Save the result |
| 133 | + buf._taint = taintResult; |
| 134 | +} |
| 135 | + |
| 136 | +exports.concatBufferArrayTaint = concatBufferArrayTaint; |
| 137 | + |
| 138 | +function concatBufferArrayTaint(list) { |
| 139 | + |
| 140 | + const taintResult = []; |
| 141 | + var offset = 0; |
| 142 | + |
| 143 | + for (const buffer of list) { |
| 144 | + if (buffer && buffer._taint) { |
| 145 | + for (const taint of buffer._taint) { |
| 146 | + taintResult.push({ begin: offset + taint.begin, |
| 147 | + end: offset + taint.end }); |
| 148 | + } |
| 149 | + } |
| 150 | + offset += buffer.length; |
| 151 | + } |
| 152 | + return taintResult; |
| 153 | +} |
0 commit comments