Saturday, August 31, 2019

angular - How to loop through a JSON object with typescript (Angular2)

I am new to Angular2 and I am trying to loop through a JSON object that I am getting back from a GET request but can't work it out.




My JSON object:



{
Results: [{
Time: "2017-02-11T08:15:01.000+00:00",
Id: "data-mopdsjkajskda",
AuthorId: "58fSDNJD"
}, {
Time: "2017-03-11T06:23:34.000+00:00",
Id: "data-2371212hjb1",

AuthorId: "43555HHHJ"
}, {
Time: "2017-04-11T07:05:11.000+00:00",
Id: "data-kjskdha22112",
AuthorId: "XDSJKJSDH"
}]
}


Part of my Angular script:




interface res {
Time: string;
Id: string;
AuthorId: string;
}
export class AppComponent {
results: res;
constructor(private _httpservice: HTTPService) {}
this._httpservice.getQuery().subscribe(

data => {
this.results = data.Results
},
error => console.log(error),
() => console.log('Done')
);
}


I do get the data back - which is great. However, I want to push the Ids into an array. In Javascript I would do this:




var ids = [];

for (i = 0; i < data.Results.length; i++) {
ids.push(data.Results[i].Id)
}


The array after the push:




ids = ['data-mopdsjkajskda', 'data-2371212hjb1', 'data-kjskdha22112'];


I am struggling to find a way to achieve the same results with Angular2. Any help would be greatly appreciated!

c# - SMTP error: "Client does not have permission to submit mail to this server"

I'm getting the following error while sending email. What could be the cause?





Client does not have permission to
submit mail to this server. The server
response was: 5.5.1 STARTTLS may not
be repeated.




Here's the stack trace...




Stack Trace



at System.Net.Mail.StartTlsCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)



I'm connecting to smtp.gmail.com with SSL on port 587 / 465

macos - Root directory permissions on Mac OS X 10.6?


I was wondering if it's normal that the root directory / should be owned by “root”.
I get asked for my password every time I want to do something there (e.g. save a file, create a directory) and I don't remember this happening before (though this may just be my faulty memory).
Here's the relevant terminal output:


MacBook:~ ago$ ls -lah /
total 37311
drwxr-xr-x@ 35 root staff 1,2K 22 Mar 12:34 .
drwxr-xr-x@ 35 root staff 1,2K 22 Mar 12:34 ..
-rw-rw-r--@ 1 root admin 21K 22 Mar 10:21 .DS_Store
drwx------ 3 root admin 102B 28 Feb 2008 .Spotlight-V100
d-wx-wx-wt 2 root admin 68B 31 Ago 2009 .Trashes
-rw-r--r--@ 1 ago 501 45K 23 Gen 2008 .VolumeIcon.icns
srwxrwxrwx 1 root staff 0B 22 Mar 12:34 .dbfseventsd
---------- 1 root admin 0B 23 Giu 2009 .file
drwx------ 27 root admin 918B 22 Mar 10:55 .fseventsd
-rw-r--r--@ 1 ago admin 59B 30 Ott 2007 .hidden
-rw------- 1 root wheel 320K 30 Nov 11:42 .hotfiles.btree
drwxr-xr-x@ 2 root wheel 68B 18 Mag 2009 .vol
drwxrwxr-x+ 276 root admin 9,2K 19 Mar 18:28 Applications
drwxrwxr-x@ 21 root admin 714B 14 Nov 12:01 Developer
drwxrwxr-t+ 74 root admin 2,5K 18 Dic 22:14 Library
drwxr-xr-x@ 2 root wheel 68B 23 Giu 2009 Network
drwxr-xr-x 4 root wheel 136B 13 Nov 17:49 System
drwxr-xr-x 6 root admin 204B 31 Ago 2009 Users
drwxrwxrwt@ 4 root admin 136B 22 Mar 12:35 Volumes
drwxr-xr-x@ 39 root wheel 1,3K 13 Nov 17:44 bin
drwxrwxr-t@ 2 root admin 68B 23 Giu 2009 cores
dr-xr-xr-x 3 root wheel 5,1K 17 Mar 11:29 dev
lrwxr-xr-x@ 1 root wheel 11B 31 Ago 2009 etc -> private/etc
dr-xr-xr-x 2 root wheel 1B 17 Mar 11:30 home
drwxrwxrwt@ 3 root wheel 102B 31 Ago 2009 lost+found
-rw-r--r--@ 1 root wheel 18M 3 Nov 19:40 mach_kernel
dr-xr-xr-x 2 root wheel 1B 17 Mar 11:30 net
drwxr-xr-x@ 3 root admin 102B 24 Nov 2007 opt
drwxr-xr-x@ 6 root wheel 204B 31 Ago 2009 private
drwxr-xr-x@ 64 root wheel 2,1K 13 Nov 17:44 sbin
lrwxr-xr-x@ 1 root wheel 11B 31 Ago 2009 tmp -> private/tmp
drwxr-xr-x@ 17 root wheel 578B 12 Set 2009 usr
lrwxr-xr-x@ 1 root wheel 11B 31 Ago 2009 var -> private/var

Are these ownerships / permissions ok? Should I chmod/chown something?


Answer



I just checked a couple of relatively clean OS X 10.6 Macs, and while / is owned by root, its group and permissions are different from what you have: it's assigned to the admin group, and has group write and the sticky bit set, and doesn't have an extended attributes (i.e. drwxrwxr-t 29 root admin). Disk Utility's permissions repair feature doesn't seem to reset this (I just tried), but you can fix it by hand:


sudo chgrp admin /
sudo chmod 1775 /
xattr -l /

The last command will display the extended attributes attached to the root; depending on what they are, you may want to remove them (use sudo xattr -d attrname /).


SQL injection vulnerability to add more balance?

Let's say I have this SQL statement:



stmt.executeUpdate("INSERT INTO TUNEUSER (USERNAME,PASSWORD,BALANCE) VALUES ('"
+ daf.getString("username")
+ "','"

+ daf.getString("password")
+ "',0.00)");


and the application has a username and password field.



How can SQL injection be used to increased the balance from "0.00" to whatever you want?

Friday, August 30, 2019

prepared statement - Mysqli - Are parameterized queries enough for preventing XSS second order attacks?




I am working on a dynamic application and I am not sure if parameterized queries are safe from XSS, second order attacks? Can you help me? Thanks!



I have this code as an example:




$stmt = $mysqli->prepare("INSERT INTO tb (1, 2, 3, 4, 5, 6, 7) VALUES (?, ?, ?, ?, ?, ?, ?)");

$stmt->bind_param('sssssss', $1, $2, $3, $4, $5, $6, $7);
$stmt->execute();

$stmt->close();

Answer



Nope.



A parametrized query protects against SQL Injection; that is it ensures query parameters are well formed and correctly escaped prior to processing by the SQL back end.



XSS is a different class of problem whereby input should be sanitized of HTML markup; given that a correctly parametrized SQL value can still contain markup, you need additional encoding (E.g. htmlspecialchars()).


c# - Huge performance difference (26x faster) when compiling for 32 and 64 bits



I was trying to measure the difference of using a for and a foreach when accessing lists of value types and reference types.



I used the following class to do the profiling.




public static class Benchmarker
{
public static void Profile(string description, int iterations, Action func)
{
Console.Write(description);

// Warm up
func();

Stopwatch watch = new Stopwatch();


// Clean up
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

watch.Start();
for (int i = 0; i < iterations; i++)
{
func();

}
watch.Stop();

Console.WriteLine(" average time: {0} ms", watch.Elapsed.TotalMilliseconds / iterations);
}
}


I used double for my value type.
And I created this 'fake class' to test reference types:




class DoubleWrapper
{
public double Value { get; set; }

public DoubleWrapper(double value)
{
Value = value;
}
}



Finally I ran this code and compared the time differences.



static void Main(string[] args)
{
int size = 1000000;
int iterationCount = 100;

var valueList = new List(size);

for (int i = 0; i < size; i++)
valueList.Add(i);

var refList = new List(size);
for (int i = 0; i < size; i++)
refList.Add(new DoubleWrapper(i));

double dummy;

Benchmarker.Profile("valueList for: ", iterationCount, () =>

{
double result = 0;
for (int i = 0; i < valueList.Count; i++)
{
unchecked
{
var temp = valueList[i];
result *= temp;
result += temp;
result /= temp;

result -= temp;
}
}
dummy = result;
});

Benchmarker.Profile("valueList foreach: ", iterationCount, () =>
{
double result = 0;
foreach (var v in valueList)

{
var temp = v;
result *= temp;
result += temp;
result /= temp;
result -= temp;
}
dummy = result;
});


Benchmarker.Profile("refList for: ", iterationCount, () =>
{
double result = 0;
for (int i = 0; i < refList.Count; i++)
{
unchecked
{
var temp = refList[i].Value;
result *= temp;
result += temp;

result /= temp;
result -= temp;
}
}
dummy = result;
});

Benchmarker.Profile("refList foreach: ", iterationCount, () =>
{
double result = 0;

foreach (var v in refList)
{
unchecked
{
var temp = v.Value;
result *= temp;
result += temp;
result /= temp;
result -= temp;
}

}

dummy = result;
});

SafeExit();
}


I selected Release and Any CPU options, ran the program and got the following times:




valueList for:  average time: 483,967938 ms
valueList foreach: average time: 477,873079 ms
refList for: average time: 490,524197 ms
refList foreach: average time: 485,659557 ms
Done!


Then I selected Release and x64 options, ran the program and got the following times:




valueList for:  average time: 16,720209 ms
valueList foreach: average time: 15,953483 ms
refList for: average time: 19,381077 ms
refList foreach: average time: 18,636781 ms
Done!


Why is x64 bit version so much faster? I expected some difference, but not something this big.



I do not have access to other computers. Could you please run this on your machines and tell me the results? I'm using Visual Studio 2015 and I have an Intel Core i7 930.




Here's the SafeExit() method, so you can compile/run by yourself:



private static void SafeExit()
{
Console.WriteLine("Done!");
Console.ReadLine();
System.Environment.Exit(1);
}



As requested, using double? instead of my DoubleWrapper:



Any CPU



valueList for:  average time: 482,98116 ms
valueList foreach: average time: 478,837701 ms
refList for: average time: 491,075915 ms
refList foreach: average time: 483,206072 ms
Done!



x64



valueList for:  average time: 16,393947 ms
valueList foreach: average time: 15,87007 ms
refList for: average time: 18,267736 ms
refList foreach: average time: 16,496038 ms
Done!



Last but not least: creating a x86 profile gives me almost the same results of using Any CPU.


Answer



I can reproduce this on 4.5.2. No RyuJIT here. Both x86 and x64 disassemblies look reasonable. Range checks and so on are the same. The same basic structure. No loop unrolling.



x86 uses a different set of float instructions. The performance of these instructions seems to be comparable with the x64 instructions except for the division:




  1. The 32 bit x87 float instructions use 10 byte precision internally.

  2. Extended precision division is super slow.




The division operation makes the 32 bit version extremely slow. Uncommenting the division equalizes performance to a large degree (32 bit down from 430ms to 3.25ms).



Peter Cordes points out that the instruction latencies of the two floating point units are not that dissimilar. Maybe some of the intermediate results are denormalized numbers or NaN. These might trigger a slow path in one of the units. Or, maybe the values diverge between the two implementations because of 10 byte vs. 8 byte float precision.



Peter Cordes also points out that all intermediate results are NaN... Removing this problem (valueList.Add(i + 1) so that no divisor is zero) mostly equalizes the results. Apparently, the 32 bit code does not like NaN operands at all. Let's print some intermediate values: if (i % 1000 == 0) Console.WriteLine(result);. This confirms that the data is now sane.



When benchmarking you need to benchmark a realistic workload. But who would have thought that an innocent division can mess up your benchmark?!




Try simply summing the numbers to get a better benchmark.



Division and modulo are always very slow. If you modify the BCL Dictionary code to simply not use the modulo operator to compute the bucket index performance measurable improves. This is how slow division is.



Here's the 32 bit code:



enter image description here



64 bit code (same structure, fast division):




enter image description here



This is not vectorized despite SSE instructions being used.


java - nextLine method in the Scanner



Possible Duplicate:
Scanner issue when using nextLine after nextInt






Why is the empty sc.nextLine(); under the husband so necessary? it did not equal to anything, and surely the wife did not have that line... but the program won't run without it. My book comments it as Skip over trailing newLine character. but I don't see how there's anything to skip!



public class FileTest {
public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("For your blended family, \n enter the wife's name");
String wife = sc.nextLine();
System.out.print("Enter the number of her children:");
int herkids = sc.nextInt();

System.out.print("Enter the husband's name:");
//sc.nextLine(); // Why is this line so necessary?
String husband = sc.nextLine();
System.out.print("Enter the number of his children:");

int hisKids = sc.nextInt();

System.out.println(wife + " and " + husband + " have " + (herkids + hisKids) + " children.");

}
}

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...