How to use the escapeHeaderAttribute function from hoek

Find comprehensive JavaScript hoek.escapeHeaderAttribute code examples handpicked from public code repositorys.

hoek.escapeHeaderAttribute is a function in the Hoek library that escapes special characters in a string that is used as an HTTP header attribute.

116
117
118
119
120
121
122
123
124
125
var hasExt = artifacts.ext !== null && artifacts.ext !== undefined && artifacts.ext !== '';       // Other falsey values allowed
var header = 'Hawk id="' + credentials.id +
             '", ts="' + artifacts.ts +
             '", nonce="' + artifacts.nonce +
             (artifacts.hash ? '", hash="' + artifacts.hash : '') +
             (hasExt ? '", ext="' + Hoek.escapeHeaderAttribute(artifacts.ext) : '') +
             '", mac="' + mac + '"';

if (artifacts.app) {
    header += ', app="' + artifacts.app +
fork icon1
star icon0
watch icon1

113
114
115
116
117
118
119
120
121
122
var mac = Crypto.calculateMac('header', credentials, artifacts);

// Construct header

var hasExt = artifacts.ext !== null && artifacts.ext !== undefined && artifacts.ext !== ''; // Other falsey values allowed
var header = 'Hawk id="' + credentials.id + '", ts="' + artifacts.ts + '", nonce="' + artifacts.nonce + (artifacts.hash ? '", hash="' + artifacts.hash : '') + (hasExt ? '", ext="' + Hoek.escapeHeaderAttribute(artifacts.ext) : '') + '", mac="' + mac + '"';

if (artifacts.app) {
    header = header + ', app="' + artifacts.app + (artifacts.dlg ? '", dlg="' + artifacts.dlg : '') + '"';
}
fork icon1
star icon0
watch icon1

How does hoek.escapeHeaderAttribute work?

In the Hoek library, hoek.escapeHeaderAttribute is a function that escapes special characters in a string that is used as an HTTP header attribute. HTTP headers are used to provide additional information about an HTTP request or response, such as the content type or the server's version number. Header values may contain special characters that could be misinterpreted or cause security vulnerabilities if they are not properly escaped. When you call hoek.escapeHeaderAttribute with a string that is used as an HTTP header attribute, it escapes any characters that have special meaning in HTTP headers using the percent-encoding scheme. This involves replacing each special character with a percent sign followed by its ASCII code in hexadecimal. For example, if you pass the string This is a test header value!"#$%&'()*+,-./:;?@[\\]^_{|}~tohoek.escapeHeaderAttribute, it will escape the special characters so that the resulting string is This is a test header value%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D%7E`. Overall, hoek.escapeHeaderAttribute is a simple but important function in the Hoek library that ensures that HTTP header attributes are properly escaped, making it easier to write secure and reliable HTTP clients and servers.

293
294
295
296
297
298
299
300
301
302
303
304
305


    if (artifacts.ext !== null &&
        artifacts.ext !== undefined &&
        artifacts.ext !== '') {                       // Other falsey values allowed


        header += ', ext="' + Hoek.escapeHeaderAttribute(artifacts.ext) + '"';
    }


    return header;
};
fork icon1
star icon0
watch icon1

114
115
116
117
118
119
120
121
122
123

if (message) {
    if (attributes) {
        wwwAuthenticate += ',';
    }
    wwwAuthenticate += ' error="' + Hoek.escapeHeaderAttribute(message) + '"';
}
else {
    err.isMissing = true;
}
fork icon0
star icon0
watch icon1

+ 171 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const Hoek = require("hoek");

// Escape special characters in an HTTP header value
const headerValue =
  "This is a test header value!\"#$%&'()*+,-./:;?@[\\]^_`{|}~";
const escapedHeaderValue = Hoek.escapeHeaderAttribute(headerValue);

// Log the original and escaped header values to the console
console.log(`Original header value: ${headerValue}`);
console.log(`Escaped header value: ${escapedHeaderValue}`);

In this example, we're using hoek.escapeHeaderAttribute to escape special characters in an HTTP header value. We first define a string that contains various special characters, and then pass this string to Hoek.escapeHeaderAttribute. Hoek.escapeHeaderAttribute then escapes the special characters in the string using the percent-encoding scheme, and returns the escaped string. We log both the original and escaped header values to the console using console.log. When you run this code, you'll see that the original header value and the escaped header value are both printed to the console. Note that the escaped header value replaces each special character with a percent sign followed by its ASCII code in hexadecimal, making it safe to use as an HTTP header attribute.

119
120
121
122
123
124
125
126
127
        if (value === null ||
            value === undefined) {              // Value can be zero

            value = '';
        }
        wwwAuthenticate += ' ' + name + '="' + Hoek.escapeHeaderAttribute(value.toString()) + '"';
        err.output.payload.attributes[name] = value;
    }
}
fork icon0
star icon0
watch icon1

114
115
116
117
118
119
120
121
122
123

if (error) {
    if (attributes) {
        wwwAuthenticate += ',';
    }
    wwwAuthenticate += ' error="' + Hoek.escapeHeaderAttribute(error) + '"';
}
else {
    err.isMissing = true;
}
fork icon0
star icon0
watch icon1

+ 5 other calls in file

295
296
297
298
299
300
301
302
303
304
305
306
307


    if (artifacts.ext !== null &&
        artifacts.ext !== undefined &&
        artifacts.ext !== '') {                       // Other falsey values allowed


        header = header + ', ext="' + Hoek.escapeHeaderAttribute(artifacts.ext) + '"';
    }


    return header;
};
fork icon0
star icon0
watch icon1

206
207
208
209
210
211
212
213
214
215

if (message) {
    if (attributes) {
        wwwAuthenticate = wwwAuthenticate + ',';
    }
    wwwAuthenticate = wwwAuthenticate + ' error="' + Hoek.escapeHeaderAttribute(message) + '"';
    err.output.payload.attributes.error = message;
}
else {
    err.isMissing = true;
fork icon0
star icon0
watch icon1

+ 2 other calls in file