SlideShare a Scribd company logo
1 of 44
JavaScript, Sixth Edition
Chapter 9
Managing State and Information Security
Understanding State
Information
State information
◦ Information about individual visits to a Web site
HTTP original design: stateless
◦ No persistent data about site visits stored
Reasons for maintaining state information
◦ Customize individual web pages
◦ Store information within a multipart form
◦ Provide shopping carts
JAVASCRIPT, SIXTH EDITION 2
Saving State Information with
Query Strings
Query string
◦ Set of name-value pairs
◦ Appended to a target URL
◦ Consists of a single text string
◦ Contains one or more pieces of information
Passes information from one web page to another
JAVASCRIPT, SIXTH EDITION 3
Saving State Information with
Query Strings (cont’d.)
Passing data with a query string
◦ Add a question mark (?) immediately after a URL
◦ Followed by the query string (in name-value pairs) for the information to preserve
◦ Ampersands (&)
◦ Separates individual name-value pairs within the query string
◦ Example:
<a href="http://www.example.com/↵
addItem.html?isbn=9780394800165&quantity=2">Order Book</a>
JAVASCRIPT, SIXTH EDITION 4
Saving State Information with
Query Strings (cont’d.)
◦ Passed query string assigned to target web page Location object search
property
◦ search property of the Location object contains a URL’s query or search parameters
From preceding example:
◦ Query string
◦ "?isbn=9780394800165&quantity=2"
◦ Available as the value of the search property of the Location object
◦ After addItem.html document opens
JAVASCRIPT, SIXTH EDITION 5
Saving State Information with
Query Strings (cont’d.)
Remove the question mark
◦ Using the substring() method combined with the length property
◦ Example:
// Assign the query string to the queryData variable
var queryData = location.search;
// Remove the opening question mark from the string
queryData = queryData.substring(1, queryData.length);
JAVASCRIPT, SIXTH EDITION 6
Saving State Information with
Query Strings (cont’d.)
Convert individual pieces of information into array elements
◦ Using the split() method
◦ Example:
◦ Convert the information in the queryData variable into an array named queryArray[]
// split queryData into an array
var queryArray = queryData.split("&");
JAVASCRIPT, SIXTH EDITION 7
Saving State Information with
Hidden Form Fields
Hidden form field
◦ Special type of form element
◦ Not displayed by web browser
◦ Allows information to be hidden from users
◦ Created with the input element
◦ Temporarily stores data sent to a server along with the rest of a form
◦ No need for user to see hidden data
Syntax
<input type="hidden">
JAVASCRIPT, SIXTH EDITION 8
Saving State Information with
Hidden Form Fields (cont’d.)
JAVASCRIPT, SIXTH EDITION 9
Figure 9-7 Submitting data from multiple forms using hidden fields
Saving State Information with
Cookies
Query strings and hidden form fields temporarily maintain state
information
Cookies
◦ Small pieces of information about a user
◦ Stored by a web server in text files
◦ Stored on the user’s computer
When Web client visits a web server
◦ Saved cookies sent from client to the server
Temporary cookies
◦ Available only for current browser session
JAVASCRIPT, SIXTH EDITION 10
Storing State Information
Query strings and hidden form fields temporarily maintain state
information
Two methods of storing state information on user's computer
◦ Cookies
◦ Web Storage
JAVASCRIPT, SIXTH EDITION 11
Storing State Information with
Cookies
Cookies
◦ Small pieces of information
◦ Stored in text files
Temporary cookies
◦ Remain available only for current browser session
Persistent cookies
◦ Remain available beyond current browser session
◦ Stored in a text file on a client computer
JAVASCRIPT, SIXTH EDITION 12
Storing State Information with
Cookies (cont'd.)
Limitations on the use of cookies
◦ Server or domain can store a maximum of 20 cookies
◦ Total cookies per browser cannot exceed 300
◦ Largest cookie size is 4 kilobytes
Not enforced by all browsers
JAVASCRIPT, SIXTH EDITION 13
Creating and Modifying
Cookies
Use the cookie property of the Document object
◦ Creates cookies in name-value pairs
◦ Syntax
document.cookie = name + "=" + value;
◦ Cookie property created with a required name attribute and four optional
attributes:
◦ expires, path, domain, secure
JAVASCRIPT, SIXTH EDITION 14
Creating and Modifying
Cookies (cont’d.)
JAVASCRIPT, SIXTH EDITION 15
Table 9-1 Cookie attributes
Creating and Modifying
Cookies (cont’d.)
name attribute
◦ Only required parameter
◦ Specifies cookie’s name-value pair
◦ Cookies created with only the name attribute:
◦ Temporary cookies
◦ cookie property adds another entry to a list of cookies
JAVASCRIPT, SIXTH EDITION 16
Creating and Modifying
Cookies (cont’d.)
◦ Example: build a list of cookies
document.cookie = "username=mw101";
document.cookie = "member=true";
document.cookie = "audio=false";
◦ Resulting value of cookie property:
username=mw101; member=true; audio=false
JAVASCRIPT, SIXTH EDITION 17
Creating and Modifying
Cookies (cont’d.)
◦ Cookies cannot include semicolons or special characters
◦ Can use special characters in cookies if using encoding
◦ Encoding involves converting special characters in a text string
◦ To corresponding hexadecimal ASCII value preceded by a percent sign
JAVASCRIPT, SIXTH EDITION 18
Creating and Modifying
Cookies (cont’d.)
◦ encodeURIComponent() function
◦ Used for encoding the individual parts of a URI
◦ Converts special characters in the individual parts of a URI to corresponding hexadecimal ASCII
value
◦ Syntax: encodeURIComponent(text)
◦ decodeURIComponent() function
◦ Counterpart of encodeURIComponent() function
◦ Syntax: decodeURIComponent(text)
JAVASCRIPT, SIXTH EDITION 19
Creating and Modifying
Cookies (cont’d.)
Testing Applications That Use Cookies
◦ Browsers open local documents without HTTP
◦ Some browsers don't support setting cookies when HTTP protocol not used
◦ Can focus testing on browsers that support cookies on files opened without
HTTP
◦ More robust option
◦ run local web server
◦ open files using HTTP protocol
◦ some code editors include built-in testing server
JAVASCRIPT, SIXTH EDITION 20
Creating and Modifying
Cookies (cont’d.)
expires attribute
◦ Determines how long a cookie can remain on a client system before being
deleted
◦ Cookies created without this attribute
◦ Available current browser session only
◦ Syntax: expires=date
◦ name-value pair and the expires=date pair separated by a semicolon
◦ Do not encode expires attribute
JAVASCRIPT, SIXTH EDITION 21
Creating and Modifying
Cookies (cont’d.)
◦ Can manually type a string in UTC format or:
◦ Can create string with the Date object
◦ Use the toUTCString() method to convert the Date object to a string
◦ Example:
JAVASCRIPT, SIXTH EDITION 22
var expiresDate = new Date();
var username = document.getElementById("username").value;
expiresDate.setFullYear(expiresDate.getFullYear() + 1);
document.cookie = "username=" +↵
encodeURIComponent(username) + "; expires=" +↵
expiresDate.toUTCString();
Creating and Modifying
Cookies (cont’d.)
Configuring availability of cookies to other web pages on the server
◦ use path attribute
◦ Default: cookie available to all web pages in the same directory
◦ To make cookie available to all directories on a server
◦ Use a slash
JAVASCRIPT, SIXTH EDITION 23
Creating and Modifying
Cookies (cont’d.)
◦ Example:
var username = document.getElementById("username").value;
document.cookie = "username=" +↵
encodeURIComponent(username + "; path=/advertising");
◦ Example:
document.cookie = "username=" +↵
encodeURIComponent(username + "; path=/");
◦ Cookies from other programs stored in the same directory
◦ Can cause JavaScript program to run erratically
JAVASCRIPT, SIXTH EDITION 24
Creating and Modifying
Cookies (cont’d.)
Sharing cookies across a domain
◦ use domain attribute
◦ Example:
var username = document.getElementById("username").value;
document.cookie = "username=" +↵
encodeURIComponent(username +↵
"; domain=.example.com");
◦ Cannot share cookies outside of a domain
JAVASCRIPT, SIXTH EDITION 25
Creating and Modifying
Cookies (cont’d.)
Securing cookie transmissions
◦ secure attribute
◦ Indicates cookie can only be transmitted across a secure Internet connection
◦ Using HTTPS or another security protocol
◦ Example:
var username = document.getElementById("username").value;
document.cookie = "username=" +↵
encodeURIComponent(username + "; secure=true");
JAVASCRIPT, SIXTH EDITION 26
Reading Cookies with
JavaScript
Parsing a cookie
◦ Decode it using decodeURIComponent() function
◦ Use the String object methods to extract individual name-value pairs
◦ Similar to parsing query strings
◦ Do not need to remove the question mark at the beginning of the string
◦ Individual cookies separated by a semicolon and a space instead of ampersands
JAVASCRIPT, SIXTH EDITION 27
Reading Cookies with
JavaScript (cont’d.)
◦ Example:
◦ Create three encoded cookies
◦ Read them from the cookie property, decode them
◦ Use the split() method to copy each name-value pair into cookieArray[] array
elements
◦ Determine which cookie holds needed value
◦ for loop to cycle through array elements
◦ if statement to check name portion of each name-value pair
JAVASCRIPT, SIXTH EDITION 28
JAVASCRIPT, SIXTH EDITION 29
document.cookie = "username=" +↵
encodeURIComponent(username);
document.cookie = "member=" +↵
encodeURIComponent(member);
document.cookie = "audio=" +↵
encodeURIComponent(audio);
var cookieString = decodeURIComponent(document.cookie);
var cookieArray = cookieString.split("; ");
Reading Cookies with JavaScript
(cont’d.)
JAVASCRIPT, SIXTH EDITION 30
var currentUsername;
var unBox = document.getElementById("username");
for (var i = 0; i < 3; i++) {
currentUsername = cookieArray[i];
if↵
(currentUsername.substring(0,currentUsername.indexOf("="))↵
=== "username") {
unBox.value =↵
currentUsername.substring(currentUsername.↵
indexOf("=") + 1,currentUsername.length);
break;
}
}
Reading Cookies with JavaScript
(cont’d.)
Deleting Cookies with
JavaScript
Not intuitive
◦ Must set cookie expiration to a date in the past
Example:
◦ Delete username cookie by setting its expires attribute to one week ago
var expiresDate = new Date();
var username = document.getElementById("username").value;
expiresDate.setDate(expiresDate.getDate() - 7);
document.cookie = "username=" +↵
encodeURIComponent(username) + "; expires=" +↵
expiresDate.toUTCString();
JAVASCRIPT, SIXTH EDITION 31
Deleting Cookies from Your
Browser
Persistent cookies can
interfere with testing
Good idea to delete
cookies periodically
JAVASCRIPT, SIXTH EDITION 32
Table 9-2 Steps to delete cookies in IE, Firefox, and Chrome
Storing State Information with
the Web Storage API
Common uses of cookies never originally planned
Web Storage
◦ Draft specification of W3C
◦ Not supported by some older browsers
◦ Cookies are still the standard
◦ Web Storage offers additional features
◦ Applications targeted at specific browsers can use Web Storage
JAVASCRIPT, SIXTH EDITION 33
Storing State Information with
the Web Storage API (cont'd.)
Two Web Storage properties of Window object
◦ localStorage
◦ Remains until you run code to delete it
◦ Similar to persistent cookies
◦ sessionStorage
◦ Removed automatically when user closes browser tab or window
◦ Like temporary cookies
JAVASCRIPT, SIXTH EDITION 34
Storing State Information with
the Web Storage API (cont'd.)
Storing/reading Web Storage easier than cookies
◦ Example:
var firstName = document.getElementById("fname").value;
localStorage.fName = firstName;
To use a method
◦ Preface it with localStorage or sessionStorage
JAVASCRIPT, SIXTH EDITION 35
Storing State Information with
the Web Storage API (cont'd.)
JAVASCRIPT, SIXTH EDITION 36
Table 9-3 Methods of the Web Storage API
Understanding Security Issues
Security threats
◦ Viruses, worms, data theft by hackers
Consider web server security and secure coding issues
Web server security technologies
◦ Firewalls
◦ Secure Socket Layer (SSL)
JavaScript programs downloaded and execute locally
JAVASCRIPT, SIXTH EDITION 37
Secure Coding with JavaScript
Secure coding or defensive coding
◦ Writing code to minimize any intentional or accidental security issues
All code: insecure unless proven otherwise
No magic formula for writing secure code
JAVASCRIPT, SIXTH EDITION 38
JavaScript Security Concerns
Security areas of most concern:
◦ Protection of a web page and JavaScript program against malicious
tampering
◦ Privacy of individual client information
◦ Protection of the local file system of the client or web site from theft or
tampering
Code injection attack
◦ Program or user enters JavaScript code that changes function of web page
◦ Validating forms helps prevent this
◦ Escaping characters also important
JAVASCRIPT, SIXTH EDITION 39
JavaScript Security Concerns
Another security concern
◦ Privacy of individual client information in the web browser window
Important JavaScript security feature
◦ Lack of certain types of functionality
◦ File manipulation
◦ Create a network connection
◦ Cannot run system commands or execute programs on a client
JAVASCRIPT, SIXTH EDITION 40
The Same Origin Policy
Restricts how JavaScript code access between windows/tabs/frames
To view and modify elements in other windows and frames
◦ They must have the same protocol and exist on the same web server
Same origin policy applies to:
◦ The domain name
◦ The server on which a document located
JAVASCRIPT, SIXTH EDITION 41
The Same Origin Policy
(cont’d.)
Policy prevents:
◦ Malicious scripts from modifying content of other windows and frames
◦ Theft of private browser information and information displayed on secure
web pages
Policy protects integrity of web page design
JAVASCRIPT, SIXTH EDITION 42
The Same Origin Policy
(cont’d.)
domain property of the Document object
◦ Changes origin of a document to its root domain name
◦ Allows documents from different origins in the same domain to access each
other’s elements and properties
JAVASCRIPT, SIXTH EDITION 43
Using Third-Party Scripts
Sometimes you want to run scripts from other domains
◦ Known as third-party scripts
◦ Examples:
◦ Widgets that run from provider's server
◦ Using Content Delivery Network (CDN)
To enable third-party script
◦ Include script element with src value pointing to third-party content
◦ Exception to same origin policy
JAVASCRIPT, SIXTH EDITION 44

More Related Content

What's hot

What's hot (20)

PowerShell-1
PowerShell-1PowerShell-1
PowerShell-1
 
TypeScript
TypeScriptTypeScript
TypeScript
 
DDD Modeling Workshop
DDD Modeling WorkshopDDD Modeling Workshop
DDD Modeling Workshop
 
OAuth 2.0
OAuth 2.0OAuth 2.0
OAuth 2.0
 
jQuery
jQueryjQuery
jQuery
 
Cascading Style Sheets(CSS)
Cascading Style Sheets(CSS)Cascading Style Sheets(CSS)
Cascading Style Sheets(CSS)
 
Qt programming-using-cpp
Qt programming-using-cppQt programming-using-cpp
Qt programming-using-cpp
 
Lightweight Xtext Editors as SWT Widgets
Lightweight Xtext Editors as SWT WidgetsLightweight Xtext Editors as SWT Widgets
Lightweight Xtext Editors as SWT Widgets
 
Decentralized Identifiers
Decentralized IdentifiersDecentralized Identifiers
Decentralized Identifiers
 
Windows PowerShell
Windows PowerShellWindows PowerShell
Windows PowerShell
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Sql
SqlSql
Sql
 
ERD를 이용한 DB 모델링
ERD를 이용한 DB 모델링ERD를 이용한 DB 모델링
ERD를 이용한 DB 모델링
 
Stored procedure in sql server
Stored procedure in sql serverStored procedure in sql server
Stored procedure in sql server
 
Long running processes in DDD
Long running processes in DDDLong running processes in DDD
Long running processes in DDD
 
6. Utilización del modelo de objetos del documento (DOM)
6. Utilización del modelo de objetos del documento (DOM)6. Utilización del modelo de objetos del documento (DOM)
6. Utilización del modelo de objetos del documento (DOM)
 
FIDO UAF Specifications: Overview & Tutorial
FIDO UAF Specifications: Overview & Tutorial FIDO UAF Specifications: Overview & Tutorial
FIDO UAF Specifications: Overview & Tutorial
 
Sql injection
Sql injectionSql injection
Sql injection
 
Getting Started in Blockchain Security and Smart Contract Auditing
Getting Started in Blockchain Security and Smart Contract AuditingGetting Started in Blockchain Security and Smart Contract Auditing
Getting Started in Blockchain Security and Smart Contract Auditing
 
Java script
Java scriptJava script
Java script
 

Similar to Ch09 -Managing State and Information Security

9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptxVatsalJain39
 
19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptxssuser4a97d3
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3Neeraj Mathur
 
Java script Advance
Java script   AdvanceJava script   Advance
Java script AdvanceJaya Kumari
 
Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesDeeptiJava
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)WebStackAcademy
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actionsAren Zomorodian
 
javaScriptCookies.pptx
javaScriptCookies.pptxjavaScriptCookies.pptx
javaScriptCookies.pptxMattMarino13
 
WordPress Security 101: Essential Security Practices Simplified
WordPress Security 101: Essential Security Practices SimplifiedWordPress Security 101: Essential Security Practices Simplified
WordPress Security 101: Essential Security Practices SimplifiedBlogVault Inc
 
State management
State managementState management
State managementLalit Kale
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Vivek chan
 

Similar to Ch09 -Managing State and Information Security (20)

9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx19_JavaScript - Storage_Cookies_students.pptx
19_JavaScript - Storage_Cookies_students.pptx
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx
 
Ch05 state management
Ch05 state managementCh05 state management
Ch05 state management
 
Ecom2
Ecom2Ecom2
Ecom2
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3
 
Chapter 8 part1
Chapter 8   part1Chapter 8   part1
Chapter 8 part1
 
Java script Advance
Java script   AdvanceJava script   Advance
Java script Advance
 
State management
State managementState management
State management
 
Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
ASP.NET Lecture 5
ASP.NET Lecture 5ASP.NET Lecture 5
ASP.NET Lecture 5
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
javaScriptCookies.pptx
javaScriptCookies.pptxjavaScriptCookies.pptx
javaScriptCookies.pptx
 
WordPress Security 101: Essential Security Practices Simplified
WordPress Security 101: Essential Security Practices SimplifiedWordPress Security 101: Essential Security Practices Simplified
WordPress Security 101: Essential Security Practices Simplified
 
State management
State managementState management
State management
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 

Recently uploaded

Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

Ch09 -Managing State and Information Security

  • 1. JavaScript, Sixth Edition Chapter 9 Managing State and Information Security
  • 2. Understanding State Information State information ◦ Information about individual visits to a Web site HTTP original design: stateless ◦ No persistent data about site visits stored Reasons for maintaining state information ◦ Customize individual web pages ◦ Store information within a multipart form ◦ Provide shopping carts JAVASCRIPT, SIXTH EDITION 2
  • 3. Saving State Information with Query Strings Query string ◦ Set of name-value pairs ◦ Appended to a target URL ◦ Consists of a single text string ◦ Contains one or more pieces of information Passes information from one web page to another JAVASCRIPT, SIXTH EDITION 3
  • 4. Saving State Information with Query Strings (cont’d.) Passing data with a query string ◦ Add a question mark (?) immediately after a URL ◦ Followed by the query string (in name-value pairs) for the information to preserve ◦ Ampersands (&) ◦ Separates individual name-value pairs within the query string ◦ Example: <a href="http://www.example.com/↵ addItem.html?isbn=9780394800165&quantity=2">Order Book</a> JAVASCRIPT, SIXTH EDITION 4
  • 5. Saving State Information with Query Strings (cont’d.) ◦ Passed query string assigned to target web page Location object search property ◦ search property of the Location object contains a URL’s query or search parameters From preceding example: ◦ Query string ◦ "?isbn=9780394800165&quantity=2" ◦ Available as the value of the search property of the Location object ◦ After addItem.html document opens JAVASCRIPT, SIXTH EDITION 5
  • 6. Saving State Information with Query Strings (cont’d.) Remove the question mark ◦ Using the substring() method combined with the length property ◦ Example: // Assign the query string to the queryData variable var queryData = location.search; // Remove the opening question mark from the string queryData = queryData.substring(1, queryData.length); JAVASCRIPT, SIXTH EDITION 6
  • 7. Saving State Information with Query Strings (cont’d.) Convert individual pieces of information into array elements ◦ Using the split() method ◦ Example: ◦ Convert the information in the queryData variable into an array named queryArray[] // split queryData into an array var queryArray = queryData.split("&"); JAVASCRIPT, SIXTH EDITION 7
  • 8. Saving State Information with Hidden Form Fields Hidden form field ◦ Special type of form element ◦ Not displayed by web browser ◦ Allows information to be hidden from users ◦ Created with the input element ◦ Temporarily stores data sent to a server along with the rest of a form ◦ No need for user to see hidden data Syntax <input type="hidden"> JAVASCRIPT, SIXTH EDITION 8
  • 9. Saving State Information with Hidden Form Fields (cont’d.) JAVASCRIPT, SIXTH EDITION 9 Figure 9-7 Submitting data from multiple forms using hidden fields
  • 10. Saving State Information with Cookies Query strings and hidden form fields temporarily maintain state information Cookies ◦ Small pieces of information about a user ◦ Stored by a web server in text files ◦ Stored on the user’s computer When Web client visits a web server ◦ Saved cookies sent from client to the server Temporary cookies ◦ Available only for current browser session JAVASCRIPT, SIXTH EDITION 10
  • 11. Storing State Information Query strings and hidden form fields temporarily maintain state information Two methods of storing state information on user's computer ◦ Cookies ◦ Web Storage JAVASCRIPT, SIXTH EDITION 11
  • 12. Storing State Information with Cookies Cookies ◦ Small pieces of information ◦ Stored in text files Temporary cookies ◦ Remain available only for current browser session Persistent cookies ◦ Remain available beyond current browser session ◦ Stored in a text file on a client computer JAVASCRIPT, SIXTH EDITION 12
  • 13. Storing State Information with Cookies (cont'd.) Limitations on the use of cookies ◦ Server or domain can store a maximum of 20 cookies ◦ Total cookies per browser cannot exceed 300 ◦ Largest cookie size is 4 kilobytes Not enforced by all browsers JAVASCRIPT, SIXTH EDITION 13
  • 14. Creating and Modifying Cookies Use the cookie property of the Document object ◦ Creates cookies in name-value pairs ◦ Syntax document.cookie = name + "=" + value; ◦ Cookie property created with a required name attribute and four optional attributes: ◦ expires, path, domain, secure JAVASCRIPT, SIXTH EDITION 14
  • 15. Creating and Modifying Cookies (cont’d.) JAVASCRIPT, SIXTH EDITION 15 Table 9-1 Cookie attributes
  • 16. Creating and Modifying Cookies (cont’d.) name attribute ◦ Only required parameter ◦ Specifies cookie’s name-value pair ◦ Cookies created with only the name attribute: ◦ Temporary cookies ◦ cookie property adds another entry to a list of cookies JAVASCRIPT, SIXTH EDITION 16
  • 17. Creating and Modifying Cookies (cont’d.) ◦ Example: build a list of cookies document.cookie = "username=mw101"; document.cookie = "member=true"; document.cookie = "audio=false"; ◦ Resulting value of cookie property: username=mw101; member=true; audio=false JAVASCRIPT, SIXTH EDITION 17
  • 18. Creating and Modifying Cookies (cont’d.) ◦ Cookies cannot include semicolons or special characters ◦ Can use special characters in cookies if using encoding ◦ Encoding involves converting special characters in a text string ◦ To corresponding hexadecimal ASCII value preceded by a percent sign JAVASCRIPT, SIXTH EDITION 18
  • 19. Creating and Modifying Cookies (cont’d.) ◦ encodeURIComponent() function ◦ Used for encoding the individual parts of a URI ◦ Converts special characters in the individual parts of a URI to corresponding hexadecimal ASCII value ◦ Syntax: encodeURIComponent(text) ◦ decodeURIComponent() function ◦ Counterpart of encodeURIComponent() function ◦ Syntax: decodeURIComponent(text) JAVASCRIPT, SIXTH EDITION 19
  • 20. Creating and Modifying Cookies (cont’d.) Testing Applications That Use Cookies ◦ Browsers open local documents without HTTP ◦ Some browsers don't support setting cookies when HTTP protocol not used ◦ Can focus testing on browsers that support cookies on files opened without HTTP ◦ More robust option ◦ run local web server ◦ open files using HTTP protocol ◦ some code editors include built-in testing server JAVASCRIPT, SIXTH EDITION 20
  • 21. Creating and Modifying Cookies (cont’d.) expires attribute ◦ Determines how long a cookie can remain on a client system before being deleted ◦ Cookies created without this attribute ◦ Available current browser session only ◦ Syntax: expires=date ◦ name-value pair and the expires=date pair separated by a semicolon ◦ Do not encode expires attribute JAVASCRIPT, SIXTH EDITION 21
  • 22. Creating and Modifying Cookies (cont’d.) ◦ Can manually type a string in UTC format or: ◦ Can create string with the Date object ◦ Use the toUTCString() method to convert the Date object to a string ◦ Example: JAVASCRIPT, SIXTH EDITION 22 var expiresDate = new Date(); var username = document.getElementById("username").value; expiresDate.setFullYear(expiresDate.getFullYear() + 1); document.cookie = "username=" +↵ encodeURIComponent(username) + "; expires=" +↵ expiresDate.toUTCString();
  • 23. Creating and Modifying Cookies (cont’d.) Configuring availability of cookies to other web pages on the server ◦ use path attribute ◦ Default: cookie available to all web pages in the same directory ◦ To make cookie available to all directories on a server ◦ Use a slash JAVASCRIPT, SIXTH EDITION 23
  • 24. Creating and Modifying Cookies (cont’d.) ◦ Example: var username = document.getElementById("username").value; document.cookie = "username=" +↵ encodeURIComponent(username + "; path=/advertising"); ◦ Example: document.cookie = "username=" +↵ encodeURIComponent(username + "; path=/"); ◦ Cookies from other programs stored in the same directory ◦ Can cause JavaScript program to run erratically JAVASCRIPT, SIXTH EDITION 24
  • 25. Creating and Modifying Cookies (cont’d.) Sharing cookies across a domain ◦ use domain attribute ◦ Example: var username = document.getElementById("username").value; document.cookie = "username=" +↵ encodeURIComponent(username +↵ "; domain=.example.com"); ◦ Cannot share cookies outside of a domain JAVASCRIPT, SIXTH EDITION 25
  • 26. Creating and Modifying Cookies (cont’d.) Securing cookie transmissions ◦ secure attribute ◦ Indicates cookie can only be transmitted across a secure Internet connection ◦ Using HTTPS or another security protocol ◦ Example: var username = document.getElementById("username").value; document.cookie = "username=" +↵ encodeURIComponent(username + "; secure=true"); JAVASCRIPT, SIXTH EDITION 26
  • 27. Reading Cookies with JavaScript Parsing a cookie ◦ Decode it using decodeURIComponent() function ◦ Use the String object methods to extract individual name-value pairs ◦ Similar to parsing query strings ◦ Do not need to remove the question mark at the beginning of the string ◦ Individual cookies separated by a semicolon and a space instead of ampersands JAVASCRIPT, SIXTH EDITION 27
  • 28. Reading Cookies with JavaScript (cont’d.) ◦ Example: ◦ Create three encoded cookies ◦ Read them from the cookie property, decode them ◦ Use the split() method to copy each name-value pair into cookieArray[] array elements ◦ Determine which cookie holds needed value ◦ for loop to cycle through array elements ◦ if statement to check name portion of each name-value pair JAVASCRIPT, SIXTH EDITION 28
  • 29. JAVASCRIPT, SIXTH EDITION 29 document.cookie = "username=" +↵ encodeURIComponent(username); document.cookie = "member=" +↵ encodeURIComponent(member); document.cookie = "audio=" +↵ encodeURIComponent(audio); var cookieString = decodeURIComponent(document.cookie); var cookieArray = cookieString.split("; "); Reading Cookies with JavaScript (cont’d.)
  • 30. JAVASCRIPT, SIXTH EDITION 30 var currentUsername; var unBox = document.getElementById("username"); for (var i = 0; i < 3; i++) { currentUsername = cookieArray[i]; if↵ (currentUsername.substring(0,currentUsername.indexOf("="))↵ === "username") { unBox.value =↵ currentUsername.substring(currentUsername.↵ indexOf("=") + 1,currentUsername.length); break; } } Reading Cookies with JavaScript (cont’d.)
  • 31. Deleting Cookies with JavaScript Not intuitive ◦ Must set cookie expiration to a date in the past Example: ◦ Delete username cookie by setting its expires attribute to one week ago var expiresDate = new Date(); var username = document.getElementById("username").value; expiresDate.setDate(expiresDate.getDate() - 7); document.cookie = "username=" +↵ encodeURIComponent(username) + "; expires=" +↵ expiresDate.toUTCString(); JAVASCRIPT, SIXTH EDITION 31
  • 32. Deleting Cookies from Your Browser Persistent cookies can interfere with testing Good idea to delete cookies periodically JAVASCRIPT, SIXTH EDITION 32 Table 9-2 Steps to delete cookies in IE, Firefox, and Chrome
  • 33. Storing State Information with the Web Storage API Common uses of cookies never originally planned Web Storage ◦ Draft specification of W3C ◦ Not supported by some older browsers ◦ Cookies are still the standard ◦ Web Storage offers additional features ◦ Applications targeted at specific browsers can use Web Storage JAVASCRIPT, SIXTH EDITION 33
  • 34. Storing State Information with the Web Storage API (cont'd.) Two Web Storage properties of Window object ◦ localStorage ◦ Remains until you run code to delete it ◦ Similar to persistent cookies ◦ sessionStorage ◦ Removed automatically when user closes browser tab or window ◦ Like temporary cookies JAVASCRIPT, SIXTH EDITION 34
  • 35. Storing State Information with the Web Storage API (cont'd.) Storing/reading Web Storage easier than cookies ◦ Example: var firstName = document.getElementById("fname").value; localStorage.fName = firstName; To use a method ◦ Preface it with localStorage or sessionStorage JAVASCRIPT, SIXTH EDITION 35
  • 36. Storing State Information with the Web Storage API (cont'd.) JAVASCRIPT, SIXTH EDITION 36 Table 9-3 Methods of the Web Storage API
  • 37. Understanding Security Issues Security threats ◦ Viruses, worms, data theft by hackers Consider web server security and secure coding issues Web server security technologies ◦ Firewalls ◦ Secure Socket Layer (SSL) JavaScript programs downloaded and execute locally JAVASCRIPT, SIXTH EDITION 37
  • 38. Secure Coding with JavaScript Secure coding or defensive coding ◦ Writing code to minimize any intentional or accidental security issues All code: insecure unless proven otherwise No magic formula for writing secure code JAVASCRIPT, SIXTH EDITION 38
  • 39. JavaScript Security Concerns Security areas of most concern: ◦ Protection of a web page and JavaScript program against malicious tampering ◦ Privacy of individual client information ◦ Protection of the local file system of the client or web site from theft or tampering Code injection attack ◦ Program or user enters JavaScript code that changes function of web page ◦ Validating forms helps prevent this ◦ Escaping characters also important JAVASCRIPT, SIXTH EDITION 39
  • 40. JavaScript Security Concerns Another security concern ◦ Privacy of individual client information in the web browser window Important JavaScript security feature ◦ Lack of certain types of functionality ◦ File manipulation ◦ Create a network connection ◦ Cannot run system commands or execute programs on a client JAVASCRIPT, SIXTH EDITION 40
  • 41. The Same Origin Policy Restricts how JavaScript code access between windows/tabs/frames To view and modify elements in other windows and frames ◦ They must have the same protocol and exist on the same web server Same origin policy applies to: ◦ The domain name ◦ The server on which a document located JAVASCRIPT, SIXTH EDITION 41
  • 42. The Same Origin Policy (cont’d.) Policy prevents: ◦ Malicious scripts from modifying content of other windows and frames ◦ Theft of private browser information and information displayed on secure web pages Policy protects integrity of web page design JAVASCRIPT, SIXTH EDITION 42
  • 43. The Same Origin Policy (cont’d.) domain property of the Document object ◦ Changes origin of a document to its root domain name ◦ Allows documents from different origins in the same domain to access each other’s elements and properties JAVASCRIPT, SIXTH EDITION 43
  • 44. Using Third-Party Scripts Sometimes you want to run scripts from other domains ◦ Known as third-party scripts ◦ Examples: ◦ Widgets that run from provider's server ◦ Using Content Delivery Network (CDN) To enable third-party script ◦ Include script element with src value pointing to third-party content ◦ Exception to same origin policy JAVASCRIPT, SIXTH EDITION 44